"""Assemble tables/hypotheses_verdicts.csv, tables/per_subject_metrics.csv and the extended
tables/label_audit.csv from the phase outputs. Every number is read from a table produced by the pipeline."""
import os
import sys

import numpy as np
import pandas as pd

sys.path.insert(0, os.path.dirname(__file__))
from common import CACHE, DS_KEYS, LABELS, TAB, get_logger  # noqa: E402

log = get_logger("f1_verdicts")
MAIN = "c19-common57-car"


def T(name):
    return pd.read_csv(os.path.join(TAB, name))


def f(x, nd=3):
    return f"{x:.{nd}g}" if isinstance(x, (float, np.floating)) else str(x)


if __name__ == "__main__":
    qc = T("qc_dataset_summary.csv").set_index("ds")
    inf = T("prefilter_inference.csv").set_index("ds")
    asym = T("env_asymmetry_summary.csv").set_index("ds")
    loud = T("env_loud_column_classifier.csv")
    par = T("design_parity.csv").set_index("ds")
    drift = T("drift_classifier_summary.csv")
    snr = T("decoder_snr_law.csv")
    accw = T("decoder_accuracy_vs_window.csv")
    xh = T("crosshalf_h6.csv")
    h9 = T("stream_order_audit.csv")
    h9s = T("stream_order_audit_subjects.csv")
    fp = T("fingerprint_results.csv")
    phys = T("physiology_avgc_switch_locked.csv")
    physd = T("physiology_direction_effects.csv")
    lab = T("label_audit_summary.csv").set_index("ds")
    pc = T("decoder_pipeline_comparison.csv")
    sp = T("spatial_summary.csv") if os.path.exists(os.path.join(TAB, "spatial_summary.csv")) else None
    V = []

    # H1
    sd = {d: qc.loc[d, "median_sd"] for d in DS_KEYS}
    rr = {d: qc.loc[d, "median_rms_sd"] for d in DS_KEYS}
    V.append(dict(id="H1", hypothesis="Amplitude scale differs ~16x; DTU, KUL-UHD, USTC carry large DC offsets",
                  previous="SD 8.3/39.2/9.6/132.1/61.5; RMS/SD 1/210/1/58/94",
                  full_corpus="median channel SD " + "/".join(f(sd[d], 3) for d in DS_KEYS) + " (max/min "
                  + f(max(sd.values()) / min(sd.values()), 3) + "x); RMS/SD " + "/".join(f(rr[d], 3) for d in DS_KEYS)
                  + "; subject-level 95% CIs in qc_dataset_summary.csv",
                  n="1979 trials / 95 subjects (149,336 channel-trials)", verdict="confirmed",
                  evidence="tables/qc_dataset_summary.csv; figures/a1_qc_metric_distributions.png"))
    # H2
    V.append(dict(id="H2", hypothesis="Different pre-filtering: KUL-AV-GC LP ~40 Hz + HP <1 Hz; strong 50 Hz in KUL-UHD, USTC",
                  previous="line ratio 10.8 (UHD), 9.3 (USTC)",
                  full_corpus=f"KUL-AV-GC roll-off {f(qc.loc['AVGC','rolloff_hz'])} Hz in {f(qc.loc['AVGC','share_rolloff_found']*100)}% of trials, "
                  f"sub-1 Hz ratio {f(qc.loc['AVGC','sub1_ratio'])} (HP ~1 Hz), 50 Hz notched (line ratio {f(qc.loc['AVGC','line_ratio_50'])}); "
                  f"50 Hz line ratio UHD {f(qc.loc['UHD','line_ratio_50'])} {qc.loc['UHD','line_ratio_50_ci']}, USTC {f(qc.loc['USTC','line_ratio_50'])} "
                  f"{qc.loc['USTC','line_ratio_50_ci']}, DTU {f(qc.loc['DTU','line_ratio_50'])}, KUL2016 {f(qc.loc['KUL','line_ratio_50'])}; "
                  "DTU/UHD/USTC not high-passed (DC offsets); KUL2016 mean-removed",
                  n="1979 trials / 95 subjects", verdict="partly",
                  evidence="KUL-AV-GC part confirmed; 50 Hz present in UHD/USTC but ~3x weaker than the example trial; "
                  "tables/prefilter_inference.csv; figures/a2_psd_median_iqr.png"))
    # H3
    V.append(dict(id="H3", hypothesis="Edge transients at trial start/end in USTC and KUL-UHD; bad channels exist",
                  previous="jumps 9,249 / 13,011 units at t=0; UHD P7 SD 2,381 vs Cz 71",
                  full_corpus="trials with a >20 robust-SD jump within 0.5 s of an edge: "
                  + ", ".join(f"{LABELS[d]} {f(qc.loc[d,'share_edge_transient_trials']*100)}%" for d in DS_KEYS)
                  + "; bad-channel share " + ", ".join(f"{LABELS[d]} {f(qc.loc[d,'share_bad_channels']*100)}%" for d in DS_KEYS)
                  + f"; bad trials (>=10% bad/flat ch.) UHD {f(qc.loc['UHD','share_bad_trials']*100)}%",
                  n="1979 trials", verdict="confirmed",
                  evidence="tables/trial_qc.csv; tables/bad_channel_share.csv; figures/a3_bad_channel_topography.png; figures/a4_edge_transients.png"))
    # H4
    s_loto = snr[(snr.scheme == "loto") & (snr.lag == "0-500ms") & (snr.ds == "AVGC")].iloc[0]
    s_lodo = snr[(snr.scheme == "lodo_pool") & (snr.lag == "0-500ms") & (snr.ds == "AVGC")].iloc[0]
    a = accw[(accw.scheme == "loto") & (accw.lag == "0-500ms") & (accw.ds == "AVGC")].set_index("w")
    ks = snr[snr.lag == "0-500ms"].k
    V.append(dict(id="H4", hypothesis="1-s SNR ~0.16 within-subject (KUL-AV-GC); acc ~ Phi(SNR1 sqrt(w)); baseline implies ~0.10",
                  previous="54.5% @1 s, 66.1% @10 s, 88.9% @60 s (n=9 windows)",
                  full_corpus=f"LOTO SNR1 {f(s_loto.snr1_median)} [{f(s_loto.snr1_ci_lo)}, {f(s_loto.snr1_ci_hi)}]; acc "
                  f"{f(a.loc[1,'median_acc']*100)}% @1 s, {f(a.loc[10,'median_acc']*100)}% @10 s, {f(a.loc[60,'median_acc']*100)}% @60 s; "
                  f"fitted k {f(s_loto.k)} (all datasets/schemes {f(ks.min())}-{f(ks.max())}); LODO SNR1 {f(s_lodo.snr1_median)} "
                  f"[{f(s_lodo.snr1_ci_lo)}, {f(s_lodo.snr1_ci_hi)}] vs baseline-implied 0.0989",
                  n="13 subjects / 99 trials (KUL-AV-GC); 95 subjects overall", verdict="confirmed",
                  evidence="k<1: consecutive 1-s windows are not independent; tables/decoder_snr_law.csv; figures/c4_snr_law.png"))
    # H5
    h5 = pc[(pc.cfg == "z19-common57-car") & (pc.scheme == "lodo_pool") & (pc.ds == "ALL")]
    if len(h5):
        txt = "; ".join(f"{r.lag} w={r.w}s: delta acc (zero-phase - causal) {f(r.median_delta_acc_vs_main*100)} pp "
                        f"[{f(r.delta_ci_lo*100)}, {f(r.delta_ci_hi*100)}], Wilcoxon p={f(r.wilcoxon_p)}" for r in h5.itertuples())
        better_zp = (h5.delta_ci_lo > 0).any()
        verdict = ("refuted (zero-phase better)" if better_zp else
                   "confirmed: causal is never worse; zero-phase is slightly worse where significant")
        V.append(dict(id="H5", hypothesis="Causal vs zero-phase filtering makes no systematic difference",
                      previous="within-trial 50-58% @1 s either way", full_corpus="LODO, paired per subject: " + txt,
                      n="95 subjects (LODO)", verdict=verdict,
                      evidence="tables/decoder_pipeline_comparison.csv"))
    # H6
    xb = xh[(xh.w == 60) & (xh.hypothesis.str.startswith("hypB"))].iloc[0]
    xa = xh[(xh.w == 60) & (xh.hypothesis.str.startswith("hypA"))].iloc[0]
    V.append(dict(id="H6", hypothesis="The KUL-AV-GC 5-min switch changes direction, not the attended talker",
                  previous="cross-half: switch hyp. 0.0% @60 s; no-switch hyp. 88.9%",
                  full_corpus=f"CND: {int(lab.loc['AVGC','speaker_switches'])} speaker switches, {int(lab.loc['AVGC','direction_switches'])} direction "
                  f"switches (all at t=300 s) in {int(lab.loc['AVGC','trials'])} trials; cross-half @60 s: no-switch {f(xb.median_acc*100)}% "
                  f"[{f(xb.ci_lo*100)}, {f(xb.ci_hi*100)}] vs switch {f(xa.median_acc*100)}% [{f(xa.ci_lo*100)}, {f(xa.ci_hi*100)}]; "
                  f"trial-level label wrong for {f(lab.loc['AVGC','frac_direction_samples_label_wrong']*100)}% of direction samples",
                  n="13 subjects / 99 trials", verdict="confirmed",
                  evidence="tables/label_audit.csv; tables/crosshalf_h6.csv"))
    # H7
    lk = loud[(loud.scheme == "LOSO")].set_index("ds")
    V.append(dict(id="H7", hypothesis="Attended column has larger envelope SD in KUL2016 and KUL-AV-GC",
                  previous="65% of 320 (p=2.3e-9); 59% of 99 (p=0.0095)",
                  full_corpus=f"KUL2016 {f(asym.loc['KUL','share_col0_larger_sd']*100)}% of 320 (binomial p={f(asym.loc['KUL','binom_p'])}; "
                  f"{int(asym.loc['KUL','n_distinct_ratio_levels'])} distinct ratio levels = 12 stimulus pairs); KUL-AV-GC "
                  f"{f(asym.loc['AVGC','share_col0_larger_sd']*100)}% of 99 (binomial p={f(asym.loc['AVGC','binom_p'])}, Wilcoxon log-ratio "
                  f"p={f(asym.loc['AVGC','wilcoxon_trial_logratio_p'])}); others ~50%. Envelope-only 'loud column' classifier LOSO: KUL2016 "
                  f"{f(lk.loc['KUL','median_acc']*100)}% [{f(lk.loc['KUL','ci_lo']*100)}, {f(lk.loc['KUL','ci_hi']*100)}], KUL-AV-GC "
                  f"{f(lk.loc['AVGC','median_acc']*100)}%, DTU {f(lk.loc['DTU','median_acc']*100)}%, UHD {f(lk.loc['UHD','median_acc']*100)}%, "
                  f"USTC {f(lk.loc['USTC','median_acc']*100)}%",
                  n="1979 trials / 95 subjects", verdict="confirmed (KUL2016); partly (KUL-AV-GC)",
                  evidence="tables/env_asymmetry_summary.csv; tables/env_loud_column_classifier.csv; figures/b4_env_sd_ratio.png"))
    # H8
    dl = drift.set_index(["scheme", "ds"])
    others = ", ".join(f"{LABELS[d]} {f(par.loc[d, 'agree_odd_is_left'] * 100)}%" for d in ("AVGC", "DTU", "KUL"))
    V.append(dict(id="H8", hypothesis="Trial label locked to trial-ID parity in KUL-UHD and USTC",
                  previous="60/60 and 180/180",
                  full_corpus=f"KUL-UHD agreement {f(par.loc['UHD','agree_odd_is_left']*100)}% of 120 trials ({int(par.loc['UHD','subjects_fully_parity_locked'])}/30 subjects "
                  f"fully locked); USTC {f(par.loc['USTC','agree_odd_is_left']*100)}% of 360 ({int(par.loc['USTC','subjects_fully_parity_locked'])}/18); "
                  f"others {others}. "
                  f"Drift-only classifier: LOSO KUL2016 {f(dl.loc[('LOSO','KUL'),'median_acc']*100)}% (p={f(dl.loc[('LOSO','KUL'),'wilcoxon_vs_0p5_p'])}), "
                  f"within-subject DTU {f(dl.loc[('LOTO','DTU'),'median_acc']*100)}% (p={f(dl.loc[('LOTO','DTU'),'wilcoxon_vs_0p5_p'])}); others n.s.",
                  n="1979 trials", verdict="confirmed", evidence="tables/design_parity.csv; tables/drift_classifier_summary.csv"))
    # H9
    u = h9.set_index(["scheme", "ds"])
    V.append(dict(id="H9", hypothesis="USTC columns may be swapped (held-out decoder prefers column 1)",
                  previous="r = 0.083 vs 0.026",
                  full_corpus=f"USTC column 1 wins {f(u.loc[('loso','USTC'),'share_col1_wins']*100)}% of 360 trials (LOSO) and "
                  f"{f(u.loc[('lodo_pool','USTC'),'share_col1_wins']*100)}% (LODO); median r col0 {f(u.loc[('loso','USTC'),'median_r_col0'])} vs col1 "
                  f"{f(u.loc[('loso','USTC'),'median_r_col1'])}; subjects with a column-1 majority at FDR q<0.05: {int((h9s.q < 0.05).sum())}",
                  n="18 USTC subjects / 360 trials; all 95 subjects", verdict="refuted",
                  evidence="USTC is the weakest dataset but its columns are not swapped; tables/stream_order_audit*.csv"))
    # H10
    g = fp[(fp.task == "dataset") & fp.features.str.startswith("cov")].set_index("alignment")
    am = fp[fp.features.str.startswith("amplitude")].set_index("features")
    sea = fp[(fp.task == "subject") & (fp.alignment == "ea")].acc
    sra = fp[(fp.task == "subject") & (fp.alignment == "running_ea")].acc
    V.append(dict(id="H10", hypothesis="Covariance identifies the dataset almost perfectly; EA removes only part",
                  previous="99.7% -> 76.1% (chance 39%)",
                  full_corpus=f"dataset-ID accuracy: none {f(g.loc['none','acc']*100)}%, trace-normalised {f(g.loc['trace','acc']*100)}%, EA "
                  f"{f(g.loc['ea','acc']*100)}%, causal running EA {f(g.loc['running_ea','acc']*100)}% (majority chance "
                  f"{f(g.loc['none','chance_majority']*100)}%); subject-ID (within dataset) after EA {f(sea.min()*100)}-{f(sea.max()*100)}%, after running EA {f(sra.min()*100)}-{f(sra.max()*100)}%; amplitude-only features "
                  f"{f(am.loc['amplitude raw','acc']*100)}% raw, {f(am.loc['amplitude after causal running z','acc']*100)}% after running z",
                  n="30,936 5-s epochs / 1979 trials", verdict="confirmed",
                  evidence="tables/fingerprint_results.csv; figures/d1_embedding.png"))
    # H11
    if sp is not None:
        sp["window_s"] = sp.window_s.astype(str)
        parts = []
        for w in ("5", "1"):
            q = sp[sp.window_s == w].pivot_table(index=["model", "ds"], columns="scheme", values="median_acc")
            gap = (q["within_trial"] - q["LOTO"])
            tid = sp[(sp.scheme == "trialID_within") & (sp.window_s == w)].set_index("ds")
            av = q.loc[("fbcsp", "AVGC")]
            parts.append(f"{w}-s windows: within-trial minus LOTO gap median {f(gap.median()*100)} pp (range {f(gap.min()*100)} to "
                         f"{f(gap.max()*100)}; e.g. KUL-UHD FBCSP {f(q.loc[('fbcsp','UHD'),'within_trial']*100)}% -> {f(q.loc[('fbcsp','UHD'),'LOTO']*100)}%); "
                         f"LODO {f(q['LODO'].min()*100)}-{f(q['LODO'].max()*100)}%; trial-ID accuracy "
                         + ", ".join(f"{LABELS[d]} {f(tid.loc[d,'median_acc']*100)}% (chance {f(tid.loc[d,'chance']*100)}%)" for d in DS_KEYS)
                         + f"; KUL-AV-GC FBCSP within-trial {f(av['within_trial']*100)}%, LOTO {f(av['LOTO']*100)}%, LODO {f(av['LODO']*100)}%")
        V.append(dict(id="H11", hypothesis="Spatial decoders exploit trial fingerprints and collapse under LOTO",
                      previous="not tested (literature: CSP trial-ID ~100%, LOTO 41-45% on KUL-AV-GC)",
                      full_corpus=" || ".join(parts) + ". LOTO is BELOW chance in USTC/KUL-UHD: labels alternate by trial parity, so "
                      "temporally neighbouring trials (similar drift state) carry the opposite label",
                      n="95 subjects; 30,936 5-s and 29,685 1-s windows (<= 24 / 15 per trial)", verdict="confirmed",
                      evidence="tables/spatial_summary.csv; tables/spatial_per_subject.csv; tables/spatial_avgc_by_condition.csv"))
    # H12
    ph = phys.set_index("signal")
    V.append(dict(id="H12", hypothesis="No alpha-lateralisation or EOG signature at the KUL-AV-GC switch",
                  previous="ALI 0.040 -> 0.045 (p=0.77); hEOG p=0.98",
                  full_corpus=f"switch-locked post-minus-pre (99 switches, 13 subjects): ALI p={f(ph.loc['ALI (1-s)','wilcoxon_p'])}, hEOG proxy "
                  f"p={f(ph.loc['hEOG proxy (4 Hz)','wilcoxon_p'])}, EOG channels p>={f(ph[ph.index.str.startswith('EOG')].wilcoxon_p.min())}. "
                  f"Elsewhere: USTC HEO separates left/right trials (d={f(physd[(physd.ds=='USTC')&(physd.feature=='eog0')].median_d.iloc[0])}, "
                  f"q={f(physd[(physd.ds=='USTC')&(physd.feature=='eog0')].fdr_q.iloc[0])})",
                  n="99 switches / 13 subjects", verdict="confirmed",
                  evidence="KUL-AV-GC data are high-passed ~1 Hz, so the <1 Hz EOG proxy is weak there by construction; "
                  "tables/physiology_*.csv; figures/d4_switch_locked.png"))
    vd = pd.DataFrame(V)
    vd.to_csv(os.path.join(TAB, "hypotheses_verdicts.csv"), index=False)
    log.info("\n" + vd[["id", "verdict"]].to_string())

    # ---------------- per-subject metrics (decoders + switch scores + spatial)
    dm = T("per_subject_metrics_decoders.csv")
    wide = dm.pivot_table(index=["cfg", "scheme", "lag", "ds", "subj_uid", "snr1"], columns="w", values="acc").reset_index()
    wide.columns = [f"acc_{c}s" if isinstance(c, (int, np.integer, float)) else c for c in wide.columns]
    sw = T(f"switch_per_subject_{MAIN}_lodo_pool_0-500ms.csv")
    best = T(f"switch_ranking_{MAIN}_lodo_pool_0-500ms.csv").integrator.iloc[0]
    sw = sw[(sw.integrator == best) & (sw.switches == "syn")].rename(columns={"subject": "subj_uid", "acc": "switch_acc_pct",
                                                                               "L": "L_SYNTHETIC", "H": "H_SYNTHETIC", "sdt": "SDT_SYNTHETIC"})
    sw["cfg"], sw["scheme"], sw["lag"] = MAIN, "lodo_pool", "0-500ms"
    out = wide.merge(sw[["cfg", "scheme", "lag", "subj_uid", "switch_acc_pct", "SDT_SYNTHETIC", "L_SYNTHETIC", "H_SYNTHETIC"]],
                     on=["cfg", "scheme", "lag", "subj_uid"], how="left")
    out["integrator_for_L_H"] = np.where(out.L_SYNTHETIC.notna(), best, "")
    out.to_csv(os.path.join(TAB, "per_subject_metrics.csv"), index=False)

    # ---------------- label audit: add the per-trial attended-column check
    la = T("label_audit.csv")
    la = la[[c for c in la.columns if not c.startswith(("r_col0_", "r_col1_", "col0_wins_")) and c not in ("sd_ratio", "col0_more_1_9")]]
    for sc in ("loso", "lodo_pool"):
        t = pd.read_parquet(os.path.join(CACHE, "eval", MAIN, f"{sc}_trial.parquet"))
        t = t[t.lag == "0-500ms"][["key", "r_col0", "r_col1"]].rename(columns={"r_col0": f"r_col0_{sc}", "r_col1": f"r_col1_{sc}"})
        la = la.merge(t, on="key", how="left")
        la[f"col0_wins_{sc}"] = la[f"r_col0_{sc}"] > la[f"r_col1_{sc}"]
    env = T("env_asymmetry_trials.csv")[["key", "sd_ratio", "col0_more_1_9"]]
    la = la.merge(env, on="key", how="left")
    la.to_csv(os.path.join(TAB, "label_audit.csv"), index=False)
    log.info(f"label_audit: {len(la)} trials; per_subject_metrics: {len(out)} rows")
