"""Official Swiss Airports and Aviation Noise Buffers.

Coordinates in Swiss LV95 (EPSG:2056) and WGS84 for:
- Major International Hubs (high frequency jet traffic: ZRH, GVA, BSL)
- Regional Commercial Airports (BRN, LUG, ACH, SIR)
- Military & Airbase Corridors (Emmen, Payerne, Meiringen, Dübendorf)
"""

from __future__ import annotations

import math
from typing import Any, Dict, List, Optional, Tuple

SWISS_AIRPORTS: Dict[str, Dict[str, Any]] = {
    # Major International Hubs (high-frequency commercial jet traffic)
    "Zurich Airport (ZRH)": {
        "easting": 2684218.0,
        "northing": 1256979.0,
        "lat": 47.4582,
        "lon": 8.5555,
        "type": "major",
        "iata": "ZRH",
        "canton": "ZH",
        "municipality": "Kloten",
    },
    "Geneva Airport (GVA)": {
        "easting": 2497454.0,
        "northing": 1121488.0,
        "lat": 46.2370,
        "lon": 6.1092,
        "type": "major",
        "iata": "GVA",
        "canton": "GE",
        "municipality": "Le Grand-Saconnex",
    },
    "EuroAirport Basel-Mulhouse (BSL)": {
        "easting": 2606798.0,
        "northing": 1271038.0,
        "lat": 47.5900,
        "lon": 7.5290,
        "type": "major",
        "iata": "BSL",
        "canton": "BS",
        "municipality": "Basel",
    },
    # Regional Commercial Airports
    "Bern-Belp (BRN)": {
        "easting": 2604454.0,
        "northing": 1195890.0,
        "lat": 46.9141,
        "lon": 7.4971,
        "type": "regional",
        "iata": "BRN",
        "canton": "BE",
        "municipality": "Belp",
    },
    "Lugano-Agno (LUG)": {
        "easting": 2714023.0,
        "northing": 1095819.0,
        "lat": 46.0043,
        "lon": 8.9106,
        "type": "regional",
        "iata": "LUG",
        "canton": "TI",
        "municipality": "Agno",
    },
    "St. Gallen-Altenrhein (ACH)": {
        "easting": 2759927.0,
        "northing": 1261522.0,
        "lat": 47.4850,
        "lon": 9.5608,
        "type": "regional",
        "iata": "ACH",
        "canton": "SG",
        "municipality": "Thal",
    },
    "Sion Airport (SIR)": {
        "easting": 2591370.0,
        "northing": 1118690.0,
        "lat": 46.2196,
        "lon": 7.3268,
        "type": "regional",
        "iata": "SIR",
        "canton": "VS",
        "municipality": "Sion",
    },
    # Military Jet Airbases & Airfields
    "Emmen Airbase": {
        "easting": 2665779.0,
        "northing": 1215995.0,
        "lat": 47.0917,
        "lon": 8.3050,
        "type": "military",
        "iata": "EMN",
        "canton": "LU",
        "municipality": "Emmen",
    },
    "Payerne Airbase": {
        "easting": 2560058.0,
        "northing": 1188129.0,
        "lat": 46.8431,
        "lon": 6.9150,
        "type": "military",
        "iata": "PAY",
        "canton": "VD",
        "municipality": "Payerne",
    },
    "Meiringen Airbase": {
        "easting": 2654000.0,
        "northing": 1175500.0,
        "lat": 46.7420,
        "lon": 8.1100,
        "type": "military",
        "iata": "MIK",
        "canton": "BE",
        "municipality": "Meiringen",
    },
    "Dübendorf Airfield": {
        "easting": 2690000.0,
        "northing": 1250000.0,
        "lat": 47.3980,
        "lon": 8.6480,
        "type": "regional",
        "iata": "DUB",
        "canton": "ZH",
        "municipality": "Dübendorf",
    },
}


def get_airport_table_rows() -> List[Tuple[str, str, str, float, float]]:
    """Return rows for DuckDB swiss_airports table: (name, airport_type, iata, gkode, gkodn)."""
    return [
        (name, info["type"], info["iata"], float(info["easting"]), float(info["northing"]))
        for name, info in SWISS_AIRPORTS.items()
    ]


def calculate_airport_proximity(
    easting: float,
    northing: float,
) -> Dict[str, Any]:
    """Calculate distance to nearest airport and nearest major international hub."""
    nearest_name = None
    nearest_dist_m = float("inf")
    nearest_type = None

    major_name = None
    major_dist_m = float("inf")

    for name, info in SWISS_AIRPORTS.items():
        dx = info["easting"] - easting
        dy = info["northing"] - northing
        dist_m = math.sqrt(dx * dx + dy * dy)

        if dist_m < nearest_dist_m:
            nearest_dist_m = dist_m
            nearest_name = name
            nearest_type = info["type"]

        if info["type"] == "major" and dist_m < major_dist_m:
            major_dist_m = dist_m
            major_name = name

    return {
        "nearest_airport": nearest_name,
        "dist_to_airport_km": round(nearest_dist_m / 1000.0, 1) if nearest_name else None,
        "airport_type": nearest_type,
        "nearest_major_airport": major_name,
        "dist_to_major_airport_km": round(major_dist_m / 1000.0, 1) if major_name else None,
    }
