import { DataSource } from 'typeorm';

// --- MONSTRES PAR ZONE ---

const MONSTERS = [
  // Marais (existants — juste ajouter zone)
  { name: 'Têtard Vase',        zone: 'marais' },
  { name: 'Grenouille Boueuse', zone: 'marais' },
  { name: 'Serpent des Marais',  zone: 'marais' },
  { name: 'Champi Vénéneux',    zone: 'marais' },
  { name: 'Golem de Boue',      zone: 'marais' },

  // Égouts (nouveau)
  { name: 'Rat d\'Égout',     zone: 'egouts', minLevel: 4, maxLevel: 6, hp: 55, attack: 9, defense: 2, attackType: 'melee',  xpReward: 18, goldMin: 8, goldMax: 18 },
  { name: 'Slime Toxique',    zone: 'egouts', minLevel: 5, maxLevel: 7, hp: 70, attack: 7, defense: 4, attackType: 'magic',  xpReward: 22, goldMin: 10, goldMax: 22 },
  { name: 'Araignée Géante',  zone: 'egouts', minLevel: 5, maxLevel: 8, hp: 85, attack: 12, defense: 3, attackType: 'ranged', xpReward: 28, goldMin: 12, goldMax: 28 },
  { name: 'Crocodile',        zone: 'egouts', minLevel: 6, maxLevel: 9, hp: 120, attack: 15, defense: 5, attackType: 'melee',  xpReward: 38, goldMin: 18, goldMax: 40 },
  { name: 'Roi des Rats',     zone: 'egouts', minLevel: 7, maxLevel: 10, hp: 180, attack: 18, defense: 7, attackType: 'melee',  xpReward: 60, goldMin: 30, goldMax: 70 },

  // Désert (nouveau)
  { name: 'Scorpion',         zone: 'desert', minLevel: 8, maxLevel: 11, hp: 100, attack: 14, defense: 6, attackType: 'melee',  xpReward: 35, goldMin: 15, goldMax: 35 },
  { name: 'Vautour',          zone: 'desert', minLevel: 9, maxLevel: 12, hp: 80,  attack: 16, defense: 3, attackType: 'ranged', xpReward: 40, goldMin: 18, goldMax: 40 },
  { name: 'Momie',            zone: 'desert', minLevel: 10, maxLevel: 13, hp: 150, attack: 13, defense: 8, attackType: 'magic',  xpReward: 48, goldMin: 22, goldMax: 50 },
  { name: 'Ver des Sables',   zone: 'desert', minLevel: 11, maxLevel: 14, hp: 200, attack: 20, defense: 6, attackType: 'melee',  xpReward: 55, goldMin: 30, goldMax: 65 },
  { name: 'Sphinx',           zone: 'desert', minLevel: 12, maxLevel: 15, hp: 280, attack: 24, defense: 10, attackType: 'magic',  xpReward: 80, goldMin: 50, goldMax: 120 },
];

// --- ITEMS BOUTIQUE ---

const ITEMS = [
  // Marais — starter
  { name: 'Épée rouillée',     type: 'weapon', rarity: 'common', attackBonus: 3, defenseBonus: 0, buyPrice: 30,  minLevel: 1, zone: 'marais', description: 'Une lame usée mais encore tranchante.' },
  { name: 'Bouclier de bois',  type: 'armor',  rarity: 'common', attackBonus: 0, defenseBonus: 2, buyPrice: 25,  minLevel: 1, zone: 'marais', description: 'Quelques planches assemblées à la va-vite.' },
  { name: 'Lame des marais',   type: 'weapon', rarity: 'common', attackBonus: 6, defenseBonus: 0, buyPrice: 80,  minLevel: 3, zone: 'marais', description: 'Forgée dans la boue des marais, étrangement solide.' },
  { name: 'Cotte de mailles',  type: 'armor',  rarity: 'common', attackBonus: 0, defenseBonus: 5, buyPrice: 70,  minLevel: 3, zone: 'marais', description: 'Protection basique mais fiable.' },

  // Égouts — mid game
  { name: 'Dague empoisonnée',   type: 'weapon', rarity: 'rare', attackBonus: 9,  defenseBonus: 0, buyPrice: 200, minLevel: 5, zone: 'egouts', description: 'La lame suinte d\'un liquide verdâtre.' },
  { name: 'Armure de cuir clouté', type: 'armor', rarity: 'rare', attackBonus: 0, defenseBonus: 7, buyPrice: 180, minLevel: 5, zone: 'egouts', description: 'Renforcée avec des clous récupérés dans les égouts.' },
  { name: 'Épée de guerre',     type: 'weapon', rarity: 'rare', attackBonus: 13, defenseBonus: 0, buyPrice: 400, minLevel: 7, zone: 'egouts', description: 'Une lame imposante qui fait trembler les rats.' },
  { name: 'Armure de fer',      type: 'armor',  rarity: 'rare', attackBonus: 0, defenseBonus: 10, buyPrice: 350, minLevel: 7, zone: 'egouts', description: 'Lourde mais résistante.' },

  // Désert — late game
  { name: 'Cimeterre du désert', type: 'weapon', rarity: 'epic', attackBonus: 18, defenseBonus: 0, buyPrice: 800,  minLevel: 10, zone: 'desert', description: 'Lame courbe forgée dans le sable brûlant.' },
  { name: 'Armure de sable',    type: 'armor',  rarity: 'epic', attackBonus: 0, defenseBonus: 14, buyPrice: 700,  minLevel: 10, zone: 'desert', description: 'Enchantée pour dévier les coups du vent.' },
  { name: 'Lame du Sphinx',     type: 'weapon', rarity: 'epic', attackBonus: 25, defenseBonus: 0, buyPrice: 1500, minLevel: 13, zone: 'desert', description: 'Seuls les plus dignes peuvent la manier.' },
  { name: 'Armure du Pharaon',  type: 'armor',  rarity: 'epic', attackBonus: 0, defenseBonus: 18, buyPrice: 1300, minLevel: 13, zone: 'desert', description: 'Dorée et ancienne, elle irradie de puissance.' },

  // Potions — consommables
  // HP potions: forceBonus = 0 → heal 50% HP
  { name: 'Potion de soin',       type: 'consumable', rarity: 'common', attackBonus: 0, defenseBonus: 0, forceBonus: 0, buyPrice: 15, minLevel: 1, zone: null, description: 'Restaure 50% des PV.' },
  // Endurance potions: forceBonus > 0 → restore N endurance
  { name: 'Potion d\'énergie',          type: 'consumable', rarity: 'common', attackBonus: 0, defenseBonus: 0, forceBonus: 30, buyPrice: 20, minLevel: 1, zone: null, description: 'Restaure 30 points d\'endurance.' },
  { name: 'Grande potion d\'énergie',   type: 'consumable', rarity: 'rare',   attackBonus: 0, defenseBonus: 0, forceBonus: 60, buyPrice: 45, minLevel: 3, zone: null, description: 'Restaure 60 points d\'endurance.' },
];

export async function seedZones(dataSource: DataSource) {
  // Update existing monsters with zone
  for (const m of MONSTERS) {
    if (!('hp' in m)) {
      // Existing monster — just set zone
      await dataSource.query('UPDATE monsters SET zone = ? WHERE name = ?', [m.zone, m.name]);
    } else {
      // New monster
      const existing = await dataSource.query('SELECT id FROM monsters WHERE name = ?', [m.name]);
      if (existing.length === 0) {
        await dataSource.query(
          'INSERT INTO monsters (id, name, zone, min_level, max_level, hp, attack, defense, attack_type, xp_reward, gold_min, gold_max) VALUES (UUID(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
          [m.name, m.zone, (m as any).minLevel, (m as any).maxLevel, (m as any).hp, (m as any).attack, (m as any).defense, (m as any).attackType, (m as any).xpReward, (m as any).goldMin, (m as any).goldMax],
        );
        console.log(`+ ${m.name} (${m.zone})`);
      }
    }
  }

  // Seed items
  const itemRepo = dataSource.getRepository('Item');
  for (const item of ITEMS) {
    const existing = await itemRepo.findOne({ where: { name: item.name } });
    if (!existing) {
      await itemRepo.save(itemRepo.create(item));
      console.log(`+ ${item.name} (${item.type}, ${item.buyPrice}💰)`);
    }
  }

  console.log(`✅ ${MONSTERS.filter(m => 'hp' in m).length} monstres + ${ITEMS.length} items seedés`);
}
