#!/usr/bin/env python3
"""Generate a complete route_order.json with correct progression ordering for all games.

Uses encounter data files as the source of truth for location names.
Validates that all encounter data locations are covered by the route ordering.
"""

import json
import sys
from pathlib import Path

SEEDS_DIR = Path(__file__).resolve().parent.parent / "backend" / "src" / "app" / "seeds"
DATA_DIR = SEEDS_DIR / "data"

# ============================================================================
# Route orderings by version group
# Each list starts with "Starter" and then lists locations in game progression order.
# Names must match the encounter data files exactly OR use the startswith matching
# (e.g. "Mt. Moon" matches "Mt. Moon (B2F)" because name.startswith("Mt. Moon ("))
# ============================================================================

ROUTE_ORDERS = {
    # ========================================================================
    # Gen 1 - FireRed/LeafGreen (also used by Red/Blue, Yellow, Let's Go)
    # ========================================================================
    "firered-leafgreen": [
        "Starter",
        "Pallet Town",
        "Professor Oak's Laboratory",
        "Route 01 (Kanto)",
        "Viridian City",
        "Route 22 (Kanto)",
        "Route 02 (Kanto)",
        "Viridian Forest",
        "Route 03 (Kanto)",
        "Mt. Moon",
        "Route 04 (Kanto)",
        "Route 4",
        "Cerulean City",
        "Route 24 (Kanto)",
        "Route 25 (Kanto)",
        "Route 05 (Kanto)",
        "Underground Path (Kanto - Routes 5-6)",
        "Route 06 (Kanto)",
        "Vermilion City",
        "Vermilion City (Port)",
        "Route 11 (Kanto)",
        "Diglett's Cave",
        "Route 09 (Kanto)",
        "Route 10 (Kanto)",
        "Rock Tunnel",
        "Power Plant",
        "Route 08 (Kanto)",
        "Route 07 (Kanto)",
        "Celadon City",
        "Pokémon Tower",
        "Silph Co.",
        "Saffron City",
        "Fighting Dojo",
        "Route 16 (Kanto)",
        "Route 17 (Kanto)",
        "Route 18 (Kanto)",
        "Fuchsia City",
        "Safari Zone (Kanto)",
        "Route 15 (Kanto)",
        "Route 14 (Kanto)",
        "Route 13 (Kanto)",
        "Route 12 (Kanto)",
        "Route 19 (Kanto)",
        "Route 20 (Kanto)",
        "Seafoam Islands",
        "Cinnabar Island",
        "Pokémon Mansion (Kanto)",
        "Cinnabar Lab",
        "Route 21 (Kanto)",
        "Route 23 (Kanto)",
        "Victory Road (Kanto)",
        "Cerulean Cave",
        # Sevii Islands (postgame)
        "One Island",
        "Kindle Road",
        "Treasure Beach",
        "Mt. Ember",
        "Cape Brink",
        "Berry Forest",
        "Bond Bridge",
        "Three-Isle Port",
        "Four Island",
        "Icefall Cave",
        "Resort Gorgeous",
        "Water Labyrinth",
        "Five Island",
        "Five Isle Meadow",
        "Memorial Pillar",
        "Outcast Island",
        "Green Path",
        "Water Path",
        "Ruin Valley",
        "Lost Cave",
        "Pattern Bush",
        "Trainer Tower",
        "Canyon Entrance",
        "Sevault Canyon",
        "Tanoby Ruins",
        "Tanoby Chambers",
        "Altering Cave (Sevii Islands)",
        # Event
        "Birth Island",
        "Navel Rock",
    ],

    # ========================================================================
    # Gen 2 - HeartGold/SoulSilver (also used by Gold/Silver, Crystal)
    # ========================================================================
    "heartgold-soulsilver": [
        "Starter",
        "New Bark Town",
        "Route 29 (Johto)",
        "Cherrygrove City",
        "Route 30 (Johto)",
        "Route 31 (Johto)",
        "Dark Cave",
        "Violet City",
        "Sprout Tower",
        "Route 32 (Johto)",
        "Ruins of Alph",
        "Union Cave",
        "Route 33 (Johto)",
        "Azalea Town",
        "Slowpoke Well",
        "Ilex Forest",
        "Route 34 (Johto)",
        "Goldenrod City",
        "Route 35 (Johto)",
        "National Park",
        "Route 36 (Johto)",
        "Route 37 (Johto)",
        "Ecruteak City",
        "Burned Tower",
        "Bell Tower",
        "Route 38 (Johto)",
        "Route 39 (Johto)",
        "Olivine City",
        "Route 40 (Johto)",
        "Route 41 (Johto)",
        "Cianwood City",
        "Route 42 (Johto)",
        "Mt Mortar",
        "Mt. Mortar",
        "Lake of Rage",
        "Route 43 (Johto)",
        "Route 44 (Johto)",
        "Ice Path",
        "Blackthorn City",
        "Dragons Den",
        "Dragon's Den",
        "Route 45 (Johto)",
        "Route 46 (Johto)",
        "Route 47 (Johto)",
        "Route 48 (Johto)",
        "Safari Zone (Johto)",
        "Safari Zone Gate",
        "Cliff Cave",
        "Cliff Edge Gate",
        "Whirl Islands",
        "Embedded Tower",
        "Tohjo Falls",
        "Route 27 (Kanto)",
        "Route 26 (Kanto)",
        "Victory Road (Kanto 2F)",
        # Kanto postgame
        "Route 28 (Kanto)",
        "Mt. Silver",
        "Mt. Silver (Cave)",
        "Pallet Town",
        "Professor Oak's Laboratory",
        "Route 01 (Kanto)",
        "Viridian City",
        "Viridian Forest",
        "Pewter City",
        "Pewter Museum of Science",
        "Kanto Route 2",
        "Route 03 (Kanto)",
        "Mt. Moon (B2F)",
        "Mt. Moon Square",
        "Route 04 (Kanto)",
        "Cerulean City",
        "Route 24 (Kanto)",
        "Route 25 (Kanto)",
        "Cerulean Cave",
        "Route 05 (Kanto)",
        "Route 06 (Kanto)",
        "Vermilion City",
        "Route 07 (Kanto)",
        "Route 08 (Kanto)",
        "Route 09 (Kanto)",
        "Route 10 (Kanto)",
        "Rock Tunnel",
        "Power Plant",
        "Route 11 (Kanto)",
        "Diglett's Cave",
        "Route 12 (Kanto)",
        "Route 13 (Kanto)",
        "Route 14 (Kanto)",
        "Route 15 (Kanto)",
        "Fuchsia City",
        "Route 16 (Kanto)",
        "Route 17 (Kanto)",
        "Route 18 (Kanto)",
        "Celadon City",
        "Saffron City",
        "Silph Co.",
        "Route 19 (Kanto)",
        "Route 20 (Kanto)",
        "Seafoam Islands",
        "Cinnabar Island",
        "Route 21 (Kanto)",
        "Route 22 (Kanto)",
        # Special
        "Team Rocket HQ (B1F)",
        "Sinjoh Ruins",
    ],

    # ========================================================================
    # Gen 3 - Emerald (also used by Ruby/Sapphire, ORAS)
    # ========================================================================
    "emerald": [
        "Starter",
        "Littleroot Town",
        "Hoenn Route 101",
        "Hoenn Route 103",
        "Hoenn Route 102",
        "Petalburg City",
        "Hoenn Route 104",
        "Petalburg Woods",
        "Rusturf Tunnel",
        "Hoenn Route 116",
        "Rustboro City",
        "Devon Corporation",
        "Hoenn Route 105",
        "Hoenn Route 106",
        "Dewford Town",
        "Granite Cave",
        "Hoenn Route 107",
        "Hoenn Route 108",
        "Hoenn Route 109",
        "Slateport City",
        "Hoenn Route 110",
        "New Mauville",
        "Hoenn Route 117",
        "Hoenn Route 111",
        "Desert Ruins",
        "Mirage Tower",
        "Hoenn Route 112",
        "Fiery Path",
        "Jagged Pass",
        "Lavaridge Town",
        "Hoenn Route 113",
        "Hoenn Route 114",
        "Meteor Falls",
        "Hoenn Route 115",
        "Fortree City",
        "Weather Institute",
        "Hoenn Route 118",
        "Hoenn Route 119",
        "Route 119",
        "Hoenn Route 120",
        "Hoenn Route 121",
        "Safari Zone (Hoenn)",
        "Lilycove City",
        "Hoenn Route 122",
        "Mt. Pyre",
        "Hoenn Route 123",
        "Team Magma Hideout",
        "Team Aqua Hideout",
        "Magma Hideout (Jagged Pass)",
        "Hoenn Route 124",
        "Mossdeep City",
        "Hoenn Route 125",
        "Shoal Cave",
        "Hoenn Route 126",
        "Sootopolis City",
        "Cave of Origin",
        "Hoenn Route 127",
        "Hoenn Route 128",
        "Seafloor Cavern",
        "Hoenn Route 129",
        "Hoenn Route 130",
        "Hoenn Route 131",
        "Pacifidlog Town",
        "Hoenn Route 132",
        "Hoenn Route 133",
        "Hoenn Route 134",
        "Ever Grande City",
        "Victory Road (Hoenn)",
        "Sky Pillar",
        "Abandoned Ship",
        "Desert Underpass",
        "Artisan Cave",
        "Altering Cave (Sevii Islands)",
        # Postgame/special
        "Ancient Tomb",
        "Island Cave",
        "Battle Frontier (Hoenn)",
        "Southern Island",
        "Marine Cave",
        "Terra Cave",
        "Mirage Island",
        "Birth Island",
        "Faraway Island",
        "Navel Rock",
    ],

    # ========================================================================
    # Gen 4 - Platinum
    # ========================================================================
    "platinum": [
        "Starter",
        "Twinleaf Town",
        "Lake Verity",
        "Route 201 (Sinnoh)",
        "Route 202 (Sinnoh)",
        "Route 203 (Sinnoh)",
        "Oreburgh Gate",
        "Oreburgh City",
        "Oreburgh Mine (B1F)",
        "Oreburgh Mining Museum",
        "Sinnoh Route 204",
        "Ravaged Path",
        "Sinnoh Route 205",
        "Valley Windworks",
        "Eterna Forest",
        "Eterna City",
        "Eterna Condominiums",
        "Old Chateau",
        "Route 206 (Sinnoh)",
        "Wayward Cave",
        "Route 207 (Sinnoh)",
        "Mount Coronet",
        "Route 208 (Sinnoh)",
        "Hearthome City",
        "Route 209 (Sinnoh)",
        "Lost Tower",
        "Solaceon Ruins",
        "Sinnoh Route 210",
        "Route 215 (Sinnoh)",
        "Veilstone City",
        "Route 214 (Sinnoh)",
        "Valor Lakefront",
        "Route 213 (Sinnoh)",
        "Pastoria City",
        "Great Marsh",
        "Sinnoh Route 212",
        "Trophy Garden",
        "Route 218 (Sinnoh)",
        "Canalave City",
        "Iron Island",
        "Sinnoh Route 211",
        "Celestic Town",
        "Fuego Ironworks",
        "Route 216 (Sinnoh)",
        "Route 217 (Sinnoh)",
        "Snowpoint City",
        "Acuity Lakefront",
        "Lake Acuity",
        "Lake Valor",
        "Spear Pillar",
        "Distortion World",
        "Route 222 (Sinnoh)",
        "Sunyshore City",
        "Route 223 (Sinnoh)",
        "Victory Road (Sinnoh)",
        "Pokémon League (Sinnoh)",
        # Postgame
        "Sendoff Spring",
        "Turnback Cave",
        "Route 224 (Sinnoh)",
        "Route 225 (Sinnoh)",
        "Route 226 (Sinnoh)",
        "Route 227 (Sinnoh)",
        "Stark Mountain",
        "Route 228 (Sinnoh)",
        "Route 229 (Sinnoh)",
        "Resort Area",
        "Route 230 (Sinnoh)",
        "Snowpoint Temple",
        "Route 219 (Sinnoh)",
        "Route 220 (Sinnoh)",
        "Route 221 (Sinnoh)",
        "Maniac Tunnel",
        # Event/special
        "Flower Paradise",
        "Newmoon Island",
        "Hall of Origin",
        "Rock Peak Ruins (Sinnoh)",
        "Iceberg Ruins (Sinnoh)",
        "Iron Ruins (Sinnoh)",
    ],

    # ========================================================================
    # Gen 4 - Diamond/Pearl
    # ========================================================================
    "diamond-pearl": [
        "Starter",
        "Twinleaf Town",
        "Lake Verity",
        "Route 201 (Sinnoh)",
        "Route 202 (Sinnoh)",
        "Route 203 (Sinnoh)",
        "Oreburgh Gate",
        "Oreburgh City",
        "Oreburgh Mine (B1F)",
        "Oreburgh Mining Museum",
        "Sinnoh Route 204",
        "Ravaged Path",
        "Sinnoh Route 205",
        "Valley Windworks",
        "Eterna Forest",
        "Eterna City",
        "Eterna Condominiums",
        "Old Chateau",
        "Route 206 (Sinnoh)",
        "Wayward Cave",
        "Route 207 (Sinnoh)",
        "Mount Coronet",
        "Route 208 (Sinnoh)",
        "Hearthome City",
        "Route 209 (Sinnoh)",
        "Lost Tower",
        "Solaceon Ruins",
        "Sinnoh Route 210",
        "Route 215 (Sinnoh)",
        "Route 214 (Sinnoh)",
        "Valor Lakefront",
        "Route 213 (Sinnoh)",
        "Pastoria City",
        "Great Marsh",
        "Sinnoh Route 212",
        "Trophy Garden",
        "Route 218 (Sinnoh)",
        "Canalave City",
        "Iron Island",
        "Sinnoh Route 211",
        "Celestic Town",
        "Fuego Ironworks",
        "Route 216 (Sinnoh)",
        "Route 217 (Sinnoh)",
        "Snowpoint City",
        "Acuity Lakefront",
        "Lake Acuity",
        "Lake Valor",
        "Lake Valor #2",
        "Spear Pillar",
        "Route 222 (Sinnoh)",
        "Sunyshore City",
        "Route 223 (Sinnoh)",
        "Victory Road (Sinnoh)",
        "Pokémon League (Sinnoh)",
        # Postgame
        "Sendoff Spring",
        "Turnback Cave",
        "Route 224 (Sinnoh)",
        "Route 225 (Sinnoh)",
        "Route 226 (Sinnoh)",
        "Route 227 (Sinnoh)",
        "Stark Mountain",
        "Route 228 (Sinnoh)",
        "Route 229 (Sinnoh)",
        "Resort Area",
        "Route 230 (Sinnoh)",
        "Snowpoint Temple",
        "Route 219 (Sinnoh)",
        "Route 220 (Sinnoh)",
        "Route 221 (Sinnoh)",
        "Maniac Tunnel",
        "Rock Peak Ruins (Sinnoh)",
    ],

    # ========================================================================
    # Gen 5 - Black/White
    # ========================================================================
    "black-white": [
        "Starter",
        "Nuvema Town",
        "Unova Route 1",
        "Unova Route 2",
        "Striaton City",
        "Dreamyard",
        "Unova Route 3",
        "Wellspring Cave",
        "Nacrene City",
        "Pinwheel Forest",
        "Castelia City",
        "Unova Route 4",
        "Desert Resort",
        "Relic Castle",
        "Unova Route 16",
        "Lostlorn Forest",
        "Unova Route 5",
        "Driftveil Drawbridge",
        "Driftveil City",
        "Cold Storage (Outside)",
        "Unova Route 6",
        "Chargestone Cave",
        "Mistralton Cave",
        "Unova Route 7",
        "Celestial Tower",
        "Twist Mountain",
        "Icirrus City",
        "Moor of Icirrus",
        "Dragonspiral Tower",
        "Unova Route 8",
        "Unova Route 9",
        "Unova Route 10",
        "Victory Road (Unova Black & White)",
        "N's Castle",
        # Postgame
        "Unova Route 11",
        "Village Bridge",
        "Unova Route 12",
        "Unova Route 13",
        "Undella Town",
        "Undella Bay",
        "Unova Route 14",
        "Abundant Shrine",
        "White Forest",
        "Unova Route 15",
        "Marvelous Bridge",
        "Challenger's Cave",
        "Unova Route 17",
        "Unova Route 18",
        "P2 Laboratory",
        "Giant Chasm",
        "Liberty Garden",
    ],

    # ========================================================================
    # Gen 5 - Black 2/White 2
    # ========================================================================
    "black-2-white-2": [
        "Starter",
        "Aspertia City",
        "Unova Route 19",
        "Floccesy Town",
        "Floccesy Ranch",
        "Unova Route 20",
        "Virbank City",
        "Virbank Complex",
        "Castelia City",
        "Castelia Sewers",
        "Relic Passage",
        "Unova Route 4",
        "Desert Resort",
        "Relic Castle",
        "Nimbasa City",
        "Unova Route 16",
        "Lostlorn Forest",
        "Unova Route 5",
        "Driftveil Drawbridge",
        "Driftveil City",
        "Unova Route 6",
        "Mistralton Cave",
        "Chargestone Cave",
        "Unova Route 7",
        "Celestial Tower",
        "Reversal Mountain",
        "Strange House",
        "Undella Town",
        "Undella Bay",
        "Unova Route 13",
        "Unova Route 12",
        "Village Bridge",
        "Unova Route 11",
        "Unova Route 9",
        "Humilau City",
        "Unova Route 21",
        "Seaside Cave",
        "Unova Route 22",
        "Giant Chasm",
        "Unova Route 23",
        "Victory Road (Unova Black 2 & White 2)",
        # Postgame
        "Unova Route 1",
        "Unova Route 2",
        "Accumula Town",
        "Unova Route 3",
        "Striaton City",
        "Dreamyard",
        "Nacrene City",
        "Pinwheel Forest",
        "Wellspring Cave",
        "Unova Route 15",
        "Marvelous Bridge",
        "Unova Route 8",
        "Moor of Icirrus",
        "Icirrus City",
        "Dragonspiral Tower",
        "Twist Mountain",
        "Unova Route 17",
        "Unova Route 18",
        "P2 Laboratory",
        "Unova Route 14",
        "Abundant Shrine",
        "Clay Tunnel",
        "Underground Ruins",
        "Nature Preserve",
    ],

    # ========================================================================
    # Gen 6 - X/Y
    # ========================================================================
    "x-y": [
        "Starter",
        "Vaniville Town",
        "Aquacorde Town",
        "Kalos Route 02",
        "Santalune Forest",
        "Kalos Route 03",
        "Santalune City",
        "Kalos Route 22",
        "Kalos Route 04",
        "South Boulevard",
        "Kalos Route 05",
        "Camphrier Town",
        "Kalos Route 06",
        "Parfume Palace",
        "Kalos Route 07",
        "Berry Fields (Kalos)",
        "Connecting Cave",
        "Kalos Route 08",
        "Ambrette Town",
        "Kalos Route 09",
        "Glittering Cave",
        "Cyllage City",
        "Kalos Route 10",
        "Geosenge Town",
        "Kalos Route 11",
        "Reflection Cave",
        "Shalour City",
        "Tower of Mastery",
        "Kalos Route 12",
        "Azure Bay",
        "Coumarine City",
        "Kalos Route 13",
        "Lumiose City",
        "Kalos Route 14",
        "Laverre City",
        "Kalos Route 15",
        "Lost Hotel",
        "Kalos Route 16",
        "Frost Cavern",
        "Kalos Route 17",
        "Team Flare Secret HQ",
        "Kalos Route 18",
        "Terminus Cave",
        "Couriway Town",
        "Kalos Route 19",
        "Kalos Route 20",
        "Pokémon Village",
        "Snowbelle City",
        "Kalos Route 21",
        "Victory Road (Kalos)",
        # Postgame
        "Unknown Dungeon (Kalos)",
        "Sea Spirits Den",
        "Friend Safari",
    ],

    # ========================================================================
    # Gen 7 - Sun/Moon
    # ========================================================================
    "sun-moon": [
        "Starter",
        "Iki Town",
        "Alola Route 1",
        "Trainer\u2019s School (Alola)",
        "Hau'oli City",
        "Alola Route 2",
        "Hau'oli Cemetery",
        "Verdant Cavern",
        "Alola Route 3",
        "Melemele Meadow",
        "Seaward Cave",
        "Kala'e Bay",
        "Ten Carat Hill",
        "Melemele Sea",
        # Akala
        "Alola Route 4",
        "Paniola Town",
        "Paniola Ranch",
        "Alola Route 5",
        "Brooklet Hill",
        "Alola Route 6",
        "Alola Route 7",
        "Wela Volcano Park",
        "Alola Route 8",
        "Lush Jungle",
        "Diglett's Tunnel",
        "Alola Route 9",
        "Memorial Hill",
        "Akala Outskirts",
        "Konikoni City",
        "Hano Beach",
        "Ruins of Life",
        # Ula'ula
        "Malie City",
        "Malie Garden",
        "Alola Route 10",
        "Mount Hokulani",
        "Alola Route 11",
        "Alola Route 12",
        "Blush Mountain",
        "Alola Route 13",
        "Haina Desert",
        "Ruins of Abundance",
        "Tapu Village",
        "Alola Route 14",
        "Thrifty Megamart (Abandoned Site)",
        "Alola Route 15",
        "Aether House",
        "Alola Route 16",
        "Ula'ula Meadow",
        "Alola Route 17",
        "Ula'ula Beach",
        "Aether Paradise",
        # Poni
        "Seafolk Village",
        "Poni Wilds",
        "Ancient Poni Path",
        "Poni Breaker Coast",
        "Ruins of Hope",
        "Exeggutor Island",
        "Vast Poni Canyon",
        "Altar of the Sunne",
        "Altar of the Moone",
        "Mount Lanakila",
        # Postgame
        "Lake of the Sunne",
        "Lake of the Moone",
        "Poni Grove",
        "Poni Plains",
        "Poni Meadow",
        "Poni Coast",
        "Poni Gauntlet",
        "Resolution Cave",
        "Ruins of Conflict",
    ],

    # ========================================================================
    # Gen 7 - Ultra Sun/Ultra Moon
    # ========================================================================
    "ultra-sun-ultra-moon": [
        "Starter",
        "Alola Route 1",
        "Trainer\u2019s School (Alola)",
        "Hau'oli City",
        "Alola Route 2",
        "Hau'oli Cemetery",
        "Verdant Cavern",
        "Alola Route 3",
        "Melemele Meadow",
        "Seaward Cave",
        "Kala'e Bay",
        "Sandy Cave",
        "Ten Carat Hill",
        "Melemele Sea",
        # Akala
        "Heahea City",
        "Heahea Beach",
        "Alola Route 4",
        "Pikachu Valley",
        "Paniola Town",
        "Paniola Ranch",
        "Alola Route 5",
        "Brooklet Hill",
        "Alola Route 6",
        "Royal Avenue",
        "Alola Route 7",
        "Wela Volcano Park",
        "Dividing Peak Tunnel",
        "Alola Route 8",
        "Lush Jungle",
        "Diglett's Tunnel",
        "Alola Route 9",
        "Konikoni City",
        "Akala Outskirts",
        "Hano Beach",
        # Ula'ula
        "Malie City (Outer Cape)",
        "Malie Garden",
        "Alola Route 10",
        "Mount Hokulani",
        "Alola Route 11",
        "Alola Route 12",
        "Blush Mountain",
        "Alola Route 13",
        "Haina Desert",
        "Tapu Village",
        "Alola Route 14",
        "Thrifty Megamart (Abandoned Site)",
        "Alola Route 15",
        "Aether House",
        "Alola Route 16",
        "Ula'ula Meadow",
        "Alola Route 17",
        "Memorial Hill",
        "Ula'ula Beach",
        "Aether Paradise",
        # Poni
        "Seafolk Village",
        "Poni Wilds",
        "Ancient Poni Path",
        "Poni Breaker Coast",
        "Ruins of Hope",
        "Exeggutor Island",
        "Vast Poni Canyon",
        "Mahalo Trail",
        "Ultra Megalopolis",
        "Megalo Tower",
        "Mount Lanakila",
        # Postgame
        "Lake of the Sunne",
        "Lake of the Moone",
        "Poni Grove",
        "Poni Plains",
        "Poni Meadow",
        "Poni Coast",
        "Poni Gauntlet",
        "Resolution Cave",
        "Ultra Space Wilds",
        "Ultra Deep-Sea",
        "Ultra Plant",
        "Ultra Forest",
        "Ultra Crater",
        "Ultra Ruin",
        "Team Rocket\u2019s Castle",
        "Ruins of Conflict",
        "Ruins of Life",
        "Ruins of Abundance",
    ],

    # ========================================================================
    # Gen 8 - Sword/Shield
    # ========================================================================
    "sword-shield": [
        "Starter",
        "Postwick",
        "Slumbering Weald",
        "Wedgehurst",
        "Route 1 - Galar",
        "Galar Route 2",
        # Wild Area - Southern
        "Rolling Fields",
        "Dappled Grove",
        "Watchtower Ruins",
        "East Lake Axewell",
        "West Lake Axewell",
        "Axew's Eye",
        "South Lake Miloch",
        "Giant's Seat",
        "North Lake Miloch",
        # Motostoke to Turffield
        "Motostoke",
        "Galar Route 3",
        "Galar Mine",
        "Galar Route 4",
        "Turffield",
        "Galar Route 5",
        "Hulbury",
        "Galar Mine No 2",
        "Motostoke Outskirts",
        # Wild Area - Northern
        "Motostoke Riverbank",
        "Bridge Field",
        "Stony Wilderness",
        "Dusty Bowl",
        "Giant's Mirror",
        "Giant's Cap",
        "Hammerlocke Hills",
        "Lake of Outrage",
        # Hammerlocke onward
        "Hammerlocke",
        "Route 6 - Galar",
        "Stow-on-Side",
        "Glimwood Tangle",
        "Route 7 - Galar",
        "Galar Route 8",
        "Circhester",
        "Galar Route 9",
        "Spikemuth",
        "Galar Route 10",
        "Wyndon",
        "Tower Summit",
        "Battle Tower (Galar)",
        # Isle of Armor DLC
        "Fields of Honor",
        "Master Dojo",
        "Soothing Wetlands",
        "Forest of Focus",
        "Challenge Beach",
        "Challenge Road",
        "Brawler's Cave",
        "Courageous Cavern",
        "Loop Lagoon",
        "Training Lowlands",
        "Warm-Up Tunnel",
        "Potbottom Desert",
        "Workout Sea",
        "Stepping Stone Sea",
        "Insular Sea",
        "Honeycalm Sea",
        "Honeycalm Island",
        # Crown Tundra DLC
        "Slippery Slope",
        "Freezington",
        "Frostpoint Field",
        "Giant's Bed",
        "Old Cemetery",
        "Snowslide Slope",
        "Tunnel to the Top",
        "Path to the Peak",
        "Crown Shrine",
        "Giant's Foot",
        "Roaring Sea Caves",
        "Frigid Sea",
        "Three-Point Pass",
        "Ballimere Lake",
        "Lakeside Cave",
        "Dyna Tree Hill",
        "Max Lair",
        "Rock Peak Ruins (Galar)",
        "Iceberg Ruins (Galar)",
        "Iron Ruins (Galar)",
        "Split-Decision Ruins",
        "Meetup Spot",
    ],

    # ========================================================================
    # Gen 8 - Brilliant Diamond/Shining Pearl
    # ========================================================================
    "brilliant-diamond-shining-pearl": [
        "Starter",
        "Twinleaf Town",
        "Lake Verity",
        "Route 201 (Sinnoh)",
        "Route 202 (Sinnoh)",
        "Route 203 (Sinnoh)",
        "Oreburgh Gate",
        "Oreburgh City",
        "Oreburgh Mine",
        "Oreburgh Mining Museum",
        "Sinnoh Route 204",
        "Ravaged Path",
        "Floaroma Town",
        "Sinnoh Route 205",
        "Valley Windworks",
        "Eterna Forest",
        "Eterna City",
        "Eterna Condominiums",
        "Old Chateau",
        "Route 206 (Sinnoh)",
        "Wayward Cave",
        "Route 207 (Sinnoh)",
        "Mount Coronet",
        "Route 208 (Sinnoh)",
        "Hearthome City",
        "Route 209 (Sinnoh)",
        "Lost Tower",
        "Solaceon Ruins",
        "Sinnoh Route 210",
        "Route 215 (Sinnoh)",
        "Route 214 (Sinnoh)",
        "Valor Lakefront",
        "Route 213 (Sinnoh)",
        "Pastoria City",
        "Great Marsh",
        "Sinnoh Route 212",
        "Trophy Garden",
        "Route 218 (Sinnoh)",
        "Canalave City",
        "Iron Island",
        "Sinnoh Route 211",
        "Celestic Town",
        "Fuego Ironworks",
        "Route 216 (Sinnoh)",
        "Route 217 (Sinnoh)",
        "Snowpoint City",
        "Acuity Lakefront",
        "Lake Acuity",
        "Lake Valor",
        "Spear Pillar",
        "Route 222 (Sinnoh)",
        "Sunyshore City",
        "Route 223 (Sinnoh)",
        "Victory Road (Sinnoh)",
        "Pokémon League (Sinnoh)",
        # Postgame
        "Sendoff Spring",
        "Turnback Cave",
        "Route 224 (Sinnoh)",
        "Route 225 (Sinnoh)",
        "Route 226 (Sinnoh)",
        "Route 227 (Sinnoh)",
        "Stark Mountain",
        "Route 228 (Sinnoh)",
        "Route 229 (Sinnoh)",
        "Resort Area",
        "Route 230 (Sinnoh)",
        "Snowpoint Temple",
        "Route 219 (Sinnoh)",
        "Route 220 (Sinnoh)",
        "Route 221 (Sinnoh)",
        "Maniac Tunnel",
        "Ramanas Park",
        # Grand Underground caves
        "Spacious Cave",
        "Grassland Cave",
        "Fountainspring Cave",
        "Rocky Cave",
        "Volcanic Cave",
        "Sandsear Cave",
        "Swampy Cave",
        "Dazzling Cave",
        "Stargleam Cavern",
        "Still-Water Cavern",
        "Sunlit Cavern",
        "Big Bluff Cavern",
        "Bogsunk Cavern",
        "Glacial Cavern",
        "Icy Cave",
        "Riverbank Cave",
        "Typhlo Cavern",
        "Whiteout Cave",
        # Event/special
        "Flower Paradise",
        "Newmoon Island",
        "Hall of Origin",
    ],

    # ========================================================================
    # Gen 8 - Legends: Arceus
    # ========================================================================
    "legends-arceus": [
        "Starter",
        "Jubilife Village",
        # Obsidian Fieldlands
        "Aspiration Hill",
        "Floaro Gardens",
        "Horseshoe Plains",
        "Deertrack Path",
        "Deertrack Heights",
        "Windswept Run",
        "Nature's Pantry",
        "The Heartwood",
        "Grueling Grove",
        "Worn Bridge",
        "Obsidian Falls",
        "Sandgem Flats",
        "Ramanas Island",
        "Lake Verity",
        "Oreburrow Tunnel",
        "Wayward Wood",
        "Grandtree Arena",
        # Crimson Mirelands
        "Golden Lowlands",
        "Gapejaw Bog",
        "Droning Meadow",
        "Holm of Trials",
        "Cloudpool Ridge",
        "Scarlet Bog",
        "Sludge Mound",
        "Cottonsedge Prairie",
        "Bolderoll Slope",
        "Bolderoll Ravine",
        "Diamond Heath",
        "Diamond Settlement",
        "Pearl Settlement",
        "Lonely Spring",
        "Shrouded Ruins",
        "Solaceon Ruins",
        "Ursa's Ring",
        "Brava Arena",
        "Lake Valor",
        # Cobalt Coastlands
        "Ginkgo Landing",
        "Aipom Hill",
        "Bathers\u2019 Lagoon",
        "Hideaway Bay",
        "Deadwood Haunt",
        "Tombolo Walk",
        "Windbreak Stand",
        "Tidewater Dam",
        "Crossing Slope",
        "Veilstone Cape",
        "Spring Path",
        "Castaway Shore",
        "Seaside Hollow",
        "Sands Reach",
        "Lunker's Lair",
        "Islespy Shore",
        "Firespit Island",
        "Tranquility Cove",
        "Seagrass Haven",
        # Coronet Highlands
        "Heavenward Lookout",
        "Wayward Cave",
        "Ancient Quarry",
        "Celestica Trail",
        "Celestica Ruins",
        "Sacred Plaza",
        "Primeval Grotto",
        "Clamberclaw Cliffs",
        "Fabled Spring",
        "Cloudcap Pass",
        "Stonetooth Rows",
        "Sonorous Path",
        "Secret Hollow",
        "Moonview Arena",
        # Alabaster Icelands
        "Snowfall Hot Spring",
        "Whiteout Valley",
        "Crevasse Passage",
        "Avalanche Slopes",
        "Arena\u2019s Approach",
        "Avalugg\u2019s Legacy",
        "Glacier Terrace",
        "Heart's Crag",
        "Icebound Falls",
        "Icepeak Cavern",
        "Bonechill Wastes",
        "Lake Acuity",
        "Snowpoint Temple",
        "Ice Column Chamber",
        # Endgame
        "Temple of Sinnoh",
        "Lava Dome Sanctum",
        "Hall of Origin",
        "Lake Verity #2",
    ],

    # ========================================================================
    # Gen 9 - Scarlet/Violet
    # ========================================================================
    "scarlet-violet": [
        "Starter",
        "Cabo Poco",
        "Poco Path",
        "Area One (South Province)",
        "Naranja Academy",
        "Uva Academy",
        "Cortondo",
        "South Paldean Sea (Ocean)",
        "Area Two (South Province)",
        "Area Three (South Province)",
        "Area Four (South Province)",
        "Area Five (South Province)",
        "Area One (West Province)",
        "Asado Desert",
        "West Paldean Sea (Ocean)",
        "Cascarrafa",
        "Area Two (West Province)",
        "Colonnade Hollow",
        "Area One (East Province)",
        "Levincia",
        "East Paldean Sea",
        "Area Two (East Province)",
        "Tagtree Thicket",
        "Area Three (East Province)",
        "Area Three (West Province)",
        "Fellhorn Gorge",
        "Dalizapa Passage",
        "Alfornada Cavern",
        "Area Six (South Province)",
        "Glaseado Mountain",
        "Area Three (North Province)",
        "North Paldean Sea",
        "Area One (North Province)",
        "Area Two (North Province)",
        "Socarrat Trail",
        "Casseroya Lake",
        "Pokémon League (Paldea)",
        "Great Crater of Paldea",
        "Area Zero",
        "Zero Lab",
        # Postgame shrines
        "Grasswither Shrine",
        "Groundblight Shrine",
        "Firescourge Shrine",
        "Icerend Shrine",
        # DLC - Teal Mask (Kitakami)
        "Mossui Town",
        "Kitakami Road",
        "Kitakami Wilds",
        "Reveler's Road",
        "Apple Hills",
        "Wistful Fields",
        "Oni Mountain",
        "Oni's Maw",
        "Kitakami Hall",
        "Infernal Pass",
        "Crystal Pool",
        "Mossfell Confluence",
        "Inlet Grotto (Cave)",
        "Torchlit Labyrinth (Cave)",
        "Chilling Waterhead",
        "Loyalty Plaza",
        "Timeless Woods",
        "Paradise Barrens",
        # DLC - Indigo Disk (Terrarium)
        "Coastal Biome",
        "Savanna Biome",
        "Canyon Biome",
        "Polar Biome",
        "Chargestone Cavern",
        # DLC - Area Zero Underdepths
        "Area Zero Underdepths",
        # Random encounters
        "Random (Around Paldea)",
        "Random (Around Kitakami)",
        "Random (Around Terrarium)",
    ],

    # ========================================================================
    # Gen 9 - Legends: Z-A
    # ========================================================================
    "legends-z-a": [
        "Starter",
        "Wild Zone 1",
        "Wild Zone 2",
        "Wild Zone 3",
        "Wild Zone 4",
        "Wild Zone 5",
        "Wild Zone 6",
        "Wild Zone 7",
        "Wild Zone 8",
        "Wild Zone 9",
        "Wild Zone 10",
        "Wild Zone 11",
        "Wild Zone 12",
        "Wild Zone 13",
        "Wild Zone 14",
        "Wild Zone 15",
        "Wild Zone 16",
        "Wild Zone 17",
        "Wild Zone 18",
        "Wild Zone 19",
        "Wild Zone 20",
    ],
}

# Aliases: maps version group keys that share the same ordering
ALIASES = {
    "red-blue": "firered-leafgreen",
    "yellow": "firered-leafgreen",
    "lets-go": "firered-leafgreen",
    "gold-silver": "heartgold-soulsilver",
    "crystal": "heartgold-soulsilver",
    "ruby-sapphire": "emerald",
    "omega-ruby-alpha-sapphire": "emerald",
}


def validate_route_order(vg_key: str, order_list: list[str]) -> list[str]:
    """Validate route order against encounter data files.

    Returns list of warning messages.
    """
    warnings = []

    # Load version_groups.json to find which game files to check
    vg_path = SEEDS_DIR / "version_groups.json"
    with open(vg_path) as f:
        version_groups = json.load(f)

    # Find the version group config
    vg_config = version_groups.get(vg_key)
    if not vg_config:
        warnings.append(f"  WARNING: Version group '{vg_key}' not found in version_groups.json")
        return warnings

    versions = vg_config.get("versions", [])

    for version in versions:
        data_file = DATA_DIR / f"{version}.json"
        if not data_file.exists():
            warnings.append(f"  WARNING: Encounter data file not found: {data_file.name}")
            continue

        with open(data_file) as f:
            encounter_data = json.load(f)

        encounter_names = set()
        for entry in encounter_data:
            encounter_names.add(entry["name"])

        # Check which encounter locations are NOT matched by the route order
        unmatched = []
        for name in sorted(encounter_names):
            matched = False
            for ordered_name in order_list:
                if name == ordered_name or name.startswith(ordered_name + " ("):
                    matched = True
                    break
            if not matched:
                unmatched.append(name)

        if unmatched:
            warnings.append(f"  {version}: {len(unmatched)} unmatched encounter locations:")
            for n in unmatched:
                warnings.append(f"    - {n}")

    return warnings


def main():
    output = {
        "_comment": "Route progression order by version group. Aliases at the bottom map version groups that share the same ordering.",
        "routes": ROUTE_ORDERS,
        "aliases": ALIASES,
    }

    # Validate
    all_warnings = []
    for vg_key, order_list in ROUTE_ORDERS.items():
        warnings = validate_route_order(vg_key, order_list)
        if warnings:
            all_warnings.append(f"\n{vg_key}:")
            all_warnings.extend(warnings)

    if all_warnings:
        print("Validation warnings:", file=sys.stderr)
        for w in all_warnings:
            print(w, file=sys.stderr)
        print(file=sys.stderr)

    # Write output
    output_path = SEEDS_DIR / "route_order.json"
    with open(output_path, "w", encoding="utf-8") as f:
        json.dump(output, f, indent=2, ensure_ascii=False)
        f.write("\n")

    print(f"Written to {output_path}")

    # Summary
    total_routes = sum(len(v) for v in ROUTE_ORDERS.values())
    print(f"  {len(ROUTE_ORDERS)} version groups, {total_routes} total route entries, {len(ALIASES)} aliases")

    if all_warnings:
        sys.exit(1)


if __name__ == "__main__":
    main()
