"""End-to-end lookahead (leakage) test of the submission pipeline by PERTURBATION.

Rule: the label of time s may only depend on data up to s + 1 s, except through the exempted whole-trial operations
(filtfilt, removeBadChannel, full-trial z-score), which are all applied BEFORE the signals perturbed here.

For a perturbation time tau every input sample at time >= tau is replaced by noise:
  EEG       the z-scored 32-Hz EEG (after the exempted steps, before the causal Euclidean alignment); channels the
            dataset's montage lacks (zero-filled, e.g. one in KUL-UHD) stay zero: the channel set is metadata
  stimulus  the z-scored 32-Hz envelopes of both streams AND their raw 128-Hz envelopes (source of the dom target)
and everything after that is recomputed with the pipeline's own code: prepare_data.causal_ea -> build_glimpse.features
(dom) -> decoders._targets -> DNNDec.reconstruct -> common_exp.evidence_from_features -> target weights -> HMM forward
filter (e1_switch.hmm_filter) -> label stamping (e1_switch.expand, 128 Hz); direct_classifier.logits; and the fusion
with the alignment of fuse_evidence.features (DEC_OFFSET).
Measured per test: the first 128-Hz label sample whose evidence or filtering posterior differs from the unperturbed run
    lookahead used = tau - time of that sample          (must be <= 1 s; comparison is exact, no tolerance)
Networks: SparrKULee-pretrained weights (the dependency structure is architectural: fine-tuning cannot add future).
Constant in the test, as in the pipeline (fitted on the OTHER datasets): target weights, scales, emissions, p_stay.
Negative controls (each must be flagged) include two violations of only 0.25 s.
usage: GLIMPSE_THR=norm python leakage_test.py   -> printed table + results/research/leakage_test.csv; exit 1 on failure"""
import os
import sys

import numpy as np
import pandas as pd
import torch
from scipy.signal import butter, sosfiltfilt

_argv, sys.argv = sys.argv, sys.argv[:1] + ["norm"]            # build_glimpse reads its variant from argv
import build_glimpse as BG  # noqa: E402
sys.argv = _argv
import decoders  # noqa: E402
import direct_classifier as DC  # noqa: E402
from common_exp import CFG, EXP, FS, evidence_from_features, rows_of  # noqa: E402
from common import DS_KEYS, load_env, load_pp  # noqa: E402
from e1_switch import BUDGET, expand, hmm_filter  # noqa: E402
from fuse_evidence import LA, OFF  # noqa: E402  (classifier / fusion stamping, default decoder offset)
from prepare_data import causal_ea, zscore_trial  # noqa: E402
from run_experiment import init_net  # noqa: E402

W_LDA, A_MIX, SD_D, SD_C, P_STAY = np.array([0.06, 0.59, 0.35]), 0.3, 0.2, 2.0, 0.9997
NEG = "NEGATIVE CONTROL: "


def evidences(x_z, env_z, raw128, t0, dec, clfs):
    """{pipeline: (evidence per step, t_end per step, stamping lookahead L)} 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)             # as build_glimpse.build
    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)
    out = {f"decoder, DEFAULT stamping (dec.lookahead_s = {dec.lookahead_s:g})": (d, te, dec.lookahead_s),
           "decoder, stamped with lookahead 0.75 (until 2026-10-04)": (d, te, 0.75),
           NEG + "decoder stamped with lookahead 0.25 (0.25 s too early)": (d, te, 0.25),
           NEG + "decoder, offline read-out (3.75 s future)": (*dec_ev(tr, "offline"), 0.5)}
    zp = sosfiltfilt(butter(2, [1, 9], "bandpass", fs=FS, output="sos"), x_z, axis=0)
    out[NEG + "decoder, a second zero-phase filter on the EEG"] = (*dec_ev({**tr, "eeg": causal_ea(zp, FS).astype(np.float32)}), 0.5)
    for ws, clf in clfs.items():
        DC.W = int(ws * FS)
        DC.POS0 = DC.W - DC.FUT - DC.HOP
        lg, starts = DC.logits(clf, eeg, Y)
        lg, tc = lg.astype(float), starts / FS + t0                                 # as direct_classifier.run_fold
        out[f"direct classifier, {ws}-s window"] = (lg, tc, LA)
        if ws != 5:
            continue
        for off, name in ((OFF, f"fusion 70/30, DEFAULT DEC_OFFSET={OFF}"), (4, "fusion 70/30, DEC_OFFSET=4 (until 2026-10-04)"),
                          (2, NEG + "fusion with DEC_OFFSET=2 (0.25 s too early)")):
            n = min(len(d), len(lg) - off)                                          # alignment of fuse_evidence.features
            assert np.allclose(te[:n], tc[off:off + n] + (4 - off) * 0.25, atol=1e-3)
            out[name] = ((1 - A_MIX) * d[:n] / SD_D + A_MIX * lg[off:off + n] / SD_C, tc[off:off + n], LA)
    return out


def labels(ev, T128):
    """Per pipeline: (evidence, posterior) stamped onto the 128-Hz label samples exactly as the scorer does."""
    res = {}
    for name, (d, t_end, L) in ev.items():
        P = hmm_filter(-0.5 * (d - 0.5) ** 2, -0.5 * (d + 0.5) ** 2, P_STAY)        # LLR = d
        res[name] = np.stack([expand(d, t_end, L, T128, 0.0), expand(P, t_end, L, T128, 0.0)])
    return res


if __name__ == "__main__":
    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()
    clfs = {}
    for ws, tag in ((5, "_v2"), (10, "_w10")):
        DC.W = int(ws * FS)
        clfs[ws] = DC.DirectNet(58, 3).to(DC.DEV)
        clfs[ws].load_state_dict(torch.load(os.path.join(EXP, "models", f"pretrain_spk_direct_clf{tag}_s0.pt"), map_location=DC.DEV))
        clfs[ws].eval()
    rng = np.random.default_rng(0)
    rows = []
    for ds in DS_KEYS:
        r = rows_of(ds).iloc[0]
        d = load_pp(r, "sup", CFG["montage"], do_car=True, do_runz=False, do_trim=True)
        t0 = float(d["t0_s"])
        T = min(len(d["eeg"]), 60 * FS)                                              # the first 60 s are enough
        x_z, env_z = zscore_trial(d["eeg"])[:T], zscore_trial(d["env"])[:T]
        raw = load_env(r["key"]).astype(np.float64)[:4 * (T + 2 * FS)]
        pres = np.flatnonzero(d["present"])
        assert (x_z[:, pres].std(0) > 0).all() and not x_z[:, ~d["present"]].any()
        T128 = len(raw)
        base = labels(evidences(x_z, env_z, raw, t0, dec, clfs), T128)
        # all 8 phases relative to the 0.25-s hop grid (the worst case is one of them) + two other places
        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:, pres] = 3 * rng.standard_normal((T - p, len(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 + t0
                new = labels(evidences(x2, e2, r2, t0, dec, clfs), T128)
                for name in base:
                    ch = np.flatnonzero((base[name] != new[name]).any(0))
                    rows.append(dict(dataset=ds, trial=r["key"], 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"{ds}: {r['key']} done", flush=True)
    df = pd.DataFrame(rows)
    os.makedirs(os.path.join(EXP, "results", "research"), exist_ok=True)
    df.to_csv(os.path.join(EXP, "results", "research", "leakage_test.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", 90, "display.max_rows", 200)
    print(g.round(4).to_string(index=False))
    neg = g.pipeline.str.startswith(NEG)
    eeg_rows = g.perturbed != "stimulus"                      # a stimulus-only perturbation cannot reveal an EEG-side leak
    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())
    print(f"\nsubmission pipelines: {'ALL WITHIN 1 s' if ok_real else 'FAILED'} (largest lookahead used {g[~neg].max_s.max():.4f} s)")
    print(f"negative controls (EEG perturbed): {'all flagged' if ok_neg else 'NOT ALL FLAGGED: the test is not sensitive enough'}")
    sys.exit(0 if ok_real and ok_neg else 1)
