import { hashPassword } from 'src/core/helpers/password/bcrypt.helpers';
import { SALT_OR_ROUNDS } from 'src/core/strings/constants/base.constants';
import { default_admin } from 'src/core/strings/constants/default-data.constants';
import { UserModel } from 'src/modules/user-related/user/data/models/user.model';
import { Connection } from 'typeorm';
import { Factory, Seeder } from 'typeorm-seeding';

export class SeedDefaultAdmin implements Seeder {
  public async run(factory: Factory, connection: Connection): Promise<void> {
    try {
      const data = new UserModel();
      Object.assign(data, {
        ...default_admin,
        password: await hashPassword(default_admin.password, SALT_OR_ROUNDS),
        created_at: new Date().getTime(),
        updated_at: new Date().getTime(),
      });

      await connection
        .createQueryBuilder()
        .insert()
        .into(UserModel)
        .values([data])
        .execute();
    } catch (error) {}
  }
}
