/**
 * Seed script for server_config table.
 * Uses known server identifiers from Chronicler #87 audit.
 * No Pterodactyl API call needed — identifiers are hardcoded.
 *
 * Usage: node migrations/139_seed_server_config.js
 */
require('dotenv').config({ path: require('path').resolve(__dirname, '../.env') });
const { Pool } = require('pg');

const pool = new Pool({
  user: process.env.DB_USER,
  host: process.env.DB_HOST,
  database: process.env.DB_NAME,
  password: process.env.DB_PASSWORD,
  port: process.env.DB_PORT || 5432
});

// Full server data — identifiers, subdomains, IPs, ports from Chronicler #87 audit
const SERVER_MAP = [
  { id: 'f408e832', name: 'All the Mods 10: To the Sky', short_name: 'atm10-tts', subdomain: 'atm10tts', ip: '216.239.104.130', port: 25565, node: 'NC1' },
  { id: 'c4bc5892', name: 'All the Mons', short_name: 'all-the-mons', subdomain: 'atmons', ip: '216.239.104.130', port: 25566, node: 'NC1' },
  { id: 'b90ced3c', name: 'Mythcraft 5', short_name: 'mythcraft-5', subdomain: 'mythcraft5', ip: '216.239.104.130', port: 25567, node: 'NC1' },
  { id: 'e1c6ff8d', name: 'All of Create (Creative)', short_name: 'all-of-create', subdomain: 'aocc', ip: '216.239.104.130', port: 25568, node: 'NC1' },
  { id: '82e63949', name: 'All The Mods 10', short_name: 'atm10', subdomain: 'atm10', ip: '216.239.104.130', port: 25569, node: 'NC1' },
  { id: 'd4790f45', name: 'Otherworld [Dungeons & Dragons]', short_name: 'otherworld', subdomain: 'otherworld', ip: '216.239.104.130', port: 25570, node: 'NC1' },
  { id: '8950fa1e', name: 'DeceasedCraft', short_name: 'deceasedcraft', subdomain: 'deceasedcraft', ip: '216.239.104.130', port: 25571, node: 'NC1' },
  { id: '7c9c2dc0', name: "Sneak's Pirate Pack", short_name: 'sneaks-pirate-pack', subdomain: 'sneakspiratpack', ip: '216.239.104.130', port: 25572, node: 'NC1' },
  { id: '25b23f6e', name: 'Farm Crossing 6', short_name: 'farm-crossing-6', subdomain: 'farmcrossing6', ip: '216.239.104.130', port: 25573, node: 'NC1' },
  { id: 'f5befeab', name: 'Homestead - A Cozy Survival Experience', short_name: 'homestead', subdomain: 'homestead', ip: '216.239.104.130', port: 25574, node: 'NC1' },
  { id: 'a0efbfe8', name: 'Stoneblock 4', short_name: 'stoneblock-4', subdomain: 'stoneblock4', ip: '38.68.14.26', port: 25565, node: 'TX1' },
  { id: '9310d0a6', name: 'Society: Sunlit Valley', short_name: 'society-sunlit-valley', subdomain: 'society', ip: '38.68.14.28', port: 25565, node: 'TX1' },
  { id: '576342b8', name: 'Submerged 2', short_name: 'submerged-2', subdomain: 'submerged2', ip: '38.68.14.26', port: 25571, node: 'TX1' },
  { id: 'e95ed4a8', name: 'Beyond Depth', short_name: 'beyond-depth', subdomain: 'beyonddepth', ip: '38.68.14.26', port: 25568, node: 'TX1' },
  { id: '3f842757', name: 'Beyond Ascension', short_name: 'beyond-ascension', subdomain: 'beyondascension', ip: '38.68.14.26', port: 25569, node: 'TX1' },
  { id: '7a9754ad', name: 'Cottage Witch', short_name: 'cottage-witch', subdomain: 'cottagewitch', ip: '38.68.14.26', port: 25572, node: 'TX1' },
  { id: '668a5220', name: 'All The Mons (Private) - TX', short_name: 'all-the-mons-private', subdomain: 'allthemons', ip: '38.68.14.30', port: 25565, node: 'TX1' },
  { id: 'fcbe0a1d', name: "Wold's Vaults", short_name: 'wolds-vaults', subdomain: 'vaults', ip: '38.68.14.26', port: 25570, node: 'TX1' }
];

async function seed() {
  console.log('Seeding server_config with %d servers...', SERVER_MAP.length);

  let count = 0;
  for (const s of SERVER_MAP) {
    await pool.query(`
      INSERT INTO server_config
        (server_identifier, short_name, short_name_locked, display_name, pterodactyl_name, subdomain, server_ip, server_port, node)
      VALUES ($1, $2, true, $3, $3, $4, $5, $6, $7)
      ON CONFLICT (server_identifier) DO UPDATE SET
        short_name = EXCLUDED.short_name,
        short_name_locked = true,
        display_name = EXCLUDED.display_name,
        pterodactyl_name = EXCLUDED.pterodactyl_name,
        subdomain = EXCLUDED.subdomain,
        server_ip = EXCLUDED.server_ip,
        server_port = EXCLUDED.server_port,
        node = EXCLUDED.node,
        updated_at = NOW()
    `, [s.id, s.short_name, s.name, s.subdomain, s.ip, s.port, s.node]);

    console.log('  OK: %s → %s (%s.firefrostgaming.com)', s.name, s.short_name, s.subdomain);
    count++;
  }

  console.log('\nDone. Seeded %d servers.', count);
  await pool.end();
}

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