import 'reflect-metadata';
import { DataSource } from 'typeorm';
import { Monster } from '../monster/monster.entity';

const dataSource = new DataSource({
  type: 'postgres',
  url: process.env.DATABASE_URL ?? 'postgresql://tetardpg:password@localhost:5432/tetardpg',
  entities: [Monster],
  synchronize: false,
});

const MONSTERS = [
  {
    name: 'Têtard Vase',
    minLevel: 1, maxLevel: 2,
    hp: 40, attack: 5, defense: 0,
    attackType: 'melee' as const,
    xpReward: 25, goldMin: 3, goldMax: 8,
  },
  {
    name: 'Grenouille Boueuse',
    minLevel: 2, maxLevel: 4,
    hp: 65, attack: 8, defense: 1,
    attackType: 'melee' as const,
    xpReward: 45, goldMin: 6, goldMax: 15,
  },
  {
    name: 'Serpent des Marais',
    minLevel: 3, maxLevel: 6,
    hp: 90, attack: 11, defense: 2,
    attackType: 'ranged' as const,
    xpReward: 70, goldMin: 10, goldMax: 25,
  },
  {
    name: 'Champi Vénéneux',
    minLevel: 2, maxLevel: 5,
    hp: 75, attack: 9, defense: 3,
    attackType: 'magic' as const,
    xpReward: 60, goldMin: 8, goldMax: 20,
  },
  {
    name: 'Golem de Boue',
    minLevel: 6, maxLevel: 9,
    hp: 150, attack: 16, defense: 5,
    attackType: 'melee' as const,
    xpReward: 130, goldMin: 25, goldMax: 60,
  },
];

async function seed() {
  await dataSource.initialize();
  console.log('DB connectée');

  const repo = dataSource.getRepository(Monster);
  const existing = await repo.count();

  if (existing >= MONSTERS.length) {
    console.log('Monstres déjà seedés — skip');
    await dataSource.destroy();
    return;
  }

  for (const data of MONSTERS) {
    const exists = await repo.findOne({ where: { name: data.name } });
    if (!exists) {
      await repo.save(repo.create(data));
      console.log(`✅ ${data.name} seedé`);
    }
  }

  console.log('✅ Seed monstres terminé');
  await dataSource.destroy();
}

seed().catch((err) => {
  console.error('Seed monstres échoué :', err);
  process.exit(1);
});
