import 'reflect-metadata';
import { DataSource } from 'typeorm';
import { LevelThreshold } from '../character/entities/level-threshold.entity';

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

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

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

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

  // XP requis = 100 × level^1.5
  // Level 1  : 100 XP
  // Level 10 : 3162 XP
  // Level 100: 1 000 000 XP
  const thresholds: LevelThreshold[] = Array.from({ length: 100 }, (_, i) => {
    const level = i + 1;
    const threshold = new LevelThreshold();
    threshold.level = level;
    threshold.xpRequired = Math.round(100 * Math.pow(level, 1.5));
    return threshold;
  });

  await repo.save(thresholds);
  console.log('✅ 100 level_thresholds seedés');

  await dataSource.destroy();
}

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