import Foundation

/// Tiny lookup for the most common ICAO airline codes → IATA + display name.
/// Used to enrich live-tracker callsigns (e.g. "DAL1234" → "Delta", IATA "DL").
///
/// Not exhaustive — about 80 entries covering the carriers we encounter
/// most. Anything not in the table falls back to the raw 3-letter ICAO code.
/// If this grows past ~200, switch to a bundled JSON.
enum AircraftRegistry {
    /// ICAO airline code → (IATA, display name).
    static let airlines: [String: (iata: String, name: String)] = [
        // US legacy + major
        "AAL": ("AA", "American Airlines"),
        "DAL": ("DL", "Delta Air Lines"),
        "UAL": ("UA", "United Airlines"),
        "SWA": ("WN", "Southwest Airlines"),
        "ASA": ("AS", "Alaska Airlines"),
        "JBU": ("B6", "JetBlue"),
        "SCX": ("SY", "Sun Country Airlines"),
        "HAL": ("HA", "Hawaiian Airlines"),
        "FFT": ("F9", "Frontier Airlines"),
        "AAY": ("G4", "Allegiant Air"),
        "JIA": ("OH", "PSA Airlines"),
        "ASH": ("YX", "Mesa Airlines"),
        "ENY": ("MQ", "Envoy Air"),
        "RPA": ("YX", "Republic Airways"),
        "SKW": ("OO", "SkyWest"),
        "EDV": ("9E", "Endeavor Air"),
        "GJS": ("OO", "GoJet"),
        "JSX": ("XE", "JSX"),

        // Canada
        "ACA": ("AC", "Air Canada"),
        "WJA": ("WS", "WestJet"),
        "JZA": ("QK", "Jazz Aviation"),

        // Mexico / LatAm
        "AMX": ("AM", "Aeromexico"),
        "VOI": ("Y4", "Volaris"),
        "VIV": ("VB", "Viva Aerobus"),
        "AVA": ("AV", "Avianca"),
        "GLO": ("G3", "GOL"),
        "AZU": ("AD", "Azul"),
        "LAN": ("LA", "LATAM"),
        "TAM": ("JJ", "LATAM Brasil"),
        "CMP": ("CM", "Copa Airlines"),

        // Europe
        "BAW": ("BA", "British Airways"),
        "DLH": ("LH", "Lufthansa"),
        "AFR": ("AF", "Air France"),
        "KLM": ("KL", "KLM"),
        "IBE": ("IB", "Iberia"),
        "AEA": ("UX", "Air Europa"),
        "SWR": ("LX", "Swiss"),
        "AUA": ("OS", "Austrian Airlines"),
        "BEL": ("SN", "Brussels Airlines"),
        "SAS": ("SK", "SAS"),
        "FIN": ("AY", "Finnair"),
        "TAP": ("TP", "TAP Portugal"),
        "VIR": ("VS", "Virgin Atlantic"),
        "EZY": ("U2", "EasyJet"),
        "RYR": ("FR", "Ryanair"),
        "WZZ": ("W6", "Wizz Air"),
        "AZA": ("AZ", "ITA Airways"),
        "THY": ("TK", "Turkish Airlines"),
        "AEE": ("A3", "Aegean Airlines"),
        "ROT": ("RO", "TAROM"),
        "LOT": ("LO", "LOT Polish"),
        "CSA": ("OK", "Czech Airlines"),

        // Middle East
        "UAE": ("EK", "Emirates"),
        "ETD": ("EY", "Etihad Airways"),
        "QTR": ("QR", "Qatar Airways"),
        "SVA": ("SV", "Saudia"),
        "KAC": ("KU", "Kuwait Airways"),
        "ELY": ("LY", "El Al"),

        // Asia / Pacific
        "ANA": ("NH", "All Nippon Airways"),
        "JAL": ("JL", "Japan Airlines"),
        "KAL": ("KE", "Korean Air"),
        "AAR": ("OZ", "Asiana"),
        "CCA": ("CA", "Air China"),
        "CSN": ("CZ", "China Southern"),
        "CES": ("MU", "China Eastern"),
        "CPA": ("CX", "Cathay Pacific"),
        "SIA": ("SQ", "Singapore Airlines"),
        "THA": ("TG", "Thai Airways"),
        "MAS": ("MH", "Malaysia Airlines"),
        "PAL": ("PR", "Philippine Airlines"),
        "GIA": ("GA", "Garuda Indonesia"),
        "QFA": ("QF", "Qantas"),
        "ANZ": ("NZ", "Air New Zealand"),
        "VOZ": ("VA", "Virgin Australia"),
        "JST": ("JQ", "Jetstar"),
        "AIC": ("AI", "Air India"),
        "IGO": ("6E", "IndiGo"),

        // Cargo
        "FDX": ("FX", "FedEx"),
        "UPS": ("5X", "UPS Airlines"),
        "GTI": ("5Y", "Atlas Air"),
        "ABX": ("GB", "ABX Air"),
        "GEC": ("LH", "Lufthansa Cargo"),
        "CKS": ("K4", "Kalitta Air")
    ]

    /// Look up display info for an ICAO code. Returns the raw code as the
    /// name if unknown so the UI never shows blank.
    static func airline(forICAO code: String?) -> (iata: String?, name: String) {
        guard let code = code?.uppercased() else { return (nil, "Unknown") }
        if let hit = airlines[code] { return (hit.iata, hit.name) }
        return (nil, code)
    }
}
