"""Measures of punctuality, computed from the counts in data/punctuality.parquet.

Every measure is a ratio of sums of counts, so it can be computed for any group
of rows (an airport, an airline, a route, a month):

    on_time_pct     flights no more than 15 minutes late, as a percentage of the
                    planned flights (cancelled flights are not on time), as the
                    CAA defines it
    avg_delay       the mean delay of the operated flights in minutes, with early
                    flights counted as zero
    cancelled_pct   cancelled flights, as a percentage of the planned flights
    late_60_pct     flights more than an hour late, as a percentage of the planned flights
    late_180_pct    flights more than three hours late, likewise
"""

from pathlib import Path

import numpy as np
import pandas as pd

from build_data import AIRPORTS, ON_TIME, OUTPUT, place

LATE_60 = ["late_61_120", "late_121_180", "late_181_360", "late_360"]
LATE_180 = ["late_181_360", "late_360"]
COUNTS = ["planned", "operated", "on_time", "cancelled", "late_60", "late_180", "delay_min"]


def load(path: Path = OUTPUT) -> pd.DataFrame:
    df = pd.read_parquet(path)
    df["on_time"] = df[ON_TIME].sum(axis=1)
    df["late_60"] = df[LATE_60].sum(axis=1)
    df["late_180"] = df[LATE_180].sum(axis=1)
    return df


def measures(counts: pd.DataFrame) -> pd.DataFrame:
    """Add the measures to a table of summed counts."""
    c = counts
    return c.assign(
        on_time_pct=100 * c.on_time / c.planned,
        avg_delay=c.delay_min / c.operated.where(c.operated > 0),
        cancelled_pct=100 * c.cancelled / c.planned,
        late_60_pct=100 * c.late_60 / c.planned,
        late_180_pct=100 * c.late_180 / c.planned,
    )


def summarise(df: pd.DataFrame, by) -> pd.DataFrame:
    return measures(df.groupby(by, observed=True)[COUNTS].sum())


def last_months(df: pd.DataFrame, n: int = 12) -> pd.DataFrame:
    """The rows for the latest n months."""
    last = df.month.max()
    return df[df.month > last - pd.DateOffset(months=n)]


def wilson(k, n, z: float = 1.96):
    """The Wilson score interval (as fractions) for k successes in n trials."""
    k, n = np.asarray(k, float), np.asarray(n, float)
    p = k / n
    centre = (p + z**2 / (2 * n)) / (1 + z**2 / n)
    half = z * np.sqrt(p * (1 - p) / n + z**2 / (4 * n**2)) / (1 + z**2 / n)
    return centre - half, centre + half


def compared_with_others(df: pd.DataFrame, unit: str, cell: list[str], min_others: int = 20) -> pd.DataFrame:
    """How much more often each unit's flights are on time than other units' flights
    in the same cells, by indirect standardisation.

    For an airline (unit="brand") the cells are an airport, direction and month,
    so its flights are compared only with other airlines' flights at the same
    airports at the same times; for an airport (unit="airport") the cells are an
    airline, direction and month. The expected on-time fraction of each of the
    unit's flights is that of the other units' flights in its cell; cells in which
    the others have fewer than min_others flights are left out.

    Returns, for each unit, its planned flights in the cells used, its on-time
    percentage there (actual), the percentage expected from the other units in
    the same cells (expected), and difference = actual - expected, in points.
    """
    keys = [unit, *cell]
    u = df.groupby(keys, observed=True)[["planned", "on_time"]].sum()
    u = u[u.planned > 0]
    totals = df.groupby(cell, observed=True)[["planned", "on_time"]].sum()
    u = u.join(totals, on=cell, rsuffix="_all")
    others_planned = u.planned_all - u.planned
    u["p_others"] = (u.on_time_all - u.on_time) / others_planned.where(others_planned > 0)
    u = u[others_planned >= min_others]
    u["expected"] = u.planned * u.p_others
    r = u.groupby(level=unit, observed=True)[["planned", "on_time", "expected"]].sum()
    return pd.DataFrame(
        {
            "planned": r.planned,
            "actual": 100 * r.on_time / r.planned,
            "expected": 100 * r.expected / r.planned,
            "difference": 100 * (r.on_time - r.expected) / r.planned,
        }
    )


def domestic_recovery(df: pd.DataFrame, min_flights: int = 300) -> pd.DataFrame:
    """Domestic routes between two reporting airports, which appear at both ends:
    the mean delay on departure (at the origin) and on arrival (at the
    destination) for each route and airline brand."""
    # The CAA names the reporting airports as destinations as it does as airports.
    names = {place(k): v[0] for k, v in AIRPORTS.items()}
    dom = df[df.country == "United Kingdom"].copy()
    dom["other"] = dom.destination.astype(str).map(names)
    dom = dom[dom.other.notna()]
    dep = dom[dom.direction == "D"].groupby(["airport", "other", "brand"], observed=True)[["operated", "delay_min"]].sum()
    arr = dom[dom.direction == "A"].groupby(["other", "airport", "brand"], observed=True)[["operated", "delay_min"]].sum()
    dep.index.names = arr.index.names = ["origin", "destination", "brand"]
    j = dep.join(arr, lsuffix="_dep", rsuffix="_arr", how="inner")
    j = j[(j.operated_dep >= min_flights) & (j.operated_arr >= min_flights)]
    return pd.DataFrame(
        {
            "flights": (j.operated_dep + j.operated_arr) / 2,
            "dep_delay": j.delay_min_dep / j.operated_dep,
            "arr_delay": j.delay_min_arr / j.operated_arr,
        }
    )


def explorer_rows(df: pd.DataFrame) -> dict:
    """Counts for each airport, direction, destination and operator, for the post's
    explorer, as columns of numbers: the airports, places (destination and
    country) and airlines (operator, brand and group) are indices into lists."""
    keys = ["airport", "direction", "destination", "country", "operator", "brand", "group"]
    g = df.groupby(keys, observed=True)[COUNTS].sum().reset_index()
    g = g[g.planned > 0]
    for k in keys:
        g[k] = g[k].astype(str)
    airports = sorted(g.airport.unique())
    places = sorted(set(zip(g.destination, g.country)))
    airlines = sorted(set(zip(g.operator, g.brand, g.group)))
    index = {
        "airport": {v: i for i, v in enumerate(airports)},
        "place": {v: i for i, v in enumerate(places)},
        "airline": {v: i for i, v in enumerate(airlines)},
    }
    columns = {
        "airport": g.airport.map(index["airport"]).tolist(),
        "arrival": (g.direction == "A").astype(int).tolist(),
        "place": [index["place"][v] for v in zip(g.destination, g.country)],
        "airline": [index["airline"][v] for v in zip(g.operator, g.brand, g.group)],
    }
    for c in COUNTS:
        columns[c] = g[c].round().astype(int).tolist()
    return {
        "airports": airports,
        "places": [list(p) for p in places],
        "airlines": [list(a) for a in airlines],
        "columns": columns,
    }
