"""End-to-end compliance test of the submission pipeline on real test trials (sub_convert -> sub_evidence -> fusion ->
CSV), for eq3, ref_t_bm, ref_t_bm_stem20, ref_t_bm_stem50 and b3v2.

Variants of a few blind-test trials (3 DLAAD, 2 NJU), built from the organisers' files with the conversion of
sub_convert.py, except that the two whole-trial operations the rules exempt (the organisers' zero-phase filter of
pre_*, the full-trial z-score) are FROZEN at their values on the original trial, so that every remaining step must be
causal on its own:
  BASE      the original trial
  CUT<j>    EEG (32-Hz pre_*), 32-Hz envelopes and 128-Hz envelopes replaced by noise from trial time Tc on
            (Tc = 25 / 50 / 80 % of the trial). Rule: every label at t < Tc - 1 s must be unchanged.
  SWAP      the two streams exchanged (32-Hz and 128-Hz envelopes). Expected: every decided label flips.
Each variant is a converted set external_data/LEAK_<variant>; the evidence of all 12 models is computed for it
(sub_evidence.py) and every fusion writes its CSVs (test_phase/leak/<combo>/<guess>/<variant>), with the organisers'
guess and with SUB_GUESS=causal. 'compare' reports for each combo / guess:
  cut   share of samples at t < Tc - 1 s whose label changed (must be 0), split into the guessed start of the trial
        and the decided part; earliest changed sample relative to Tc
  swap  share of decided samples that flip (1 = column-symmetric); guessed samples are not expected to flip
usage: python sub_leakage.py build ; (evidence + predictions: runners/sub_leakage.sh) ; python sub_leakage.py compare
"""
import json
import os
import sys

HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)

import numpy as np  # noqa: E402
import pandas as pd  # noqa: E402

import sub_convert as SC  # noqa: E402
from prepare_data import CFG, causal_ea, ea_prior  # noqa: E402
from common import B64U, MONTAGES  # noqa: E402

TP = os.path.abspath(os.path.join(HERE, "..", "..", "test_phase"))
PICK = {"TEST_DLAAD": (os.path.join(TP, "dlaad", "dataCND"), [(1, 0), (11, 3), (41, 5)]),
        "TEST_NJU": (os.path.join(TP, "nju", "dataCND"), [(2, 0), (9, 4)])}
CUTS = (0.25, 0.5, 0.8)
VARIANTS = ["BASE"] + [f"CUT{j}" for j in range(len(CUTS))] + ["SWAP"]
COMBOS = ("eq3", "ref_t_bm", "ref_t_bm_stem20", "ref_t_bm_stem50", "b3v2")
OUT = os.path.join(TP, "leak")


def zs_stats(x):
    sd = x.std(0, ddof=1); sd[sd == 0] = 1.0
    return x.mean(0), sd


def convert(eeg, labels, env, e128, stats):
    """sub_convert.preprocess with the full-trial z-score statistics given (frozen)."""
    pos = {c.upper(): i for i, c in enumerate(labels)}
    src = np.array([pos.get(c, -1) for c in B64U]); present = src >= 0
    x = np.zeros((eeg.shape[0], 64)); x[:, present] = eeg[:, src[present]]
    sel = np.array([B64U.index(c) for c in MONTAGES[CFG["montage"]]]); x, pres = x[:, sel], present[sel]
    x[:, pres] = x[:, pres] - x[:, pres].mean(1, keepdims=True)
    n128 = len(e128); n = min(len(x), len(env), -(-n128 // 4))
    x, env = x[SC.TRIM:n - SC.TRIM], env[SC.TRIM:n - SC.TRIM]
    if stats is None:
        stats = (zs_stats(x), zs_stats(env))
    (mx, sx), (me, se) = stats
    x, env = (x - mx) / sx, (env - me) / se
    P = CFG["preps"][SC.PREP]
    x = causal_ea(x, SC.FS, tau_s=P.get("ea_tau", 60.0), R0=ea_prior(P, x), n0=P.get("ea_n0"), shrink=P.get("ea_shrink", 0.1))
    T = len(x); g = SC.BG.features(e128)
    out = dict(eeg=x.astype(np.float32), env=env.astype(np.float32), t0=SC.TRIM / SC.FS, n128=n128, missing=0)
    for k in ("dom", "orel", "odiff"):
        out[k] = g[k][::4][SC.TRIM:SC.TRIM + T].astype(np.float32)
    return out, stats


def build():
    rng = np.random.default_rng(0)
    items = {v: [] for v in VARIANTS}
    meta = {}
    for ds, (d, picks) in PICK.items():
        for sid, t in picks:
            eeg, labels, env, e128 = SC.subject_trials(d, sid)[t]
            key = f"{ds}_S{sid:03d}_T{t:02d}"
            base, stats = convert(eeg, labels, env, e128, None)
            # the ORIGINAL conversion must be reproduced exactly when the statistics come from the trial itself
            ref = np.load(os.path.join(SC.EXT, ds, "cache", SC.PREP, key + ".npz"))
            assert np.abs(ref["eeg"] - base["eeg"]).max() < 1e-5, key
            items["BASE"].append((key, sid, t, base))
            dur = len(e128) / 128.0
            meta[key] = dict(dur=dur, cuts=[c * dur for c in CUTS])
            for j, c in enumerate(CUTS):
                tc = c * dur
                e2, v2, h2 = eeg.copy(), env.copy(), e128.copy()
                i32, i128 = int(np.ceil(tc * 32)), int(np.ceil(tc * 128))
                e2[i32:] = rng.standard_normal(e2[i32:].shape) * eeg.std(0)
                v2[i32:] = rng.permutation(v2[i32:]) + rng.standard_normal(v2[i32:].shape) * env.std(0)
                h2[i128:] = np.abs(rng.standard_normal(h2[i128:].shape)) * np.median(e128, 0)
                items[f"CUT{j}"].append((key, sid, t, convert(e2, labels, v2, h2, stats)[0]))
            (mx, sx), (me, se) = stats
            items["SWAP"].append((key, sid, t, convert(eeg, labels, env[:, ::-1], e128[:, ::-1], ((mx, sx), (me[::-1], se[::-1])))[0]))
    for v, it in items.items():
        SC.write(f"LEAK_{v}", SC.EXT, it)
    os.makedirs(OUT, exist_ok=True)
    json.dump(meta, open(os.path.join(OUT, "meta.json"), "w"), indent=1)
    print("built", {v: len(it) for v, it in items.items()}, flush=True)


def read(path):
    df = pd.read_csv(path)
    return {int(t): g.label.values for t, g in df.groupby("trial")}


def compare():
    import pickle
    from fuse_evidence import LA, OFF
    clf0 = pickle.load(open(os.path.join(HERE, "results", "submission", "LEAK_BASE", "clf_s0.pkl"), "rb"))
    meta = json.load(open(os.path.join(OUT, "meta.json")))
    idx = pd.read_csv(os.path.join(SC.EXT, "LEAK_BASE", "cache", "index.csv"))
    rows = []
    for combo in COMBOS:
        for guess in ("organisers", "causal"):
            L = {}
            for v in VARIANTS:
                L[v] = {}
                for sid in idx.subject.unique():
                    for t, lab in read(os.path.join(OUT, combo, guess, v, f"test_sample_{sid:03d}.csv")).items():
                        L[v][(sid, t)] = lab
            for _, r in idx.iterrows():
                k, sid, t = r["key"], r["subject"], r["trial"]
                b = L["BASE"][(sid, t)]
                first = int(np.ceil((float(clf0[k]["t_end"][OFF]) + LA - 1.0) * 128))   # first decided 128-Hz sample of the fused stream
                for j, tc in enumerate(meta[k]["cuts"]):
                    c = L[f"CUT{j}"][(sid, t)]
                    lim = int(np.floor((tc - 1.0) * 128))
                    ch = np.flatnonzero(b[:lim] != c[:lim])
                    allch = np.flatnonzero(b != c)
                    rows.append(dict(combo=combo, guess=guess, key=k, test=f"cut{j}", tc_s=tc,
                                     changed_before=len(ch), changed_guess=int((ch < first).sum()), changed_decided=int((ch >= first).sum()),
                                     earliest_change_minus_tc_s=float(allch[0] / 128 - tc) if len(allch) else np.nan))
                s = L["SWAP"][(sid, t)]
                rows.append(dict(combo=combo, guess=guess, key=k, test="swap", flip_decided=float(np.mean(s[first:] != b[first:])),
                                 flip_guess=float(np.mean(s[:first] != b[:first]))))
    df = pd.DataFrame(rows)
    df.to_csv(os.path.join(OUT, "leakage_results.csv"), index=False)
    cut = df[df.test.str.startswith("cut")].groupby(["combo", "guess"]).agg(
        trials_x_cuts=("key", "count"), changed_before=("changed_before", "sum"), in_guess=("changed_guess", "sum"),
        in_decided=("changed_decided", "sum"), earliest_minus_tc_s=("earliest_change_minus_tc_s", "min"))
    sw = df[df.test == "swap"].groupby(["combo", "guess"]).agg(flip_decided=("flip_decided", "mean"), flip_guess=("flip_guess", "mean"))
    txt = ("End-to-end leakage test of the submission pipeline (5 blind-test trials, exempt whole-trial operations frozen)\n\n"
           "CUT: labels at t < Tc - 1 s that changed when everything from Tc on was replaced by noise (must be 0);\n"
           "earliest change relative to Tc (s; >= -1 is compliant)\n" + cut.to_string() +
           "\n\nSWAP: share of labels that flip when the two streams are exchanged (decided part; 1 = symmetric)\n" + sw.round(4).to_string() + "\n")
    open(os.path.join(OUT, "leakage_summary.txt"), "w").write(txt)
    print(txt, flush=True)
    bad = cut[cut.in_decided > 0]
    return 1 if len(bad) else 0


if __name__ == "__main__":
    cmd = sys.argv[1]
    if cmd == "build":
        build()
    else:
        sys.exit(compare())
