"""Lookahead (leakage), column-swap and position-invariance checks of the CURRENT best pipeline with the B1 decision rule.

Pipeline under test (as submitted): causal EA (tau 60 s) -> 4-block vlaai_ws decoder (env, onset, dom; SparrKULee-pretrained s0 weights)
+ 3-block 10-s direct classifier (fold weights of the held-out dataset, trained with sw2) -> fusion a = 0.5 at DEC_OFFSET -> decision rule:
HMM p_stay 0.998 | B1 semi-Markov (several (mean dwell, shape)) | NEGATIVE: B1 run backwards in time. Constants of the test are fixed numbers
(the checks are structural: LDA weights / scales / emissions are fitted on other data in the pipeline).
Parts
  A  lookahead by perturbation (as leakage_test.py: replace everything at >= tau by noise; first 128-Hz label that changes; must be <= 1 s),
     with the B1 filter in the chain; negative controls must be flagged.
  B  column swap: exchange the two streams (envelope, raw envelope -> dom). Evidence and posterior must flip exactly: d' = -d, P' = 1 - P.
  C  position invariance
     C1  EEG of one trial with the stimuli of ANOTHER trial (no real attention): the share of steps that favour column 0 must be 0.5 at every
         position of the trial (0-5, 5-15, 15-30, 30-60 s), i.e. no learned "column 0 first" prior (the stem_ms96 failure: 99.9 %).
     C2  the same trial without its first 20 s (EEG, stimuli cut after the exempted z-score): evidence 30 s after the cut vs the full run
         (informational: causal EA has a 60-s memory, so exact equality is not expected).
  D  decision rule alone on pure noise evidence: antisymmetry of every filter and the share of P > 0.5 per position.
usage: GLIMPSE_THR=norm python leakage_test_b1.py -> results/research/leakage_b1.csv, leakage_b1.txt; exit 1 on failure"""
import os
import sys

import numpy as np
import pandas as pd
import torch

import leakage_test as L
from leakage_test import BG, DC, FS, decoders, zscore_trial, causal_ea
from common import DS_KEYS, load_env, load_pp
from common_exp import CFG, EXP, evidence_from_features, rows_of
from e1_switch import BUDGET, expand, hmm_filter
from fuse_evidence import LA, OFF
from hmm_semimarkov import dwell_prior, sm_filter
from run_experiment import init_net

W_LDA, SD_D, SD_C, A_MIX, P_STAY = np.array([0.06, 0.59, 0.35]), 0.2, 2.0, 0.5, 0.998
B1 = {"B1 m20 k0.6": (20.0, 0.6), "B1 m10 k0.6": (10.0, 0.6), "B1 m5 k1.5": (5.0, 1.5)}
NEG = "NEGATIVE CONTROL: "
out_lines = []


def say(s=""):
    print(s, flush=True)
    out_lines.append(s)


def llr(d):
    return -0.5 * (d - 0.5) ** 2, -0.5 * (d + 0.5) ** 2


def posterior(d, kind):
    ll0, ll1 = llr(d)
    if kind == "hmm":
        return hmm_filter(ll0, ll1, P_STAY)
    if kind == "sm_rev":                                    # anti-causal on purpose
        h, pi = dwell_prior(*B1["B1 m20 k0.6"])
        return sm_filter(ll0[::-1].copy(), ll1[::-1].copy(), h, pi)[::-1]
    h, pi = dwell_prior(*B1[kind])
    return sm_filter(ll0, ll1, h, pi)


def evidences(x_z, env_z, raw128, t0, dec, clf):
    """{pipeline: (evidence, t_end, stamping lookahead, decision rule)} for one trial."""
    T = len(x_z)
    eeg = causal_ea(x_z, FS, tau_s=60.0).astype(np.float32)
    dom = BG.features(raw128)["dom"][::4][32:32 + T].astype(np.float32)
    tr = dict(eeg=eeg, env=env_z.astype(np.float32), att=np.zeros(T, np.int8), t0=t0, dom=dom, key="x")
    Y = decoders._targets(tr)

    def dec_ev(trial, mode="causal"):
        e = evidence_from_features(dec.reconstruct(trial, mode), Y[:, 0, :], Y[:, 1, :], trial["att"], t0)
        return (e["r0f"].astype(float) - e["r1f"].astype(float)) @ W_LDA, e["t_end"].astype(float)

    d, te = dec_ev(tr)
    DC.W = 10 * FS
    DC.POS0 = DC.W - DC.FUT - DC.HOP
    lg, starts = DC.logits(clf, eeg, Y)
    lg, tc = lg.astype(float), starts / FS + t0
    out = {f"decoder (lookahead_s = {dec.lookahead_s:g})": (d, te, dec.lookahead_s, "hmm"),
           "direct classifier 10 s, 3 blocks": (lg, tc, LA, "hmm"),
           NEG + "decoder, offline read-out (3.75 s future)": (*dec_ev(tr, "offline"), 0.5, "hmm")}
    for off, name in ((OFF, f"fusion a=0.5, DEC_OFFSET={OFF} (default)"), (2, NEG + "fusion with DEC_OFFSET=2 (0.25 s too early)")):
        n = min(len(d), len(lg) - off)
        assert np.allclose(te[:n], tc[off:off + n] + (4 - off) * 0.25, atol=1e-3)
        f = (1 - A_MIX) * d[:n] / SD_D + A_MIX * lg[off:off + n] / SD_C
        for kind in ("hmm", *B1):
            out[f"{name} | {'HMM 0.998' if kind == 'hmm' else kind}"] = (f, tc[off:off + n], LA, kind)
    f = out[f"fusion a=0.5, DEC_OFFSET={OFF} (default) | HMM 0.998"]
    out[NEG + "B1 filter run backwards in time"] = (f[0], f[1], LA, "sm_rev")
    return out


def labels(ev, T128):
    res = {}
    for name, (d, t_end, la, kind) in ev.items():
        res[name] = np.stack([expand(d, t_end, la, T128, 0.0), expand(posterior(d, kind), t_end, la, T128, 0.0)])
    return res


def load_trial(r, tmax=60 * FS):
    d = load_pp(r, "sup", CFG["montage"], do_car=True, do_runz=False, do_trim=True)
    T = min(len(d["eeg"]), tmax)
    return dict(x=zscore_trial(d["eeg"])[:T], env=zscore_trial(d["env"])[:T], raw=load_env(r["key"]).astype(np.float64), t0=float(d["t0_s"]),
                pres=np.flatnonzero(d["present"]), key=r["key"], T=T)


def crop(tr, T):
    return tr["x"][:T], tr["env"][:T], tr["raw"][:4 * (T + 2 * FS)]


def models():
    torch.manual_seed(0)
    decoders.D["targets"] = ["env", "onset", "dom"]
    quiet = lambda *a: None
    dec = decoders.make("vlaai_ws", 58, quiet)
    init_net(dec, os.path.join(EXP, "models", "pretrain_spk_vlaai_ws_s0.pt"), quiet)
    dec.net.eval()
    decoders.D["model_blocks"] = 3
    DC.W = 10 * FS
    clfs = {}
    for ds in DS_KEYS:
        clfs[ds] = DC.DirectNet(58, 3).to(DC.DEV)
        clfs[ds].load_state_dict(torch.load(os.path.join(EXP, "models", f"fold_direct_clf__eaP_n5_t60_w10b3_ptSPK_aug-sw2_wts__{ds}.pt"),
                                            map_location=DC.DEV))
        clfs[ds].eval()
    return dec, clfs


def pick(ds, n):
    rows = rows_of(ds)
    return [rows.iloc[i] for i in np.linspace(0, len(rows) - 1, n).astype(int)]


# ---------------------------------------------------------------------------------------------------------------------
def part_a(dec, clfs):
    rng = np.random.default_rng(0)
    rows = []
    for ds in DS_KEYS:
        tr = load_trial(rows_of(ds).iloc[0])
        T = tr["T"]
        x_z, env_z, raw = tr["x"], tr["env"], tr["raw"][:4 * (T + 2 * FS)]
        T128 = len(raw)
        base = labels(evidences(x_z, env_z, raw, tr["t0"], dec, clfs[ds]), T128)
        for p in [T // 2 + k for k in range(8)] + [int(0.3 * T) // 8 * 8 + 7, int(0.8 * T) // 8 * 8 + 7]:
            for what in ("EEG", "stimulus", "EEG+stimulus"):
                x2, e2, r2 = x_z.copy(), env_z.copy(), raw.copy()
                if "EEG" in what:
                    x2[p:, tr["pres"]] = 3 * rng.standard_normal((T - p, len(tr["pres"])))
                if "stimulus" in what:
                    e2[p:] = 3 * rng.standard_normal(e2[p:].shape)
                    r2[4 * (p + FS):] = np.abs(rng.standard_normal(r2[4 * (p + FS):].shape)) * 3 * raw.mean()
                tau = p / FS + tr["t0"]
                new = labels(evidences(x2, e2, r2, tr["t0"], dec, clfs[ds]), T128)
                for name in base:
                    ch = np.flatnonzero((base[name] != new[name]).any(0))
                    rows.append(dict(dataset=ds, pipeline=name, perturbed=what, tau_s=tau, changed_samples=len(ch),
                                     lookahead_used_s=tau - ch[0] / 128 if len(ch) else np.nan))
        print(f"A {ds} done", flush=True)
    df = pd.DataFrame(rows)
    df.to_csv(os.path.join(EXP, "results", "research", "leakage_b1.csv"), index=False)
    g = df.groupby(["pipeline", "perturbed"], sort=False).agg(tests=("tau_s", "size"), changed=("changed_samples", lambda v: int((v > 0).sum())),
                                                              min_s=("lookahead_used_s", "min"), max_s=("lookahead_used_s", "max")).reset_index()
    g["verdict"] = np.where(g.max_s <= BUDGET, "ok", "VIOLATION (> 1 s)")
    pd.set_option("display.width", 250, "display.max_colwidth", 80, "display.max_rows", 300)
    say("A. lookahead by perturbation (5 datasets x 10 positions x 3 modes)")
    say(g.round(4).to_string(index=False))
    neg = g.pipeline.str.startswith(NEG)
    eeg_rows = g.perturbed != "stimulus"
    ok_real = bool((g[~neg].max_s <= BUDGET).all() and (g[~neg].changed == g[~neg].tests).all())
    ok_neg = bool((g[neg & eeg_rows].max_s > BUDGET).all())
    say(f"A. submission pipelines: {'ALL WITHIN 1 s' if ok_real else 'FAILED'} (largest lookahead used {g[~neg].max_s.max():.4f} s); "
        f"negative controls (EEG perturbed): {'all flagged' if ok_neg else 'NOT ALL FLAGGED'}")
    return ok_real and ok_neg


def part_b(dec, clfs):
    say("\nB. column swap (streams exchanged): max |d' + d| / sd(d) and max |P' + P - 1|, sign agreement of d' = -d; 3 trials per dataset")
    rows = []
    for ds in DS_KEYS:
        for r in pick(ds, 3):
            tr = load_trial(r)
            x, e, raw = crop(tr, tr["T"])
            a = evidences(x, e, raw, tr["t0"], dec, clfs[ds])
            b = evidences(x, e[:, ::-1].copy(), raw[:, ::-1].copy(), tr["t0"], dec, clfs[ds])
            for name in a:
                if name.startswith(NEG):
                    continue
                (d, _, _, kind), (d2, _, _, _) = a[name], b[name]
                n = min(len(d), len(d2))
                rel = np.abs(d[:n] + d2[:n]).max() / d[:n].std()
                p1, p2 = posterior(d[:n], kind), posterior(d2[:n], kind)
                rows.append(dict(ds=ds, pipeline=name, rel_evidence=rel, post_err=np.abs(p1 + p2 - 1).max(),
                                 sign_agree=np.mean(np.sign(d[:n]) == -np.sign(d2[:n]))))
    df = pd.DataFrame(rows)
    g = df.groupby("pipeline", sort=False).agg(rel_evidence=("rel_evidence", "max"), post_err=("post_err", "max"), sign_agree=("sign_agree", "min"))
    say(g.to_string(float_format=lambda v: f"{v:.2e}" if v < 0.01 else f"{v:.4f}"))
    ok = bool((g.rel_evidence < 1e-3).all() and (g.post_err < 1e-3).all())
    say(f"B. column swap: {'EXACT FLIP (< 1e-3)' if ok else 'NOT ANTISYMMETRIC'}")
    return ok


def part_c(dec, clfs):
    bins = [(0, 5), (5, 15), (15, 30), (30, 60)]
    names = None
    res = {}
    say("\nC1. EEG with the stimuli of another trial: share of steps with d > 0 (column 0 favoured), by position in the trial; mean +- SE over pairs")
    for ds in DS_KEYS:
        rows = rows_of(ds)
        idx = np.linspace(0, len(rows) - 1, 5).astype(int)
        for k, i in enumerate(idx):
            for j in (idx[(k + 1) % 5], idx[(k + 2) % 5]):
                a, b = load_trial(rows.iloc[i]), load_trial(rows.iloc[j])
                T = min(a["T"], b["T"])
                x = a["x"][:T]
                for tag, st in (("mismatched", b), ("matched", a)):
                    ev = evidences(x, st["env"][:T], st["raw"][:4 * (T + 2 * FS)], a["t0"], dec, clfs[ds])
                    for name, (d, te, _, kind) in ev.items():
                        if name.startswith(NEG) or kind != "hmm":
                            continue
                        pos = te - a["t0"]
                        for lo, hi in bins:
                            m = (pos >= lo) & (pos < hi)
                            if m.sum() >= 4:
                                res.setdefault((name, tag, f"{lo}-{hi} s"), []).append(np.mean(d[m] > 0))
    rows = [dict(pipeline=k[0], pairs=k[1], position=k[2], share=np.mean(v), se=np.std(v) / np.sqrt(len(v)), n=len(v)) for k, v in res.items()]
    df = pd.DataFrame(rows)
    say(df.pivot_table(index=["pipeline", "pairs"], columns="position", values="share", sort=False).round(3).to_string())
    mm = df[df.pairs == "mismatched"]
    ok = bool(((mm.share - 0.5).abs() < np.maximum(0.05, 3 * mm.se)).all())
    say(f"C1. mismatched stimuli: largest |share - 0.5| = {(mm.share - 0.5).abs().max():.3f}  ->  {'NO POSITION / COLUMN PRIOR' if ok else 'BIAS FOUND'}")
    say("\nC2. trial without its first 20 s vs the full trial (evidence 30 s after the cut); correlation and sign agreement, 3 trials per dataset")
    r2 = []
    for ds in DS_KEYS:
        for r in pick(ds, 3):
            tr = load_trial(r, 90 * FS)
            x, e, raw = crop(tr, tr["T"])
            c = 20 * FS
            full = evidences(x, e, raw, tr["t0"], dec, clfs[ds])
            cut = evidences(x[c:], e[c:], raw[4 * c:], tr["t0"] + 20, dec, clfs[ds])
            for name in full:
                if name.startswith(NEG) or full[name][3] != "hmm":
                    continue
                (d, te, _, _), (d2, te2, _, _) = full[name], cut[name]
                t_min = tr["t0"] + 50
                m1 = {int(round(t * 4)): v for t, v in zip(te, d) if t >= t_min}
                m2 = {int(round(t * 4)): v for t, v in zip(te2, d2) if t >= t_min}
                ks = sorted(set(m1) & set(m2))
                if len(ks) > 8:
                    u, v = np.array([m1[k] for k in ks]), np.array([m2[k] for k in ks])
                    r2.append(dict(ds=ds, pipeline=name, corr=np.corrcoef(u, v)[0, 1], agree=np.mean(np.sign(u) == np.sign(v))))
    g = pd.DataFrame(r2).groupby("pipeline", sort=False)[["corr", "agree"]].mean()
    say(g.round(3).to_string())
    return ok


def part_d():
    say("\nD. decision rule alone on pure-noise evidence (1500 trials of 60 s): antisymmetry and share of P > 0.5 by position")
    rng = np.random.default_rng(0)
    ok = True
    rows = []
    for kind in ("hmm", *B1):
        sh = np.zeros(3)
        err = 0.0
        for _ in range(1500):
            d = rng.normal(size=240)
            p, q = posterior(d, kind), posterior(-d, kind)
            err = max(err, np.abs(p + q - 1).max())
            sh += [np.mean(p[a:b] > 0.5) for a, b in ((0, 20), (20, 120), (120, 240))]
        sh /= 1500
        rows.append(dict(rule="HMM 0.998" if kind == "hmm" else kind, antisym_err=err, **{"0-5 s": sh[0], "5-30 s": sh[1], "30-60 s": sh[2]}))
        ok &= err < 1e-9 and bool(np.abs(sh - 0.5).max() < 0.03)
    say(pd.DataFrame(rows).to_string(index=False, float_format=lambda v: f"{v:.3g}"))
    say(f"D. {'OK' if ok else 'FAILED'}")
    return ok


if __name__ == "__main__":
    os.makedirs(os.path.join(EXP, "results", "research"), exist_ok=True)
    only = sys.argv[1] if len(sys.argv) > 1 else ""
    dec, clfs = models()
    res = {}
    if only in ("", "D"):
        res["D"] = part_d()
    if only in ("", "B"):
        res["B"] = part_b(dec, clfs)
    if only in ("", "C"):
        res["C"] = part_c(dec, clfs)
    if only in ("", "A"):
        res["A"] = part_a(dec, clfs)
    say("\nSUMMARY: " + ", ".join(f"{k} {'ok' if v else 'FAILED'}" for k, v in res.items()))
    open(os.path.join(EXP, "results", "research", "leakage_b1.txt"), "w").write("\n".join(out_lines) + "\n")
    sys.exit(0 if all(res.values()) else 1)
