-- Seed regions + games + region_game_mappings from CSVs
-- Auto-generated from data/csvs/regions.csv and data/csvs/games.csv

-- Regions
INSERT INTO regions (name) VALUES
  ('Kanto'),
  ('Johto'),
  ('Hoenn'),
  ('Sinnoh'),
  ('Unova'),
  ('Galar'),
  ('Alola'),
  ('Kalos'),
  ('Hisui'),
  ('Paldea'),
  ('Go'),
  ('Home'),
  ('Orre'),
  ('Ranch')
ON CONFLICT (name) DO UPDATE SET
  "updatedAt" = NOW();

-- Games
INSERT INTO games (id, "displayName", region, generation, "releaseYear", "regionId") VALUES
  ('red', 'Red', 'Kanto', 1, 1996, (SELECT id FROM regions WHERE name = 'Kanto')),
  ('blue', 'Blue', 'Kanto', 1, 1996, (SELECT id FROM regions WHERE name = 'Kanto')),
  ('yellow', 'Yellow', 'Kanto', 1, 1998, (SELECT id FROM regions WHERE name = 'Kanto')),
  ('gold', 'Gold', 'Johto', 2, 1999, (SELECT id FROM regions WHERE name = 'Johto')),
  ('silver', 'Silver', 'Johto', 2, 1999, (SELECT id FROM regions WHERE name = 'Johto')),
  ('crystal', 'Crystal', 'Johto', 2, 2000, (SELECT id FROM regions WHERE name = 'Johto')),
  ('ruby', 'Ruby', 'Hoenn', 3, 2002, (SELECT id FROM regions WHERE name = 'Hoenn')),
  ('sapphire', 'Sapphire', 'Hoenn', 3, 2002, (SELECT id FROM regions WHERE name = 'Hoenn')),
  ('colosseum', 'Colosseum', 'Orre', 3, 2003, (SELECT id FROM regions WHERE name = 'Orre')),
  ('emerald', 'Emerald', 'Hoenn', 3, 2004, (SELECT id FROM regions WHERE name = 'Hoenn')),
  ('firered', 'FireRed', 'Kanto', 1, 2004, (SELECT id FROM regions WHERE name = 'Kanto')),
  ('leafgreen', 'LeafGreen', 'Kanto', 1, 2004, (SELECT id FROM regions WHERE name = 'Kanto')),
  ('xd-gale-of-darkness', 'XD: Gale of Darkness', 'Orre', 3, 2005, (SELECT id FROM regions WHERE name = 'Orre')),
  ('diamond', 'Diamond', 'Sinnoh', 4, 2006, (SELECT id FROM regions WHERE name = 'Sinnoh')),
  ('pearl', 'Pearl', 'Sinnoh', 4, 2006, (SELECT id FROM regions WHERE name = 'Sinnoh')),
  ('platinum', 'Platinum', 'Sinnoh', 4, 2008, (SELECT id FROM regions WHERE name = 'Sinnoh')),
  ('ranch', 'Ranch', 'Ranch', 4, 2008, (SELECT id FROM regions WHERE name = 'Ranch')),
  ('heart-gold', 'Heart Gold', 'Johto', 2, 2009, (SELECT id FROM regions WHERE name = 'Johto')),
  ('soul-silver', 'Soul Silver', 'Johto', 2, 2009, (SELECT id FROM regions WHERE name = 'Johto')),
  ('black', 'Black', 'Unova', 5, 2010, (SELECT id FROM regions WHERE name = 'Unova')),
  ('white', 'White', 'Unova', 5, 2010, (SELECT id FROM regions WHERE name = 'Unova')),
  ('black-2', 'Black 2', 'Unova', 5, 2012, (SELECT id FROM regions WHERE name = 'Unova')),
  ('white-2', 'White 2', 'Unova', 5, 2012, (SELECT id FROM regions WHERE name = 'Unova')),
  ('dream-radar', 'Dream Radar', 'Unova', 5, 2012, (SELECT id FROM regions WHERE name = 'Unova')),
  ('x', 'X', 'Kalos', 6, 2013, (SELECT id FROM regions WHERE name = 'Kalos')),
  ('y', 'Y', 'Kalos', 6, 2013, (SELECT id FROM regions WHERE name = 'Kalos')),
  ('omega-ruby', 'Omega Ruby', 'Hoenn', 3, 2014, (SELECT id FROM regions WHERE name = 'Hoenn')),
  ('alpha-sapphire', 'Alpha Sapphire', 'Hoenn', 3, 2014, (SELECT id FROM regions WHERE name = 'Hoenn')),
  ('sun', 'Sun', 'Alola', 7, 2016, (SELECT id FROM regions WHERE name = 'Alola')),
  ('moon', 'Moon', 'Alola', 7, 2016, (SELECT id FROM regions WHERE name = 'Alola')),
  ('moon-(demo)', 'Moon (demo)', 'Alola', 7, 2016, (SELECT id FROM regions WHERE name = 'Alola')),
  ('go', 'Go', 'Go', 7, 2016, (SELECT id FROM regions WHERE name = 'Go')),
  ('ultra-sun', 'Ultra Sun', 'Alola', 7, 2017, (SELECT id FROM regions WHERE name = 'Alola')),
  ('ultra-moon', 'Ultra Moon', 'Alola', 7, 2017, (SELECT id FROM regions WHERE name = 'Alola')),
  ('lg-pikachu', 'LG: Pikachu', 'Kanto', 1, 2018, (SELECT id FROM regions WHERE name = 'Kanto')),
  ('lg-eevee', 'LG: Eevee', 'Kanto', 1, 2018, (SELECT id FROM regions WHERE name = 'Kanto')),
  ('sword', 'Sword', 'Galar', 8, 2019, (SELECT id FROM regions WHERE name = 'Galar')),
  ('shield', 'Shield', 'Galar', 8, 2019, (SELECT id FROM regions WHERE name = 'Galar')),
  ('home', 'Home', 'Home', 8, 2020, (SELECT id FROM regions WHERE name = 'Home')),
  ('brilliant-diamond', 'Brilliant Diamond', 'Sinnoh', 4, 2021, (SELECT id FROM regions WHERE name = 'Sinnoh')),
  ('shining-pearl', 'Shining Pearl', 'Sinnoh', 4, 2021, (SELECT id FROM regions WHERE name = 'Sinnoh')),
  ('legends-arceus', 'Legends: Arceus', 'Hisui', 8, 2022, (SELECT id FROM regions WHERE name = 'Hisui')),
  ('scarlet', 'Scarlet', 'Paldea', 9, 2022, (SELECT id FROM regions WHERE name = 'Paldea')),
  ('violet', 'Violet', 'Paldea', 9, 2022, (SELECT id FROM regions WHERE name = 'Paldea')),
  ('legends-z-a', 'Legends: Z-A', 'Kalos', 6, 2025, (SELECT id FROM regions WHERE name = 'Kalos'))
ON CONFLICT (id) DO UPDATE SET
  "displayName" = EXCLUDED."displayName",
  region = EXCLUDED.region,
  generation = EXCLUDED.generation,
  "releaseYear" = EXCLUDED."releaseYear",
  "regionId" = EXCLUDED."regionId",
  "updatedAt" = NOW();

-- Region ↔ game mappings
INSERT INTO region_game_mappings ("regionId", "gameId") VALUES
  ((SELECT id FROM regions WHERE name = 'Kanto'), 'red'),
  ((SELECT id FROM regions WHERE name = 'Kanto'), 'blue'),
  ((SELECT id FROM regions WHERE name = 'Kanto'), 'yellow'),
  ((SELECT id FROM regions WHERE name = 'Johto'), 'gold'),
  ((SELECT id FROM regions WHERE name = 'Johto'), 'silver'),
  ((SELECT id FROM regions WHERE name = 'Johto'), 'crystal'),
  ((SELECT id FROM regions WHERE name = 'Hoenn'), 'ruby'),
  ((SELECT id FROM regions WHERE name = 'Hoenn'), 'sapphire'),
  ((SELECT id FROM regions WHERE name = 'Orre'), 'colosseum'),
  ((SELECT id FROM regions WHERE name = 'Hoenn'), 'emerald'),
  ((SELECT id FROM regions WHERE name = 'Kanto'), 'firered'),
  ((SELECT id FROM regions WHERE name = 'Kanto'), 'leafgreen'),
  ((SELECT id FROM regions WHERE name = 'Orre'), 'xd-gale-of-darkness'),
  ((SELECT id FROM regions WHERE name = 'Sinnoh'), 'diamond'),
  ((SELECT id FROM regions WHERE name = 'Sinnoh'), 'pearl'),
  ((SELECT id FROM regions WHERE name = 'Sinnoh'), 'platinum'),
  ((SELECT id FROM regions WHERE name = 'Ranch'), 'ranch'),
  ((SELECT id FROM regions WHERE name = 'Johto'), 'heart-gold'),
  ((SELECT id FROM regions WHERE name = 'Johto'), 'soul-silver'),
  ((SELECT id FROM regions WHERE name = 'Unova'), 'black'),
  ((SELECT id FROM regions WHERE name = 'Unova'), 'white'),
  ((SELECT id FROM regions WHERE name = 'Unova'), 'black-2'),
  ((SELECT id FROM regions WHERE name = 'Unova'), 'white-2'),
  ((SELECT id FROM regions WHERE name = 'Unova'), 'dream-radar'),
  ((SELECT id FROM regions WHERE name = 'Kalos'), 'x'),
  ((SELECT id FROM regions WHERE name = 'Kalos'), 'y'),
  ((SELECT id FROM regions WHERE name = 'Hoenn'), 'omega-ruby'),
  ((SELECT id FROM regions WHERE name = 'Hoenn'), 'alpha-sapphire'),
  ((SELECT id FROM regions WHERE name = 'Alola'), 'sun'),
  ((SELECT id FROM regions WHERE name = 'Alola'), 'moon'),
  ((SELECT id FROM regions WHERE name = 'Alola'), 'moon-(demo)'),
  ((SELECT id FROM regions WHERE name = 'Go'), 'go'),
  ((SELECT id FROM regions WHERE name = 'Alola'), 'ultra-sun'),
  ((SELECT id FROM regions WHERE name = 'Alola'), 'ultra-moon'),
  ((SELECT id FROM regions WHERE name = 'Kanto'), 'lg-pikachu'),
  ((SELECT id FROM regions WHERE name = 'Kanto'), 'lg-eevee'),
  ((SELECT id FROM regions WHERE name = 'Galar'), 'sword'),
  ((SELECT id FROM regions WHERE name = 'Galar'), 'shield'),
  ((SELECT id FROM regions WHERE name = 'Home'), 'home'),
  ((SELECT id FROM regions WHERE name = 'Sinnoh'), 'brilliant-diamond'),
  ((SELECT id FROM regions WHERE name = 'Sinnoh'), 'shining-pearl'),
  ((SELECT id FROM regions WHERE name = 'Hisui'), 'legends-arceus'),
  ((SELECT id FROM regions WHERE name = 'Paldea'), 'scarlet'),
  ((SELECT id FROM regions WHERE name = 'Paldea'), 'violet'),
  ((SELECT id FROM regions WHERE name = 'Kalos'), 'legends-z-a')
ON CONFLICT ("regionId", "gameId") DO NOTHING;

-- Add metadata
INSERT INTO metadata (key, value) VALUES
  ('regions_seeded', 'true'),
  ('regions_seed_date', NOW()::TEXT),
  ('regions_count', '14'),
  ('games_seeded', 'true'),
  ('games_seed_date', NOW()::TEXT),
  ('games_count', '45')
ON CONFLICT (key) DO UPDATE SET
  value = EXCLUDED.value,
  "updatedAt" = NOW();
