import { Injectable } from "@nestjs/common"
import { InjectRepository } from "@nestjs/typeorm"
import { Country } from "src/modules/country/entities/country.entity"
import { countries } from "src/static-data/country"
import { Repository } from "typeorm"

@Injectable()
export class CountrySeedService {
  constructor(
    @InjectRepository(Country)
    private readonly countryRepository: Repository<Country>,
  ) {}

  async run() {
    try {
      for (const country of countries) {
        const exists = await this.countryRepository.findOne({
          where: { name: country },
        })

        if (!exists) {
          const newCountry = this.countryRepository.create({ name: country })
          await this.countryRepository.save(newCountry)
          console.log(`Country ${country} seeded successfully!`)
        } else {
          console.log(`Country ${country} already exists, skipping...`)
        }
      }
    } catch (error) {
      console.error("Error seeding countries:", error)
    }
  }
}
