"""Second-stage logit network `stem_ms96` (from eeg-thao.zip) scored on REAL attention switches (AASD, Carta2026).

The zip's five checkpoints (one per held-out corpus dataset, trained on synthetic-switch logits of the other four) are
loaded with torch.load(weights_only=True); nothing from the zip is executed. The network is re-implemented here
(LogitTransformer, 24 causal "ms" features of the 4 seed logits, window 96 frames). Its input is the 4-seed logit of the
direct classifiers; for the external sets that is ext_clf_<DS>_w10b3sw2P_evidence_s0..3.pkl (classifiers trained on all five
corpus datasets, real key-press labels, no synthetic switch).

Variants
  mean4   plain mean of the 4 seed logits, corpus emissions (reference, no second stage)
  orig    stem_ms96 as trained
  sym     (f(x) - f(-x)) / 2 : the same weights made odd under a swap of the two stimulus columns, so the learned
          "the first 24 s of a trial are column 0" prior (bias + zero-padded window) cannot act
For orig / sym every fold model is used alone; emissions (mu, sd of the signed stream) come from the four corpus datasets
that model was trained on, as in stem_ms96_image_split.py. p_stay / scale: the fold's selected pair (SEL, from their log)
and a fixed grid. Lookahead and stamping as eval_external.score.
usage: python eval_stem_external.py --ckpt-dir DIR [--device cuda]  -> results/research/stem_external.csv
"""
import argparse
import importlib
import os
import pickle
import sys

import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.nn.functional as F
from numba import njit

import eval_external as E
import score as S
from common import DS_KEYS
from common_exp import EXP, rows_of

REF = ["direct_clf__eaP_n5_t60_w10b3_ptSPK_aug-sw2", "direct_clf__eaP_n5_t60_w10b3_ptSPK_s1_aug-sw2",
       "direct_clf__eaP_n5_t60_w10b3_ptSPK_s2_aug-sw2", "direct_clf__eaP_n5_t60_w10b3_ptSPK_s3_aug-sw2"]
EXT_TAG = "_w10b3sw2P"
SEL = {"AVGC": (0.994, 0.8), "DTU": (0.997, 1.0), "KUL": (0.998, 0.8), "UHD": (0.994, 0.8), "USTC": (0.994, 0.8)}
GRID = [(0.994, 0.8), (0.998, 1.0), (0.9997, 1.0)]
W = 96


class DilatedResidual(nn.Module):
    def __init__(self, d, k=5, dilation=1, dropout=0.1):
        super().__init__()
        self.pad = (k - 1) * dilation
        self.conv = nn.Conv1d(d, d, k, dilation=dilation)
        self.norm = nn.LayerNorm(d)
        self.drop = nn.Dropout(dropout)

    def forward(self, x):
        h = self.conv(F.pad(x.transpose(1, 2), (self.pad, 0))).transpose(1, 2)
        return x + self.drop(F.gelu(self.norm(h)))


class LogitTransformer(nn.Module):
    def __init__(self, c_in=24, window=W, d_model=64, nhead=4, nlayers=2, dropout=0.1):
        super().__init__()
        self.window, self.dw_pad = window, 8
        self.dw = nn.Conv1d(c_in, c_in, 9, groups=c_in)
        self.proj = nn.Linear(c_in, d_model)
        self.blocks = nn.ModuleList([DilatedResidual(d_model, 5, d, dropout) for d in (1, 2, 4, 8)])
        self.pos = nn.Parameter(torch.zeros(1, window, d_model))
        layer = nn.TransformerEncoderLayer(d_model, nhead, d_model * 2, dropout, batch_first=True, norm_first=True,
                                           activation="gelu")
        self.enc = nn.TransformerEncoder(layer, nlayers, enable_nested_tensor=False)
        self.head = nn.Linear(d_model, 1)
        self.skip = nn.Linear(c_in, 1, bias=False)
        self.register_buffer("mask", nn.Transformer.generate_square_subsequent_mask(window))

    def forward(self, x):
        raw = x
        x = x + self.dw(F.pad(x.transpose(1, 2), (self.dw_pad, 0))).transpose(1, 2)
        h = self.proj(x)
        for b in self.blocks:
            h = b(h)
        h = self.enc(h + self.pos[:, : h.size(1)], mask=self.mask)
        return self.head(h).squeeze(-1) + self.skip(raw).squeeze(-1)


@njit(cache=False)
def _ema(x, a):
    y = np.empty_like(x)
    y[0] = a * x[0]
    for t in range(1, len(x)):
        y[t] = a * x[t] + (1 - a) * y[t - 1]
    return y


@njit(cache=False)
def _div(x, tau):
    y = np.empty_like(x)
    m2 = x[0] * x[0]
    for t in range(len(x)):
        y[t] = x[t] / (np.sqrt(m2) if m2 > 1e-12 else 1e-6)
        m2 += (x[t] * x[t] - m2) / tau
    return y


def ms_features(D):
    """(T, 4) seed logits -> (T, 24): raw, hp60, hp120, run-div 60 s, 1 s slope, 2 s mean (all causal)."""
    D = np.ascontiguousarray(D, np.float64)
    c = lambda f: np.stack([f(np.ascontiguousarray(D[:, j])) for j in range(D.shape[1])], 1)  # noqa: E731
    h60 = D - c(lambda v: _ema(v, 1 - np.exp(-0.25 / 60)))
    h120 = D - c(lambda v: _ema(v, 1 - np.exp(-0.25 / 120)))
    z = c(lambda v: _div(v, 240.0))
    slope = np.zeros_like(D)
    slope[4:] = D[4:] - D[:-4]
    cs = np.vstack([np.zeros((1, 4)), np.cumsum(D, 0)])
    i = np.arange(len(D))
    left = np.maximum(0, i - 7)
    mean = (cs[i + 1] - cs[left]) / (i - left + 1)[:, None]
    return np.concatenate([D, h60, h120, z, slope, mean], 1).astype(np.float32)


@torch.no_grad()
def infer(model, X, mu, sd, dev):
    xp = torch.from_numpy(np.concatenate([np.zeros((W - 1, X.shape[1]), np.float32), X]))
    w = xp.unfold(0, W, 1).permute(0, 2, 1)
    w = (w - torch.from_numpy(mu)) / torch.from_numpy(sd)
    return torch.cat([model(w[i:i + 4096].to(dev))[:, -1].cpu() for i in range(0, len(w), 4096)]).numpy().astype(np.float64)


def streams_of(model, X, mu, sd, dev):
    """orig = f(x); sym = (f(x) - f(-x)) / 2, with -x the raw features negated before normalisation."""
    a = infer(model, X, mu, sd, dev)
    b = infer(model, -X, mu, sd, dev)
    return a, (a - b) / 2


def emission(streams, att):
    A = np.concatenate([s * np.where(att[k] == 0, 1.0, -1.0) for k, s in streams.items()])
    return float(A.mean()), float(A.std())


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--ckpt-dir", required=True)
    ap.add_argument("--device", default="cuda" if torch.cuda.is_available() else "cpu")
    a = ap.parse_args()
    dev = torch.device(a.device)
    torch.set_num_threads(4)

    # allow only numpy types that the checkpoints contain (arrays, dtypes); everything else stays refused
    need = set()
    for ds in DS_KEYS:
        need |= set(torch.serialization.get_unsafe_globals_in_checkpoint(os.path.join(a.ckpt_dir, f"stem_ms96__held-{ds}.pt")))
    allow = []
    for name in need:
        mod, _, attr = name.rpartition(".")
        if not mod.startswith("numpy"):
            raise SystemExit(f"refusing non-numpy global {name}")
        allow.append(getattr(importlib.import_module(mod), attr))
    torch.serialization.add_safe_globals(allow + [type(np.dtype(np.float32)), type(np.dtype(np.float64))])

    evs, lookahead = [], None
    for exp in REF:
        ev, info = S.load_folds(exp, "causal")
        evs.append(ev)
        lookahead = float(info[DS_KEYS[0]]["lookahead_s"]) if lookahead is None else lookahead
    print(f"corpus folds loaded, lookahead {lookahead}", flush=True)
    corpus, att = {}, {}
    for ds in DS_KEYS:
        corpus[ds], att[ds] = {}, {}
        for k in evs[0][ds]:
            D = np.stack([(ev[ds][k]["r0"] - ev[ds][k]["r1"]) for ev in evs], 1)
            corpus[ds][k] = ms_features(D)
            att[ds][k] = evs[0][ds][k]["att"]
    print("corpus features done", flush=True)

    ext = {}
    for ds in ("AASD", "Carta2026"):
        E4 = [pickle.load(open(os.path.join(EXP, "results", "research", f"ext_clf_{ds}{EXT_TAG}_evidence_s{i}.pkl"), "rb")) for i in range(4)]
        ext[ds] = {}
        for k in E4[0]:
            D = np.stack([(e[k]["r0"] - e[k]["r1"]).astype(np.float64) for e in E4], 1)
            ext[ds][k] = dict(ev=E4[0][k], X=ms_features(D), D=D)
        print(f"{ds}: {len(ext[ds])} trials, {len(set(v['ev']['subject'] for v in ext[ds].values()))} subjects", flush=True)

    rows = []

    def add(variant, fold, ds, p, sc, r):
        rows.append(dict(variant=variant, fold_model=fold, ds=ds, p_stay=p, scale=sc, **{x: float(r[x]) for x in ("H", "Acc", "SDT", "L", "nSwitches", "nUndetected")}))
        print(f"{variant:6s} {fold:5s} {ds:9s} p {p} sc {sc}  H {r['H']:.3f} Acc {r['Acc']:.1f} SDT {r['SDT']:.1f} L {r['L']:.3f}", flush=True)

    # reference: mean of the 4 seed logits, emissions from all five corpus datasets
    m4 = {k: v.mean(1) for ds in DS_KEYS for k, v in ((k, corpus[ds][k][:, :4]) for k in corpus[ds])}
    mu0, sd0 = emission(m4, {k: att[ds][k] for ds in DS_KEYS for k in att[ds]})
    for ds in ext:
        for p in (0.998, 0.9997):
            ev = {k: dict(v["ev"], r0=v["D"].mean(1), r1=np.zeros(len(v["D"]))) for k, v in ext[ds].items()}
            add("mean4", "-", ds, p, 1.0, E.score(ev, mu0, sd0, p, lookahead))

    for held in DS_KEYS:
        ck = torch.load(os.path.join(a.ckpt_dir, f"stem_ms96__held-{held}.pt"), map_location="cpu", weights_only=True)
        model = LogitTransformer().to(dev)
        model.load_state_dict(ck["state"])
        model.eval()
        mu, sd = np.asarray(ck["mu"], np.float32), np.asarray(ck["sd"], np.float32)
        tr_streams = {"orig": {}, "sym": {}}
        tr_att = {}
        for ds in DS_KEYS:
            for k, X in corpus[ds].items():
                o, s = streams_of(model, X, mu, sd, dev)
                if ds != held:
                    tr_streams["orig"][k], tr_streams["sym"][k] = o, s
                    tr_att[k] = att[ds][k]
                else:                                                         # sanity: frame accuracy on the held-out set
                    y = att[ds][k] == 0
                    rows.append(dict(variant="check", fold_model=held, ds=ds, p_stay=0, scale=0, H=float(np.mean((o > 0) == y)),
                                                Acc=float(np.mean((s > 0) == y)), SDT=0, L=0, nSwitches=0, nUndetected=0))
        emis = {v: emission(tr_streams[v], tr_att) for v in tr_streams}
        print(f"fold {held}: emissions orig {emis['orig']} sym {emis['sym']}", flush=True)
        for ds in ext:
            st = {k: streams_of(model, v["X"], mu, sd, dev) for k, v in ext[ds].items()}
            for vi, v in enumerate(("orig", "sym")):
                ev = {k: dict(ext[ds][k]["ev"], r0=st[k][vi], r1=np.zeros(len(st[k][vi]))) for k in st}
                mu_v, sd_v = emis[v]
                for p, sc in [SEL[held]] + [g for g in GRID if g != SEL[held]]:
                    add(v, held, ds, p, sc, E.score(ev, mu_v, sd_v * sc, p, lookahead))
    df = pd.DataFrame(rows)
    out = os.path.join(EXP, "results", "research", "stem_external.csv")
    df.to_csv(out, index=False)
    chk = df[df.variant == "check"]
    print("corpus frame accuracy of the held-out fold (orig / sym):", [(r.fold_model, round(r.H, 3), round(r.Acc, 3)) for r in chk.itertuples()])
    sc = df[df.variant != "check"].groupby(["variant", "ds", "p_stay", "scale"])[["H", "Acc", "SDT", "L"]].mean().round(3)
    print(sc.to_string())
    print("wrote", out)


if __name__ == "__main__":
    main()
