import { Injectable } from "@nestjs/common"
import { DEFAULT_FLAGS } from "../../../constants/flag.constant"
import { Flags } from "../../../modules/employee-flags/entities/flag.entity"
import { FlagRepository } from "../../../modules/employee-flags/repositories/flag.repository"

@Injectable()
export class FlagSeedService {
  constructor(private readonly flagRepository: FlagRepository) {}

  async run() {
    try {
      for (const flag of DEFAULT_FLAGS) {
        const findExistingFlags = await this.flagRepository.getByParams({
          where: {
            name: flag,
          },
          findOne: true,
        })

        if (findExistingFlags) {
          console.log(`Already found the ${flag} flag`)
        } else {
          const flagObj = new Flags()
          flagObj.name = flag

          await this.flagRepository.save(flagObj)
          console.log(`Created the ${flag} flag`)
        }
      }
    } catch (error) {
      console.error("Error running Flag seeder:", error)
    }
  }
}
