import { faker } from '@faker-js/faker'
import { nanoid } from '../../nanoid.js'
import { IUser, Kind as UserKind } from '../../models/store/User.js'

export interface IUserInit {
  noEmail?: boolean
  noPicture?: boolean
  noProvider?: boolean
}

export class User {
  /**
   * Generates a random user object.
   * @param init Configuration options for the user object.
   * @returns Random user
   */
  user(init: IUserInit = {}): IUser {
    const result: IUser = {
      kind: UserKind,
      key: nanoid(),
      name: faker.person.fullName(),
      status: faker.helpers.arrayElement(['active', 'deleted', 'suspended', 'pending']),
      email: [],
      created: faker.date.past().getTime(),
      updated: faker.date.recent().getTime(),
    }
    if (!init.noEmail) {
      result.email = [
        {
          email: faker.internet.email(),
          verified: faker.datatype.boolean(),
        },
      ]
    }
    if (!init.noPicture) {
      result.picture = {
        url: faker.image.avatar(),
      }
    }
    if (!init.noProvider) {
      result.provider = {
        refreshToken: faker.string.alphanumeric(21),
      }
    }
    return result
  }

  /**
   * Generates a list of random user objects.
   * @param size The size of the returning array
   * @param init Configuration options for the user object.
   * @returns List of random users
   */
  users(size = 25, init: IUserInit = {}): IUser[] {
    const result: IUser[] = []
    for (let i = 0; i < size; i++) {
      result.push(this.user(init))
    }
    return result
  }
}
