import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Role } from 'src/roles/entities/role.entity';
import { RoleEnum } from 'src/roles/roles.enum';
import { Repository } from 'typeorm';

@Injectable()
export class RoleSeedService {
  constructor(
    @InjectRepository(Role)
    private repository: Repository<Role>,
  ) {}

  async run() {
    const countPlayer = await this.repository.count({
      where: {
        id: RoleEnum.user,
      },
    });

    if (countPlayer === 0) {
      await this.repository.save(
        this.repository.create({
          id: RoleEnum.user,
          name: 'user',
        }),
      );
    }

    const countAdmins = await this.repository.count({
      where: {
        id: RoleEnum.admin,
      },
    });

    if (countAdmins === 0) {
      await this.repository.save(
        this.repository.create({
          id: RoleEnum.admin,
          name: 'admin',
        }),
      );
    }
  }
}
