"""Phase A aggregation: tables/channel_qc.parquet, trial-level PSD features, figures, pre-filter inference."""
import glob
import os
import sys

import matplotlib

matplotlib.use("Agg")
import matplotlib.pyplot as plt  # noqa: E402
import numpy as np  # noqa: E402
import pandas as pd  # noqa: E402
from joblib import Parallel, delayed  # noqa: E402

sys.path.insert(0, os.path.dirname(__file__))
from common import (CACHE, CFG, COLORS, DS_KEYS, LABELS, TAB, boot_ci, get_logger, load_eeg,  # noqa: E402
                    savefig, topo, trial_index)

log = get_logger("a2_qc_report")
Q = CFG["qc"]


def psd_features(f, p):
    m = f > 0
    lf, lp = np.log10(f[m]), np.log10(p[m])
    ff = f[m]
    fit_m = ((ff >= 2) & (ff < 7)) | ((ff > 14) & (ff <= 25))
    b = np.polyfit(lf[fit_m], lp[fit_m], 1)
    resid = lp - np.polyval(b, lf)
    am = (ff >= 7) & (ff <= 14)
    ia = np.argmax(resid[am])
    out = dict(aperiodic_slope=-b[0], alpha_peak_hz=ff[am][ia], alpha_peak_db=10 * resid[am][ia])
    for name, (lo, hi) in Q["bands"].items():
        mm = (f >= lo) & (f < hi)
        out[f"bp_{name}"] = np.trapz(p[mm], f[mm])
    tot = np.trapz(p[(f >= 1) & (f < 45)], f[(f >= 1) & (f < 45)])
    for name in Q["bands"]:
        out[f"rel_{name}"] = out[f"bp_{name}"] / tot
    line = p[(f >= 49.5) & (f <= 50.5)].mean()
    ref = np.concatenate([p[(f >= a) & (f <= c)] for a, c in Q["line_ref_hz"]]).mean()
    out["line_ratio_50"] = line / ref
    out["sub1_ratio"] = p[(f >= 0.25) & (f <= 0.75)].mean() / p[(f >= 1) & (f <= 2)].mean()
    # roll-off: first f >= 25 Hz where PSD sits >10 dB under the aperiodic line for >= 2 Hz (50 Hz +-2 ignored)
    rd = 10 * resid
    ok = (ff >= 25) & ~((ff > 47.5) & (ff < 52.5))
    ro = np.nan
    cand = ff[ok & (rd < -10)]
    for c in cand:
        w = ok & (ff >= c) & (ff <= c + 2)
        if np.all(rd[w] < -10):
            ro = c
            break
    out["rolloff_hz"] = ro
    out["psd_ratio_40_20"] = p[(f >= 38) & (f <= 42)].mean() / p[(f >= 18) & (f <= 22)].mean()
    return out


def ref_check(row):
    """SD of the across-channel mean relative to the median channel SD (0 -> average reference)."""
    x = np.asarray(load_eeg(row["key"])[: 60 * 128], dtype=float)
    x = x - x.mean(0)
    return dict(key=row["key"], car_ratio=float(x.mean(1).std() / np.median(x.std(0))),
                min_ch_sd_ratio=float(x.std(0).min() / np.median(x.std(0))))


if __name__ == "__main__":
    ti = trial_index()
    qc = pd.concat([pd.read_parquet(f) for f in sorted(glob.glob(os.path.join(CACHE, "qc", "*.parquet")))])
    qc = qc.merge(ti[["key", "ds", "subject", "trial", "subj_uid", "dur_s"]], on="key")
    med_sd = qc.groupby("ds", observed=True).sd.transform("median")
    med_rsd = qc.groupby("ds", observed=True).rsd.transform("median")
    qc["flat"] = qc.sd < Q["flat_frac"] * med_sd
    qc["bad"] = qc.rsd > Q["bad_mult"] * med_rsd
    qc["edge_jump"] = (qc.edge_jump_start_rsd > Q["edge_jump_rsd"]) | (qc.edge_jump_end_rsd > Q["edge_jump_rsd"])
    qc.to_parquet(os.path.join(TAB, "channel_qc.parquet"))
    log.info(f"channel_qc: {len(qc)} channel-trials from {qc.key.nunique()} trials")

    # ---------------- trial-level features from the median-channel PSD
    rows, psds = [], []
    for key in ti.key:
        z = np.load(os.path.join(CACHE, "qcpsd", f"{key}.npz"))
        f = z["f"]
        if len(f) != 257:          # trials shorter than 4 s would change the grid (none expected)
            log.warning(f"{key}: PSD grid {len(f)}")
        rows.append(dict(key=key, **psd_features(f, z["psd_med"])))
        psds.append(z["psd_med"])
    F = f
    PS = np.array(psds)
    np.save(os.path.join(CACHE, "psd_median_channel.npy"), PS)
    tf = pd.DataFrame(rows).merge(ti[["key", "ds", "subject", "trial", "subj_uid"]], on="key")
    agg = qc.groupby("key").agg(median_sd=("sd", "median"), median_rsd=("rsd", "median"),
                                median_rms_sd=("rms_sd", "median"), median_abs_dc=("mean", lambda s: np.median(np.abs(s))),
                                median_abs_drift=("drift_per_min", lambda s: np.median(np.abs(s))),
                                median_kurt=("kurt", "median"), median_frac_out5=("frac_out5", "median"),
                                max_jump=("jump_max", "max"), n_ch=("ch", "size"), n_bad=("bad", "sum"),
                                n_flat=("flat", "sum"), n_edge_jump_ch=("edge_jump", "sum"),
                                max_edge_start_rsd=("edge_jump_start_rsd", "max"),
                                max_edge_end_rsd=("edge_jump_end_rsd", "max"))
    tf = tf.merge(agg.reset_index(), on="key")
    tf["frac_bad_or_flat"] = (tf.n_bad + tf.n_flat) / tf.n_ch
    tf["bad_trial"] = tf.frac_bad_or_flat >= 0.10
    tf["edge_transient"] = tf.n_edge_jump_ch > 0
    tf["edge_start"] = tf.max_edge_start_rsd > Q["edge_jump_rsd"]
    tf["edge_end"] = tf.max_edge_end_rsd > Q["edge_jump_rsd"]
    # reference check on 3 trials per subject
    sub = ti.groupby("subj_uid").head(3)
    rc = pd.DataFrame(Parallel(n_jobs=4)(delayed(ref_check)(r) for _, r in sub.iterrows()))
    tf = tf.merge(rc, on="key", how="left")
    tf.to_csv(os.path.join(TAB, "trial_qc.csv"), index=False)

    # ---------------- dataset summary with subject-level CIs
    metrics = ["median_sd", "median_rsd", "median_rms_sd", "median_abs_dc", "median_abs_drift", "median_kurt",
               "median_frac_out5", "max_jump", "aperiodic_slope", "alpha_peak_hz", "alpha_peak_db", "line_ratio_50",
               "sub1_ratio", "rolloff_hz", "psd_ratio_40_20", "rel_delta", "rel_theta", "rel_alpha", "rel_beta",
               "rel_gamma", "frac_bad_or_flat", "car_ratio"]
    out = []
    for ds in DS_KEYS:
        d = tf[tf.ds == ds]
        s = d.groupby("subject")[metrics].median()
        r = dict(ds=ds, n_subjects=len(s), n_trials=len(d))
        for m in metrics:
            r[m] = boot_ci(s[m].values)
        r["share_bad_trials"] = float(d.bad_trial.mean())
        r["share_edge_transient_trials"] = float(d.edge_transient.mean())
        r["share_edge_start"] = float(d.edge_start.mean())
        r["share_edge_end"] = float(d.edge_end.mean())
        r["share_bad_channels"] = float(qc[qc.ds == ds].bad.mean())
        r["share_flat_channels"] = float(qc[qc.ds == ds].flat.mean())
        r["share_rolloff_found"] = float(d.rolloff_hz.notna().mean())
        out.append(r)
    summ = pd.DataFrame(out)
    flat = summ.copy()
    for m in metrics:
        flat[m] = summ[m].map(lambda t: t[0])
        flat[m + "_ci"] = summ[m].map(lambda t: f"[{t[1]:.4g}, {t[2]:.4g}]")
    flat.to_csv(os.path.join(TAB, "qc_dataset_summary.csv"), index=False)
    log.info("\n" + flat[["ds", "n_subjects", "n_trials", "median_sd", "median_rms_sd", "median_abs_dc", "line_ratio_50",
                          "sub1_ratio", "rolloff_hz", "aperiodic_slope", "alpha_peak_hz", "share_bad_trials",
                          "share_edge_transient_trials", "share_bad_channels", "car_ratio"]].to_string())

    # ---------------- pre-filtering inference table
    inf = []
    for _, r in flat.iterrows():
        hp = ("no HP (DC offsets present)" if r.median_rms_sd > 2 else
              ("HP ~1 Hz or higher" if r.sub1_ratio < 0.5 else "HP < 1 Hz (or none, DC removed)"))
        lp = (f"LP, roll-off ~{r.rolloff_hz:.1f} Hz" if np.isfinite(r.rolloff_hz) and r.rolloff_hz < 60 else
              "no LP below 60 Hz (only the resampling anti-alias at the 64 Hz Nyquist)")
        notch = ("notch at 50 Hz" if r.line_ratio_50 < 0.5 else ("strong 50 Hz line" if r.line_ratio_50 > 3 else
                                                                 "no notable 50 Hz line"))
        ref = "average reference (channel mean ~0)" if r.car_ratio < 0.05 else "not average-referenced"
        inf.append(dict(ds=r.ds, median_rms_over_sd=r.median_rms_sd, sub1_ratio=r.sub1_ratio, rolloff_hz=r.rolloff_hz,
                        share_trials_with_rolloff=r.share_rolloff_found, line_ratio_50=r.line_ratio_50,
                        car_ratio=r.car_ratio, inferred_highpass=hp, inferred_lowpass=lp, inferred_notch=notch,
                        inferred_reference=ref))
    inf = pd.DataFrame(inf)
    inf.to_csv(os.path.join(TAB, "prefilter_inference.csv"), index=False)
    log.info("\n" + inf.to_string())

    # ---------------- figures
    # A1 metric distributions (one dot per trial, median per subject as black ticks)
    show = [("median_sd", True), ("median_rms_sd", True), ("median_abs_dc", True), ("median_abs_drift", True),
            ("median_kurt", True), ("median_frac_out5", False), ("max_jump", True), ("aperiodic_slope", False),
            ("alpha_peak_hz", False), ("alpha_peak_db", False), ("line_ratio_50", True), ("sub1_ratio", True),
            ("rolloff_hz", False), ("rel_alpha", False), ("frac_bad_or_flat", False), ("car_ratio", True)]
    fig, axs = plt.subplots(4, 4, figsize=(16, 13))
    rng = np.random.default_rng(0)
    for ax, (m, lg) in zip(axs.ravel(), show):
        for i, ds in enumerate(DS_KEYS):
            v = tf[tf.ds == ds][m].values.astype(float)
            v = v[np.isfinite(v)]
            if lg:
                v = v[v > 0]
            if len(v) > 1:
                parts = ax.violinplot([np.log10(v) if lg else v], [i], widths=0.8, showextrema=False)
                for pc in parts["bodies"]:
                    pc.set_facecolor(COLORS[ds])
                    pc.set_alpha(0.35)
            ax.scatter(i + (rng.random(len(v)) - 0.5) * 0.3, np.log10(v) if lg else v, s=2, color=COLORS[ds])
            ax.plot([i - 0.35, i + 0.35], [np.median(np.log10(v) if lg else v)] * 2 if len(v) else [np.nan] * 2, "k-")
        ax.set_xticks(range(len(DS_KEYS)), [LABELS[d] for d in DS_KEYS], fontsize=7)
        ax.set_title(("log10 " if lg else "") + m, fontsize=9)
    fig.suptitle("Phase A trial-level QC metrics (dot = trial; black bar = median). n trials: " +
                 ", ".join(f"{LABELS[d]} {int((tf.ds == d).sum())}" for d in DS_KEYS), fontsize=10)
    savefig(fig, "a1_qc_metric_distributions", tf)

    # A2 median PSD with IQR
    fig, ax = plt.subplots(figsize=(8, 5))
    pr = []
    for ds in DS_KEYS:
        m = (tf.set_index("key").loc[ti.key].ds == ds).values
        P = PS[m]
        q1, q2, q3 = np.quantile(P, [0.25, 0.5, 0.75], axis=0)
        ax.fill_between(F[1:], q1[1:], q3[1:], color=COLORS[ds], alpha=0.2)
        ax.loglog(F[1:], q2[1:], color=COLORS[ds], label=f"{LABELS[ds]} (n={m.sum()} trials)")
        pr.append(pd.DataFrame(dict(ds=ds, f=F, q25=q1, median=q2, q75=q3)))
    ax.set_xlabel("frequency (Hz)")
    ax.set_ylabel("PSD (stored units$^2$/Hz), median over channels")
    ax.set_title("Raw 128 Hz EEG: median-channel Welch PSD (line = median over trials, band = IQR)")
    ax.legend(fontsize=8)
    ax.grid(alpha=0.3, which="both")
    savefig(fig, "a2_psd_median_iqr", pd.concat(pr))

    # A3 per-channel bad share: topography for B64 datasets, scatter for KUL-UHD
    pos = pd.read_csv(os.path.join(CACHE, "b64_pos.csv")).set_index("ch")
    fig, axs = plt.subplots(1, 5, figsize=(20, 4.2))
    bc = qc.groupby(["ds", "ch_up"], observed=True).agg(bad=("bad", "mean"), flat=("flat", "mean"),
                                                         edge=("edge_jump", "mean"), n=("key", "size")).reset_index()
    bc.to_csv(os.path.join(TAB, "bad_channel_share.csv"), index=False)
    for ax, ds in zip(axs, DS_KEYS):
        b = bc[bc.ds == ds].set_index("ch_up")
        if ds == "UHD":
            loc = pd.read_csv(os.path.join(CFG["paths"]["root"], "data/cnd/KUL_AAD_UltraHD2021/eeg255ch_locs.csv"))
            loc["ch"] = loc["Channel Name"].str.upper()
            loc = loc.set_index("ch").reindex(b.index)
            # BESA spherical coordinates: signed polar angle theta from Cz, azimuth phi
            r_ = loc.Theta.values / 180.0
            x, y = r_ * np.cos(np.deg2rad(loc.Phi.values)), r_ * np.sin(np.deg2rad(loc.Phi.values))
            sc = ax.scatter(x, y, c=b.bad.values, cmap="Reds", vmin=0, vmax=max(0.05, b.bad.max()), s=18)
            ax.set_aspect("equal")
            ax.axis("off")
            plt.colorbar(sc, ax=ax, fraction=0.04)
        else:
            chs = [c for c in b.index if c in pos.index]
            topo(ax, pos.loc[chs, "x"].values, pos.loc[chs, "y"].values, b.loc[chs, "bad"].values, cmap="Reds",
                 vmin=0, vmax=max(0.05, b.bad.max()), names=None)
        ax.set_title(f"{LABELS[ds]}: share of trials channel is bad\n(n={int((ti.ds == ds).sum())} trials)", fontsize=9)
    savefig(fig, "a3_bad_channel_topography", bc)

    # A4 edge transients
    fig, ax = plt.subplots(figsize=(7, 3.5))
    et = tf.groupby("ds", observed=True)[["edge_start", "edge_end", "edge_transient"]].mean().reindex(DS_KEYS)
    x = np.arange(len(DS_KEYS))
    ax.bar(x - 0.2, et.edge_start, 0.4, label="within 0.5 s of start", color=[COLORS[d] for d in DS_KEYS])
    ax.bar(x + 0.2, et.edge_end, 0.4, label="within 0.5 s of end", color=[COLORS[d] for d in DS_KEYS], alpha=0.5)
    ax.set_xticks(x, [f"{LABELS[d]}\n(n={int((tf.ds == d).sum())})" for d in DS_KEYS])
    ax.set_ylabel("share of trials")
    ax.set_title(f"Trials with a first-difference jump > {Q['edge_jump_rsd']} robust SD in any channel")
    ax.legend(fontsize=8)
    savefig(fig, "a4_edge_transients", et.reset_index())
    log.info("\n" + et.to_string())
    log.info("done")
