import { Pool } from 'pg';
import { PrismaPg } from '@prisma/adapter-pg';
import { PrismaClient } from '@prisma/client';
import 'dotenv/config';

const pool = new Pool({ connectionString: process.env.DATABASE_URL });
const adapter = new PrismaPg(pool);
const prisma = new PrismaClient({ adapter });

async function main() {
  console.log('🌱 Start seeding refinement balance data...');

  // 1. Refinement System Config (Global)
  const systemConfigs = [
    { key: 'MAX_LEVEL', value: '25', description: 'Maximum weapon level' },
    { key: 'START_GOLD', value: '1000', description: 'Initial gold for new players' },
    { key: 'CHECKIN_GOLD', value: '500', description: 'Gold awarded for daily check-in' },
    { key: 'DAILY_BATTLE_LIMIT', value: '10', description: 'Maximum battles per day' },
    { key: 'INVERSE_REWARD_MULTIPLIER', value: '2.0', description: 'Base multiplier for battle rewards' },
    { key: 'REWARD_RANDOM_MIN', value: '0.8', description: 'Minimum random factor for rewards' },
    { key: 'REWARD_RANDOM_MAX', value: '1.2', description: 'Maximum random factor for rewards' },
  ];

  for (const config of systemConfigs) {
    await prisma.refinementSystemConfig.upsert({
      where: { key: config.key },
      update: config,
      create: config,
    });
  }

  // 2. Refinement Level Config (0-25)
  const levelConfigs = [];
  for (let l = 0; l <= 25; l++) {
    let successRate = 0;
    if (l === 0) successRate = 1.0;
    else if (l === 1) successRate = 0.98;
    else if (l === 2) successRate = 0.95;
    else if (l === 3) successRate = 0.92;
    else if (l === 4) successRate = 0.90;
    else if (l === 5) successRate = 0.05;
    else if (l === 6) successRate = 0.04;
    else if (l === 7) successRate = 0.03;
    else if (l === 8) successRate = 0.02;
    else if (l < 10) successRate = 0.01;
    else if (l < 20) successRate = 0.001;
    else successRate = 0.0001;

    const cost = Math.floor(10 * Math.pow(1.6, l));
    const destroyRate = l * 0.015;
    const sellMultiplier = l < 5 ? 2.0 : 2 + (l - 4) * 10;

    levelConfigs.push({
      level: l,
      successRate,
      destroyRate,
      cost,
      sellMultiplier,
    });
  }

  for (const config of levelConfigs) {
    await prisma.refinementLevelConfig.upsert({
      where: { level: config.level },
      update: config,
      create: config,
    });
  }

  // 3. Refinement Battle Config (Gap)
  const battleConfigs = [];
  
  for (let g = 0; g <= 25; g++) {
    battleConfigs.push({ gap: g, winRate: Math.min(0.99, 0.5 + g * 0.05) });
  }

  const negativeGaps = [
    { gap: -1, rate: 0.4 },
    { gap: -2, rate: 0.3 },
    { gap: -3, rate: 0.2 },
    { gap: -4, rate: 0.1 },
    { gap: -5, rate: 0.05 },
    { gap: -6, rate: 0.04 },
    { gap: -7, rate: 0.03 },
    { gap: -8, rate: 0.02 },
    { gap: -9, rate: 0.01 },
  ];

  for (const n of negativeGaps) {
    battleConfigs.push({ gap: n.gap, winRate: n.rate });
  }

  for (let g = -10; g >= -25; g--) {
    battleConfigs.push({ gap: g, winRate: 0 });
  }

  for (const config of battleConfigs) {
    await prisma.refinementBattleConfig.upsert({
      where: { levelGap: config.gap },
      update: { winRate: config.winRate },
      create: { levelGap: config.gap, winRate: config.winRate },
    });
  }

  console.log('✅ Seeding completed!');
}

main()
  .catch((e) => {
    console.error(e);
    process.exit(1);
  })
  .finally(async () => {
    await prisma.$disconnect();
    await pool.end();
  });
