-- Seed: central ports + NPC faction leaders (one per continent).
-- Run ONCE, after migration 004_central_dispatch.sql, against skyrama_clone.
-- Idempotent: every statement is guarded by WHERE NOT EXISTS, so re-running is a
-- no-op. Order per continent: product -> hub country -> NPC user -> central
-- airport -> faction. (Replaces the old Go Client.EnsureStage0World.)

-- Placeholder product so hub countries have a produce FK to point at.
INSERT INTO products (name) SELECT 'Misc Cargo'
  WHERE NOT EXISTS (SELECT 1 FROM products);

-- ─── void → Orbital Concord / Cmdr. Vega Solaris ───────────────────────────────
INSERT INTO countries (name,continent,produce_1_id,produce_2_id,produce_3_id)
  SELECT 'void Hub','void', p.id, p.id, p.id
  FROM (SELECT id FROM products ORDER BY id LIMIT 1) p
  WHERE NOT EXISTS (SELECT 1 FROM countries WHERE continent='void');
INSERT INTO users (username,password_hash,is_npc)
  SELECT 'vega_solaris','',TRUE
  WHERE NOT EXISTS (SELECT 1 FROM users WHERE username='vega_solaris');
INSERT INTO airports (name,user_id,origin_country_id,is_central)
  SELECT 'Orbital Spaceport', u.id, c.id, TRUE
  FROM (SELECT id FROM users WHERE username='vega_solaris') u,
       (SELECT id FROM countries WHERE continent='void' ORDER BY id LIMIT 1) c
  WHERE NOT EXISTS (SELECT 1 FROM airports WHERE user_id=(SELECT id FROM users WHERE username='vega_solaris'));
INSERT INTO factions (name,continent,leader_name,background,user_id,central_airport_id)
  SELECT 'Orbital Concord','void','Cmdr. Vega Solaris',
         'Runs the only spaceport beyond the sky and keeps the orbital lanes clear for every flag.',
         u.id, a.id
  FROM (SELECT id FROM users WHERE username='vega_solaris') u,
       (SELECT id FROM airports WHERE user_id=(SELECT id FROM users WHERE username='vega_solaris')) a
  WHERE NOT EXISTS (SELECT 1 FROM factions WHERE continent='void');

-- ─── Africa → Savannah Pact / Amara Okonkwo ────────────────────────────────────
INSERT INTO countries (name,continent,produce_1_id,produce_2_id,produce_3_id)
  SELECT 'Africa Hub','Africa', p.id, p.id, p.id
  FROM (SELECT id FROM products ORDER BY id LIMIT 1) p
  WHERE NOT EXISTS (SELECT 1 FROM countries WHERE continent='Africa');
INSERT INTO users (username,password_hash,is_npc)
  SELECT 'amara_okonkwo','',TRUE
  WHERE NOT EXISTS (SELECT 1 FROM users WHERE username='amara_okonkwo');
INSERT INTO airports (name,user_id,origin_country_id,is_central)
  SELECT 'Africa Central', u.id, c.id, TRUE
  FROM (SELECT id FROM users WHERE username='amara_okonkwo') u,
       (SELECT id FROM countries WHERE continent='Africa' ORDER BY id LIMIT 1) c
  WHERE NOT EXISTS (SELECT 1 FROM airports WHERE user_id=(SELECT id FROM users WHERE username='amara_okonkwo'));
INSERT INTO factions (name,continent,leader_name,background,user_id,central_airport_id)
  SELECT 'Savannah Pact','Africa','Amara Okonkwo',
         'Unites the continent''s scattered bush-strip airfields under a single banner.',
         u.id, a.id
  FROM (SELECT id FROM users WHERE username='amara_okonkwo') u,
       (SELECT id FROM airports WHERE user_id=(SELECT id FROM users WHERE username='amara_okonkwo')) a
  WHERE NOT EXISTS (SELECT 1 FROM factions WHERE continent='Africa');

-- ─── Antarctica → Polar Frontier / Dr. Erik Frost ──────────────────────────────
INSERT INTO countries (name,continent,produce_1_id,produce_2_id,produce_3_id)
  SELECT 'Antarctica Hub','Antarctica', p.id, p.id, p.id
  FROM (SELECT id FROM products ORDER BY id LIMIT 1) p
  WHERE NOT EXISTS (SELECT 1 FROM countries WHERE continent='Antarctica');
INSERT INTO users (username,password_hash,is_npc)
  SELECT 'erik_frost','',TRUE
  WHERE NOT EXISTS (SELECT 1 FROM users WHERE username='erik_frost');
INSERT INTO airports (name,user_id,origin_country_id,is_central)
  SELECT 'Antarctica Central', u.id, c.id, TRUE
  FROM (SELECT id FROM users WHERE username='erik_frost') u,
       (SELECT id FROM countries WHERE continent='Antarctica' ORDER BY id LIMIT 1) c
  WHERE NOT EXISTS (SELECT 1 FROM airports WHERE user_id=(SELECT id FROM users WHERE username='erik_frost'));
INSERT INTO factions (name,continent,leader_name,background,user_id,central_airport_id)
  SELECT 'Polar Frontier','Antarctica','Dr. Erik Frost',
         'Keeps the ice runways open against the harshest weather on Earth.',
         u.id, a.id
  FROM (SELECT id FROM users WHERE username='erik_frost') u,
       (SELECT id FROM airports WHERE user_id=(SELECT id FROM users WHERE username='erik_frost')) a
  WHERE NOT EXISTS (SELECT 1 FROM factions WHERE continent='Antarctica');

-- ─── Asia → Monsoon Alliance / Mei-Lin Zhao ────────────────────────────────────
INSERT INTO countries (name,continent,produce_1_id,produce_2_id,produce_3_id)
  SELECT 'Asia Hub','Asia', p.id, p.id, p.id
  FROM (SELECT id FROM products ORDER BY id LIMIT 1) p
  WHERE NOT EXISTS (SELECT 1 FROM countries WHERE continent='Asia');
INSERT INTO users (username,password_hash,is_npc)
  SELECT 'meilin_zhao','',TRUE
  WHERE NOT EXISTS (SELECT 1 FROM users WHERE username='meilin_zhao');
INSERT INTO airports (name,user_id,origin_country_id,is_central)
  SELECT 'Asia Central', u.id, c.id, TRUE
  FROM (SELECT id FROM users WHERE username='meilin_zhao') u,
       (SELECT id FROM countries WHERE continent='Asia' ORDER BY id LIMIT 1) c
  WHERE NOT EXISTS (SELECT 1 FROM airports WHERE user_id=(SELECT id FROM users WHERE username='meilin_zhao'));
INSERT INTO factions (name,continent,leader_name,background,user_id,central_airport_id)
  SELECT 'Monsoon Alliance','Asia','Mei-Lin Zhao',
         'Commands the busiest and most fiercely contested skies on the planet.',
         u.id, a.id
  FROM (SELECT id FROM users WHERE username='meilin_zhao') u,
       (SELECT id FROM airports WHERE user_id=(SELECT id FROM users WHERE username='meilin_zhao')) a
  WHERE NOT EXISTS (SELECT 1 FROM factions WHERE continent='Asia');

-- ─── Europe → Continental Union / Lars Vandenberg ──────────────────────────────
INSERT INTO countries (name,continent,produce_1_id,produce_2_id,produce_3_id)
  SELECT 'Europe Hub','Europe', p.id, p.id, p.id
  FROM (SELECT id FROM products ORDER BY id LIMIT 1) p
  WHERE NOT EXISTS (SELECT 1 FROM countries WHERE continent='Europe');
INSERT INTO users (username,password_hash,is_npc)
  SELECT 'lars_vandenberg','',TRUE
  WHERE NOT EXISTS (SELECT 1 FROM users WHERE username='lars_vandenberg');
INSERT INTO airports (name,user_id,origin_country_id,is_central)
  SELECT 'Europe Central', u.id, c.id, TRUE
  FROM (SELECT id FROM users WHERE username='lars_vandenberg') u,
       (SELECT id FROM countries WHERE continent='Europe' ORDER BY id LIMIT 1) c
  WHERE NOT EXISTS (SELECT 1 FROM airports WHERE user_id=(SELECT id FROM users WHERE username='lars_vandenberg'));
INSERT INTO factions (name,continent,leader_name,background,user_id,central_airport_id)
  SELECT 'Continental Union','Europe','Lars Vandenberg',
         'Old-money hub baron who controls the western air corridors.',
         u.id, a.id
  FROM (SELECT id FROM users WHERE username='lars_vandenberg') u,
       (SELECT id FROM airports WHERE user_id=(SELECT id FROM users WHERE username='lars_vandenberg')) a
  WHERE NOT EXISTS (SELECT 1 FROM factions WHERE continent='Europe');

-- ─── North America → Liberty Air Coalition / Jack Sullivan ─────────────────────
INSERT INTO countries (name,continent,produce_1_id,produce_2_id,produce_3_id)
  SELECT 'North America Hub','North America', p.id, p.id, p.id
  FROM (SELECT id FROM products ORDER BY id LIMIT 1) p
  WHERE NOT EXISTS (SELECT 1 FROM countries WHERE continent='North America');
INSERT INTO users (username,password_hash,is_npc)
  SELECT 'jack_sullivan','',TRUE
  WHERE NOT EXISTS (SELECT 1 FROM users WHERE username='jack_sullivan');
INSERT INTO airports (name,user_id,origin_country_id,is_central)
  SELECT 'North America Central', u.id, c.id, TRUE
  FROM (SELECT id FROM users WHERE username='jack_sullivan') u,
       (SELECT id FROM countries WHERE continent='North America' ORDER BY id LIMIT 1) c
  WHERE NOT EXISTS (SELECT 1 FROM airports WHERE user_id=(SELECT id FROM users WHERE username='jack_sullivan'));
INSERT INTO factions (name,continent,leader_name,background,user_id,central_airport_id)
  SELECT 'Liberty Air Coalition','North America','Jack Sullivan',
         'Brash operator of the great transcontinental gateways.',
         u.id, a.id
  FROM (SELECT id FROM users WHERE username='jack_sullivan') u,
       (SELECT id FROM airports WHERE user_id=(SELECT id FROM users WHERE username='jack_sullivan')) a
  WHERE NOT EXISTS (SELECT 1 FROM factions WHERE continent='North America');

-- ─── Oceania → Coral Compass / Talia Reef ──────────────────────────────────────
INSERT INTO countries (name,continent,produce_1_id,produce_2_id,produce_3_id)
  SELECT 'Oceania Hub','Oceania', p.id, p.id, p.id
  FROM (SELECT id FROM products ORDER BY id LIMIT 1) p
  WHERE NOT EXISTS (SELECT 1 FROM countries WHERE continent='Oceania');
INSERT INTO users (username,password_hash,is_npc)
  SELECT 'talia_reef','',TRUE
  WHERE NOT EXISTS (SELECT 1 FROM users WHERE username='talia_reef');
INSERT INTO airports (name,user_id,origin_country_id,is_central)
  SELECT 'Oceania Central', u.id, c.id, TRUE
  FROM (SELECT id FROM users WHERE username='talia_reef') u,
       (SELECT id FROM countries WHERE continent='Oceania' ORDER BY id LIMIT 1) c
  WHERE NOT EXISTS (SELECT 1 FROM airports WHERE user_id=(SELECT id FROM users WHERE username='talia_reef'));
INSERT INTO factions (name,continent,leader_name,background,user_id,central_airport_id)
  SELECT 'Coral Compass','Oceania','Talia Reef',
         'Island-hopping seaplane magnate of the southern seas.',
         u.id, a.id
  FROM (SELECT id FROM users WHERE username='talia_reef') u,
       (SELECT id FROM airports WHERE user_id=(SELECT id FROM users WHERE username='talia_reef')) a
  WHERE NOT EXISTS (SELECT 1 FROM factions WHERE continent='Oceania');

-- ─── South America → Andean Wings / Mateo Rivera ───────────────────────────────
INSERT INTO countries (name,continent,produce_1_id,produce_2_id,produce_3_id)
  SELECT 'South America Hub','South America', p.id, p.id, p.id
  FROM (SELECT id FROM products ORDER BY id LIMIT 1) p
  WHERE NOT EXISTS (SELECT 1 FROM countries WHERE continent='South America');
INSERT INTO users (username,password_hash,is_npc)
  SELECT 'mateo_rivera','',TRUE
  WHERE NOT EXISTS (SELECT 1 FROM users WHERE username='mateo_rivera');
INSERT INTO airports (name,user_id,origin_country_id,is_central)
  SELECT 'South America Central', u.id, c.id, TRUE
  FROM (SELECT id FROM users WHERE username='mateo_rivera') u,
       (SELECT id FROM countries WHERE continent='South America' ORDER BY id LIMIT 1) c
  WHERE NOT EXISTS (SELECT 1 FROM airports WHERE user_id=(SELECT id FROM users WHERE username='mateo_rivera'));
INSERT INTO factions (name,continent,leader_name,background,user_id,central_airport_id)
  SELECT 'Andean Wings','South America','Mateo Rivera',
         'Flies the high mountain passes that nobody else dares.',
         u.id, a.id
  FROM (SELECT id FROM users WHERE username='mateo_rivera') u,
       (SELECT id FROM airports WHERE user_id=(SELECT id FROM users WHERE username='mateo_rivera')) a
  WHERE NOT EXISTS (SELECT 1 FROM factions WHERE continent='South America');
