"""Catálogos de ubicaciones por país: ciudad, puerto (UN/LOCODE), aeropuerto (IATA).

Dependientes de `pais` (`parent_catalog='pais'`, `parent_code=<ISO3>`). Curado a las
rutas de comercio más usadas (extensible: agregar países/nodos según tarifarios).
Los códigos de puerto/aeropuerto se alinean con los que usan las lanes del tarifario
para que el Cotizador encuentre ruta.
"""

# (ISO3, ciudades[(code,label)], puertos[(code,label)], aeropuertos[(code,label)])
_LOC = [
    ("MEX",
     [("MX-CDMX", "Ciudad de México"), ("MX-GDL", "Guadalajara"), ("MX-MTY", "Monterrey"),
      ("MX-QRO", "Querétaro"), ("MX-TIJ", "Tijuana"), ("MX-VER", "Veracruz")],
     [("MXZLO", "Manzanillo"), ("MXVER", "Veracruz"), ("MXATM", "Altamira"),
      ("MXLZC", "Lázaro Cárdenas"), ("MXPGO", "Progreso"), ("MXESE", "Ensenada")],
     [("MEX", "AICM Ciudad de México"), ("NLU", "AIFA Santa Lucía"), ("GDL", "Guadalajara"),
      ("MTY", "Monterrey"), ("TIJ", "Tijuana"), ("CUN", "Cancún")]),
    ("USA",
     [("US-LAX", "Los Ángeles"), ("US-NYC", "Nueva York"), ("US-HOU", "Houston"),
      ("US-CHI", "Chicago"), ("US-MIA", "Miami"), ("US-LRD", "Laredo")],
     [("USLAX", "Los Angeles"), ("USLGB", "Long Beach"), ("USNYC", "Nueva York/NJ"),
      ("USHOU", "Houston"), ("USSAV", "Savannah"), ("USSEA", "Seattle"), ("USOAK", "Oakland")],
     [("LAX", "Los Ángeles"), ("JFK", "Nueva York JFK"), ("ORD", "Chicago O'Hare"),
      ("MIA", "Miami"), ("DFW", "Dallas Fort Worth"), ("ATL", "Atlanta")]),
    ("CHN",
     [("CN-SHA", "Shanghái"), ("CN-SZX", "Shenzhen"), ("CN-CAN", "Guangzhou"),
      ("CN-NGB", "Ningbo"), ("CN-TAO", "Qingdao"), ("CN-PEK", "Pekín")],
     [("CNSHA", "Shanghái"), ("CNNGB", "Ningbo"), ("CNSZX", "Shenzhen"),
      ("CNTAO", "Qingdao"), ("CNCAN", "Guangzhou"), ("CNXMN", "Xiamen"), ("CNTXG", "Tianjin")],
     [("PVG", "Shanghái Pudong"), ("PEK", "Pekín Capital"), ("CAN", "Guangzhou"),
      ("SZX", "Shenzhen"), ("HKG", "Hong Kong")]),
    ("DEU",
     [("DE-HAM", "Hamburgo"), ("DE-FRA", "Fráncfort"), ("DE-MUC", "Múnich"), ("DE-BER", "Berlín")],
     [("DEHAM", "Hamburgo"), ("DEBRV", "Bremerhaven")],
     [("FRA", "Fráncfort"), ("MUC", "Múnich"), ("HAM", "Hamburgo")]),
    ("ESP",
     [("ES-MAD", "Madrid"), ("ES-BCN", "Barcelona"), ("ES-VLC", "Valencia")],
     [("ESVLC", "Valencia"), ("ESBCN", "Barcelona"), ("ESALG", "Algeciras")],
     [("MAD", "Madrid Barajas"), ("BCN", "Barcelona")]),
    ("NLD",
     [("NL-RTM", "Róterdam"), ("NL-AMS", "Ámsterdam")],
     [("NLRTM", "Róterdam")],
     [("AMS", "Ámsterdam Schiphol")]),
    ("BRA",
     [("BR-SAO", "São Paulo"), ("BR-SSZ", "Santos"), ("BR-RIO", "Río de Janeiro")],
     [("BRSSZ", "Santos"), ("BRPNG", "Paranaguá"), ("BRRIG", "Rio Grande")],
     [("GRU", "São Paulo Guarulhos"), ("GIG", "Río de Janeiro")]),
    ("CAN",
     [("CA-YVR", "Vancouver"), ("CA-YYZ", "Toronto"), ("CA-YMQ", "Montreal")],
     [("CAVAN", "Vancouver"), ("CAMTR", "Montreal"), ("CAHAL", "Halifax")],
     [("YVR", "Vancouver"), ("YYZ", "Toronto Pearson")]),
    ("JPN",
     [("JP-TYO", "Tokio"), ("JP-OSA", "Osaka"), ("JP-YOK", "Yokohama")],
     [("JPYOK", "Yokohama"), ("JPTYO", "Tokio"), ("JPNGO", "Nagoya"), ("JPKOB", "Kobe")],
     [("NRT", "Tokio Narita"), ("HND", "Tokio Haneda"), ("KIX", "Osaka Kansai")]),
    ("KOR",
     [("KR-SEL", "Seúl"), ("KR-PUS", "Busan")],
     [("KRPUS", "Busan"), ("KRINC", "Incheon")],
     [("ICN", "Seúl Incheon")]),
]


def _build() -> dict:
    ciudad, puerto, aeropuerto = [], [], []
    for iso3, cities, ports, airports in _LOC:
        for code, label in cities:
            ciudad.append({"code": code, "label": label, "parent_catalog": "pais", "parent_code": iso3})
        for code, label in ports:
            puerto.append({"code": code, "label": f"{label} ({code})", "parent_catalog": "pais", "parent_code": iso3})
        for code, label in airports:
            aeropuerto.append({"code": code, "label": f"{label} ({code})", "parent_catalog": "pais", "parent_code": iso3})
    return {
        "ciudad": {"label": "Ciudad", "is_system": True, "items": ciudad},
        "puerto": {"label": "Puerto", "is_system": True, "items": puerto},
        "aeropuerto": {"label": "Aeropuerto", "is_system": True, "items": aeropuerto},
    }


LOCATION_CATALOGS = _build()
