"""Collect results/*.json -> results/summary_mean.csv, results/summary_per_dataset.csv, results/summary.xlsx, results/SUMMARY.md"""
import glob
import json
import os
import sys

import re

import pandas as pd

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from common_exp import EXP, LAB  # noqa: E402

GROUPS = {
    "A": "A. Main benchmark: 13 decoders x mat/py, full-trial z-score",
    "B": "B. Causal running z-score comparison (_rz)",
    "C": "C. VLAAI fixes (warm-up _wu10, skip connections _skip)",
    "D": "D. Input window length (_w3s/_w7s/_w10s)",
    "E": "E. Training augmentation (_aug*)",
    "F": "F. FFT band-pass preprocessing (__fft) - NON-COMPLIANT with the lookahead rules (reference only)",
    "G": "G. Signal amplification: onset / sub-band targets (_onset*), InfoNCE (_nce), wider band 1-15 Hz (__fft15, NON-COMPLIANT)",
    "H": "H. Training-seed repeats of the best config (_s1.._s3, step 0)",
}


def group(e):
    if "_onset" in e or "__fft15" in e:
        return "G"
    if "__fft" in e:
        return "F"
    if re.search(r"_s\d+$", e):
        return "H"
    if re.search(r"_w\d+s$", e):
        return "D"
    if "_skip" in e or "_wu" in e:
        return "C"
    if "_aug" in e:
        return "E"
    if e.endswith("_rz"):
        return "B"
    return "A"


DNN = {"simple_cnn", "lsm_cnn", "vlaai", "vlaai_ws", "deformer", "deformer_ws", "ssmamba", "adt", "happyquokka", "direct_clf"}
rows, per, off = [], [], []
for f in sorted(glob.glob(os.path.join(EXP, "results", "*.json"))):
    r = json.load(open(f))
    if "causal" not in r or "experiment" not in r:          # research outputs (integrators, msm) are not experiments
        continue
    la = r.get("lookahead_s", r["causal"]["info"]["AVGC"]["lookahead_s"])      # label stamping actually used for the score
    base = dict(experiment=r["experiment"], group=group(r["experiment"]), decoder=r["decoder"], prep=r["prep"],
                zscore="causal running" if r["prep"].endswith("_rz") else "full-trial",
                type="DNN" if r["decoder"] in DNN else "linear", lookahead_s=la)
    rows.append({**base, **{k: v for k, v in r["causal"]["mean"].items()}})
    for ds, v in r["causal"]["per_dataset"].items():
        per.append({**base, "held_out": LAB[ds], **v})
    if "offline_reference_NONCOMPLIANT" in r:
        off.append({**base, "lookahead_s": 3.75, **r["offline_reference_NONCOMPLIANT"]["mean"]})
cols = ["Acc", "SDT", "L", "H", "miss_rate", "null_L", "null_H", "acc1", "acc10", "snr1"]
M = pd.DataFrame(rows)
if len(M):
    M = M.sort_values("H", ascending=False)
    M.to_csv(os.path.join(EXP, "results", "summary_mean.csv"), index=False)
    P = pd.DataFrame(per)
    P.to_csv(os.path.join(EXP, "results", "summary_per_dataset.csv"), index=False)
    O = pd.DataFrame(off)
    with pd.ExcelWriter(os.path.join(EXP, "results", "summary.xlsx")) as w:
        M.round(4).to_excel(w, sheet_name="MEAN_over_heldout", index=False)
        P.round(4).to_excel(w, sheet_name="per_heldout_dataset", index=False)
        if len(O):
            O.round(4).to_excel(w, sheet_name="DNN_offline_NONCOMPLIANT", index=False)
        for g in sorted(M.group.unique()):                   # one sheet per experiment group
            M[M.group == g].round(4).to_excel(w, sheet_name=f"group_{g}", index=False)
    # catalog: every experiment by group, with its MEAN scores (results/CATALOG.md)
    with open(os.path.join(EXP, "results", "CATALOG.md"), "w") as fh:
        fh.write("# Experiment catalog (auto-generated by summarize.py)\n\n"
                 "MEAN over the 5 LODO held-out datasets, synthetic switches, HMM p_stay 0.998. Best per group in bold.\n\n")
        for g in sorted(M.group.unique()):
            t = M[M.group == g].sort_values("H", ascending=False)
            fh.write(f"## {GROUPS[g]}\n\n| experiment | Acc | SDT | L | H | SNR1 |\n|---|---|---|---|---|---|\n")
            for i, (_, x) in enumerate(t.iterrows()):
                b = "**" if i == 0 else ""
                fh.write(f"| {b}{x.experiment}{b} | {x.Acc:.2f} | {x.SDT:.2f} | {x.L:.3f} | {b}{x.H:.3f}{b} | {x.snr1:.3f} |\n")
            fh.write("\n")
        rs = sorted(glob.glob(os.path.join(EXP, "results", "research", "*")))
        if rs:
            fh.write("## Research outputs (results/research/)\n\n" + "".join(f"- `{os.path.basename(f)}`\n" for f in rs))
    with open(os.path.join(EXP, "results", "SUMMARY.md"), "w") as fh:
        fh.write("# LODO decoder benchmark: MEAN over the 5 held-out datasets\n\n")
        fh.write("SYNTHETIC switches, HMM p_stay 0.998 per 0.25-s step, official scorer (Python port). "
                 "Acc in %, SDT in s. acc1/acc10 = plain window accuracy (median over subjects), snr1 = 1-s evidence SNR.\n\n")
        t = M[["experiment", "type", "zscore", "lookahead_s"] + cols].round(3)
        fh.write("| " + " | ".join(t.columns) + " |\n|" + "---|" * len(t.columns) + "\n")
        for _, x in t.iterrows():
            fh.write("| " + " | ".join(str(v) for v in x.values) + " |\n")
    print(M[["experiment"] + cols].round(3).to_string(index=False))
