"""trf-lagbank-clf — Path A: frozen train-only TRF/CCA spatial filters + lag-bank scores.

Distinct from decoder CCA/FBCCA/WF reconstruction benchmarks and from xcorr CNN:
  * Fit multivariate ridge TRF (EEG→attended envelope) and/or CCA spatial filters on each
    LODO train split only (never held-out / AASD / Carta labels).
  * Freeze filters. At inference, compute per-lag correlation scores vs env0 and env1
    (lag bank ~0..250–400 ms @32 Hz); antisymmetric evidence = mean_τ (r0_τ − r1_τ).
  * Path A (preferred): frozen scores alone → HMM emissions (no deep net).
  * Optional Path B (tiny linear head) aborted if AVGC BCE ≥0.685 @50 steps.

Prep eaP_n5_t60. Score LODO 5 + AASD + Carta @ HMM 0.994/0.8; also hp30 if 1-D.

usage:
  /root/disk_3tb/EEG-sAAD-Grand-Challenge-Training-Dataset/.venv/bin/python trf_lagbank_clf.py
  PATH=trf|cca|both  MAX_LAG_MS=400  PROBE_ONLY=1
"""
from __future__ import annotations

import json
import os
import pickle
import time
from dataclasses import dataclass

import numpy as np

import ensemble_clf_transformer_v4 as V4
import eval_external as E
import score as S
from common import DS_KEYS, SEED
from common_exp import (
    CFG, EXP, FS, evidence_from_features, fold_file, load_trial, rows_of, save_pickle,
)
from run_experiment import load_external, log_to

# -------------------------------------------------------------------------------------------------
EXP_NAME = "trf_lagbank__eaP_n5_t60"
TAG = "_trf_lagbank"
PREP = "eaP_n5_t60"
LOG_PATH = os.path.join(EXP, "experiments", "trf_lagbank_clf.log")
OUT_JSON = os.path.join(EXP, "results", f"{EXP_NAME}.json")
CKPT_DIR = os.path.join(EXP, "models", "trf_lagbank")
EXT_DIR = os.path.join(EXP, "results", "research")
RESULTS_MD = os.path.join(EXP, "multi_agent_opt", "results_trf_lagbank.md")
P_STAY, SCALE = 0.994, 0.8
LA = 0.75  # evidence stamped at window end; filters are causal past-EEG → env

MAX_LAG_MS = int(os.environ.get("MAX_LAG_MS", "400"))
N_COMP = int(os.environ.get("N_COMP", "3"))          # CCA components / TRF multi-output unused
RIDGE_REL = float(os.environ.get("RIDGE_REL", "1e-3"))
PATH = os.environ.get("PATH_MODE", os.environ.get("PATH", "both"))  # trf | cca | both
PROBE_ONLY = os.environ.get("PROBE_ONLY", "0") == "1"
ACC1_GATE = float(os.environ.get("ACC1_GATE", "0.53"))  # Path A must clear this on AVGC probe
CHANCE_LOSS = float(os.environ.get("CHANCE_LOSS", "0.685"))
CHANCE_STEPS = int(os.environ.get("CHANCE_STEPS", "50"))
LINEAR_HP30_AASD = 53.12
SW2_AASD = 53.46


def log_line(msg):
    print(msg, flush=True)


# -------------------------------------------------------------------------------------------------
# Filters
# -------------------------------------------------------------------------------------------------
@dataclass
class LagBankFilters:
    kind: str                       # "trf" | "cca"
    max_lag: int                    # samples inclusive (0..max_lag)
    W: np.ndarray                   # TRF: (n_lags, C); CCA: (C, K)
    ridge: float = RIDGE_REL
    n_comp: int = N_COMP


def _att_env(tr):
    a = tr["att"].astype(int)
    idx = np.arange(len(tr["env"]))
    return tr["env"][idx, a].astype(np.float64), tr["env"][idx, 1 - a].astype(np.float64)


def fit_trf(train, max_lag, ridge_rel=RIDGE_REL, log=None, max_samples=2_000_000):
    """Multivariate ridge TRF (decoding, same lag axis as official WF):
    env(t) ≈ Σ_{τ=0..L} EEG(t+τ,:) · w_τ. Lookahead = L/FS (~400 ms).
    Returns W (L+1, C). Chunked accumulation; caps samples for speed.
    """
    C = train[0]["eeg"].shape[1]
    n_lags = max_lag + 1
    p = n_lags * C
    XtX = np.zeros((p, p), np.float64)
    Xty = np.zeros(p, np.float64)
    n = 0
    chunk = 4096
    ordered = sorted(train, key=lambda tr: 1 if tr.get("_grp") == "Fuglsang2020" else 0)
    for tr in ordered:
        if n >= max_samples:
            break
        X = np.asarray(tr["eeg"], np.float64)
        y, _ = _att_env(tr)
        T = min(len(X), len(y))
        if T <= max_lag + 8:
            continue
        # valid t = 0 .. T-1-L ; feature = concat_τ EEG[t+τ]
        n_valid = T - max_lag
        take = min(n_valid, max_samples - n)
        if take < n_valid:
            idx = np.linspace(0, n_valid - 1, take, dtype=int)
        else:
            idx = np.arange(n_valid)
        for s0 in range(0, len(idx), chunk):
            ii = idx[s0:s0 + chunk]
            m = len(ii)
            Xt = np.empty((m, p), np.float64)
            for τ in range(n_lags):
                Xt[:, τ * C:(τ + 1) * C] = X[ii + τ]
            yt = y[ii]
            XtX += Xt.T @ Xt
            Xty += Xt.T @ yt
            n += m
            if n >= max_samples:
                break
    if n < p:
        raise RuntimeError(f"TRF underdetermined: n={n} p={p}")
    XtX /= n
    Xty /= n
    lam = ridge_rel * (np.trace(XtX) / p)
    XtX.flat[:: p + 1] += lam
    w = np.linalg.solve(XtX, Xty)
    W = w.reshape(n_lags, C)
    if log:
        log(f"    TRF fit n={n} p={p} ridge={lam:.3e} ||W||={np.linalg.norm(W):.4f} "
            f"lookahead={max_lag/FS:.3f}s")
    return LagBankFilters(kind="trf", max_lag=max_lag, W=W.astype(np.float64), ridge=ridge_rel)


def fit_cca(train, max_lag, n_comp=N_COMP, ridge_rel=RIDGE_REL, log=None, max_samples=2_000_000):
    """Spatial CCA between EEG and lagged attended-env bank. Wx (C, K) frozen.
    Lag bank at inference vs both envelopes — not package CCADec lagged-EEG×env CCA."""
    C = train[0]["eeg"].shape[1]
    Ky = min(n_comp, max_lag + 1)
    Rxx = np.zeros((C, C), np.float64)
    Ryy = np.zeros((Ky, Ky), np.float64)
    Rxy = np.zeros((C, Ky), np.float64)
    n = 0
    ordered = sorted(train, key=lambda tr: 1 if tr.get("_grp") == "Fuglsang2020" else 0)
    for tr in ordered:
        if n >= max_samples:
            break
        X = np.asarray(tr["eeg"], np.float64)
        y, _ = _att_env(tr)
        T = min(len(X), len(y))
        if T <= max_lag + 8:
            continue
        n_valid = T - max_lag
        take = min(n_valid, max_samples - n)
        if take < n_valid:
            # subsample rows
            sel = np.linspace(0, n_valid - 1, take, dtype=int)
            Xb = X[max_lag:T][sel]
            Y = np.empty((take, Ky), np.float64)
            for k in range(Ky):
                Y[:, k] = y[max_lag - k: T - k][sel]
        else:
            Xb = X[max_lag:T]
            Y = np.empty((n_valid, Ky), np.float64)
            for k in range(Ky):
                Y[:, k] = y[max_lag - k: T - k]
        Xb = Xb - Xb.mean(0, keepdims=True)
        Y = Y - Y.mean(0, keepdims=True)
        Rxx += Xb.T @ Xb
        Ryy += Y.T @ Y
        Rxy += Xb.T @ Y
        n += len(Xb)
    Rxx /= n
    Ryy /= n
    Rxy /= n
    Rxx = Rxx + ridge_rel * (np.trace(Rxx) / C) * np.eye(C)
    Ryy = Ryy + ridge_rel * (np.trace(Ryy) / Ky) * np.eye(Ky)
    iRyy = np.linalg.solve(Ryy, np.eye(Ky))
    M = np.linalg.solve(Rxx, Rxy) @ iRyy @ Rxy.T
    evals, evecs = np.linalg.eigh(M)
    order = np.argsort(evals)[::-1]
    Wx = evecs[:, order[:n_comp]].copy()
    for k in range(Wx.shape[1]):
        nrm = np.sqrt(max(Wx[:, k] @ Rxx @ Wx[:, k], 1e-18))
        Wx[:, k] /= nrm
    if log:
        top = float(np.sqrt(np.clip(evals[order[0]], 0, None)))
        log(f"    CCA-spatial fit n={n} C={C} K={Wx.shape[1]} topρ≈{top:.3f}")
    return LagBankFilters(kind="cca", max_lag=max_lag, W=Wx.astype(np.float64),
                          ridge=ridge_rel, n_comp=Wx.shape[1])


def _sliding_corr_1d(a, b, W, H):
    """Pearson corr of 1-d series in windows of W every H. Returns (n,), starts."""
    a = np.asarray(a, np.float64)
    b = np.asarray(b, np.float64)
    n = min(len(a), len(b))
    a, b = a[:n], b[:n]
    starts = np.arange(0, n - W + 1, H)
    if len(starts) == 0:
        return np.zeros(0), starts
    cs_a = np.concatenate([[0.0], np.cumsum(a)])
    cs_b = np.concatenate([[0.0], np.cumsum(b)])
    cs_aa = np.concatenate([[0.0], np.cumsum(a * a)])
    cs_bb = np.concatenate([[0.0], np.cumsum(b * b)])
    cs_ab = np.concatenate([[0.0], np.cumsum(a * b)])
    sa = cs_a[starts + W] - cs_a[starts]
    sb = cs_b[starts + W] - cs_b[starts]
    saa = cs_aa[starts + W] - cs_aa[starts]
    sbb = cs_bb[starts + W] - cs_bb[starts]
    sab = cs_ab[starts + W] - cs_ab[starts]
    cov = sab - sa * sb / W
    va = np.maximum(saa - sa ** 2 / W, 1e-12)
    vb = np.maximum(sbb - sb ** 2 / W, 1e-12)
    return (cov / np.sqrt(va * vb)).astype(np.float64), starts


def lagbank_scores(filt: LagBankFilters, eeg, env0, env1):
    """Per-lag antisymmetric bank → streams A, B0, B1 for evidence_from_features.

    TRF (WF lag axis): ŷ_τ(t)=EEG(t+τ)·w_τ; A = [Σŷ, ŷ_0..ŷ_L]; corr vs env at t.
    CCA: z=EEG@Wx; lag-bank corr(z[t+τ], env[t]). Lookahead = L/FS.
    """
    eeg = np.asarray(eeg, np.float64)
    env0 = np.asarray(env0, np.float64)
    env1 = np.asarray(env1, np.float64)
    T = min(len(eeg), len(env0), len(env1))
    eeg, env0, env1 = eeg[:T], env0[:T], env1[:T]
    L = filt.max_lag
    Tv = T - L

    if filt.kind == "trf":
        n_lags = L + 1
        Yhat = np.zeros((Tv, n_lags), np.float64)
        for τ in range(n_lags):
            Yhat[:, τ] = eeg[τ: τ + Tv] @ filt.W[τ]
        ysum = Yhat.sum(1, keepdims=True)
        # Evidence streams: coherent sum + per-lag bank (r0f/r1f keep all for optional LDA)
        A = np.concatenate([ysum, Yhat], axis=1)
        e0, e1 = env0[:Tv], env1[:Tv]
        B0 = np.broadcast_to(e0[:, None], A.shape).copy()
        B1 = np.broadcast_to(e1[:, None], A.shape).copy()
        return A.astype(np.float32), B0.astype(np.float32), B1.astype(np.float32), L / FS

    z = eeg @ filt.W
    K = z.shape[1]
    n_lags = L + 1
    A = np.empty((Tv, K * n_lags), np.float64)
    B0 = np.empty_like(A)
    B1 = np.empty_like(A)
    for τ in range(n_lags):
        A[:, τ * K:(τ + 1) * K] = z[τ: τ + Tv]
        B0[:, τ * K:(τ + 1) * K] = np.broadcast_to(env0[:Tv, None], (Tv, K))
        B1[:, τ * K:(τ + 1) * K] = np.broadcast_to(env1[:Tv, None], (Tv, K))
    return A.astype(np.float32), B0.astype(np.float32), B1.astype(np.float32), L / FS


def evidence_trial(filt, tr):
    A, B0, B1, la = lagbank_scores(filt, tr["eeg"], tr["env"][:, 0], tr["env"][:, 1])
    # Features aligned to env time t=0..Tv-1; lookahead = la (future EEG used)
    t0 = tr["t0"]
    att = tr["att"][: len(A)]
    return evidence_from_features(A, B0, B1, att, t0), la


def signed_acc1(ev_dict):
    """Pooled 1-s Acc of signed evidence (for probe)."""
    parts = []
    for e in ev_dict.values():
        d = (e["r0"] - e["r1"])[::4]
        sg = np.where(e["att"][::4] == 0, 1.0, -1.0)
        parts.append(d * sg)
    d = np.concatenate(parts)
    return float((d > 0).mean()), float(d.mean() / (d.std() + 1e-12))


def swap_probe(filt, tr):
    """Speaker-order swap must flip evidence; zero-EEG must collapse |d|."""
    A, B0, B1, _ = lagbank_scores(filt, tr["eeg"], tr["env"][:, 0], tr["env"][:, 1])
    # mean feature-wise corr proxy: use full-trial pearson of mean-A vs env
    a = A.mean(1)
    r0 = np.corrcoef(a, B0.mean(1))[0, 1]
    r1 = np.corrcoef(a, B1.mean(1))[0, 1]
    d = r0 - r1
    # swapped
    r0s = np.corrcoef(a, B1.mean(1))[0, 1]
    r1s = np.corrcoef(a, B0.mean(1))[0, 1]
    d_swap = r0s - r1s
    # zero eeg
    A0, B00, B10, _ = lagbank_scores(filt, np.zeros_like(tr["eeg"]), tr["env"][:, 0], tr["env"][:, 1])
    a0 = A0.mean(1)
    # constant A → corr undefined; use std
    if a0.std() < 1e-12:
        d0 = 0.0
    else:
        d0 = np.corrcoef(a0, B00.mean(1))[0, 1] - np.corrcoef(a0, B10.mean(1))[0, 1]
    swap_delta = abs(d - d_swap)
    return dict(d=float(d), d_swap=float(d_swap), d0=float(d0),
                swap_delta=float(swap_delta), align_vs_zero=float(abs(d) - abs(d0)))


# -------------------------------------------------------------------------------------------------
# Optional Path B: tiny linear head on frozen lag-bank window features
# -------------------------------------------------------------------------------------------------
def path_b_chance_probe(filt, train_trials, log):
    """BCE on frozen lag-bank summary features; abort if mean loss ≥ CHANCE_LOSS @ CHANCE_STEPS."""
    import torch
    import torch.nn.functional as F

    W = int(CFG["evidence"]["win_s"] * FS)  # 1 s
    feats, labels = [], []
    rng = np.random.default_rng(CFG["seed"])
    for tr in train_trials:
        A, B0, B1, _ = lagbank_scores(filt, tr["eeg"], tr["env"][:, 0], tr["env"][:, 1])
        # per-window: [mean_f corr(A,B0), mean_f corr(A,B1), diff] — already in evidence; use raw
        # Use windowed mean of (A*B0 - A*B1) normalised as a cheap feature
        T = len(A)
        att = tr["att"][:T].astype(int)
        if filt.kind == "trf":
            att = tr["att"][filt.max_lag: filt.max_lag + T].astype(int)
        starts = np.arange(0, T - W + 1, W)  # non-overlap for speed
        if len(starts) == 0:
            continue
        for s0 in starts:
            sl = slice(s0, s0 + W)
            # feature: per-dim mean A, mean B0, mean B1 → too big; use corr diffs per lag-group
            a = A[sl]
            # reduce: corr of each feature col with env0/env1 mean
            e0 = B0[sl, 0] if B0.shape[1] else B0[sl].mean(1)
            e1 = B1[sl, 0] if B1.shape[1] else B1[sl].mean(1)
            # compact: [corr(a_mean,e0)-corr(a_mean,e1), ||a||]
            am = a.mean(1)
            def _r(u, v):
                u, v = u - u.mean(), v - v.mean()
                den = (np.linalg.norm(u) * np.linalg.norm(v) + 1e-12)
                return float((u @ v) / den)
            diff = _r(am, e0) - _r(am, e1)
            # also per-lag group means if multi-col
            if a.shape[1] > 1:
                # sample up to 8 columns
                cols = np.linspace(0, a.shape[1] - 1, num=min(8, a.shape[1]), dtype=int)
                extra = [_r(a[:, j], e0) - _r(a[:, j], e1) for j in cols]
            else:
                extra = []
            x = np.array([diff] + extra, np.float32)
            y = 0.0 if (att[s0:s0 + W] == 0).mean() > 0.5 else 1.0
            # label: 0 means attend stream0; logit should be positive when r0>r1 i.e. attend0
            # Use y=1 when attend stream0 for BCE with logit = scale * diff
            y = 1.0 if (att[s0:s0 + W] == 0).mean() > 0.5 else 0.0
            feats.append(x)
            labels.append(y)
        if len(feats) > 4000:
            break
    X = np.stack(feats)
    y = np.asarray(labels, np.float32)
    # standardise
    mu, sd = X.mean(0), X.std(0) + 1e-6
    X = (X - mu) / sd
    device = torch.device("cpu")
    Xt = torch.from_numpy(X).to(device)
    yt = torch.from_numpy(y).to(device)
    head = torch.nn.Linear(X.shape[1], 1).to(device)
    opt = torch.optim.Adam(head.parameters(), lr=1e-2)
    losses = []
    for step in range(CHANCE_STEPS):
        idx = rng.choice(len(X), size=min(256, len(X)), replace=False)
        logit = head(Xt[idx]).squeeze(-1)
        # antisym prior: prefer weight on dim0 (diff); BCE
        # Map: attend0 → y=1; evidence diff>0 should give high logit
        loss = F.binary_cross_entropy_with_logits(logit, yt[idx])
        opt.zero_grad()
        loss.backward()
        opt.step()
        losses.append(float(loss.detach()))
    mean_loss = float(np.mean(losses))
    log(f"  Path B chance-probe mean_loss@{CHANCE_STEPS}={mean_loss:.4f} "
        f"(abort if >={CHANCE_LOSS})")
    return mean_loss, mean_loss < CHANCE_LOSS


# -------------------------------------------------------------------------------------------------
# LODO / external
# -------------------------------------------------------------------------------------------------
USE_FUGLSANG = os.environ.get("USE_FUGLSANG", "0") == "1"  # Path A default: corpus-only (fast, enough)


def gather_train(held, prep, log, extra_fug=None):
    if extra_fug is None:
        extra_fug = USE_FUGLSANG
    train = []
    for ds in DS_KEYS:
        if held is not None and ds == held:
            continue
        for _, r in rows_of(ds).iterrows():
            train.append(load_trial(prep, r["key"]))
    if extra_fug:
        try:
            train += load_external(["Fuglsang2020"], prep, log)
        except Exception as ex:
            log(f"  Fuglsang skip: {ex}")
    return train


def fit_filters(train, kind, max_lag, log):
    if kind == "trf":
        return fit_trf(train, max_lag, log=log)
    if kind == "cca":
        return fit_cca(train, max_lag, log=log)
    raise ValueError(kind)


def run_fold(held, kind, max_lag, exp, log):
    t0 = time.time()
    train = gather_train(held, PREP, log)
    log(f"  fold held-out={held} kind={kind}: {len(train)} train trials, L={max_lag} ({max_lag/FS*1000:.0f} ms)")
    filt = fit_filters(train, kind, max_lag, log)
    os.makedirs(CKPT_DIR, exist_ok=True)
    ckpt = os.path.join(CKPT_DIR, f"held-{held}_{kind}.npz")
    np.savez(ckpt, kind=filt.kind, max_lag=filt.max_lag, W=filt.W, ridge=filt.ridge, n_comp=filt.n_comp)
    del train
    # swap probe on first held trial
    rows = rows_of(held)
    tr0 = load_trial(PREP, rows.iloc[0]["key"])
    sp = swap_probe(filt, tr0)
    log(f"  swap-probe {sp}")
    ev = {}
    for _, r in rows.iterrows():
        tr = load_trial(PREP, r["key"])
        e, la = evidence_trial(filt, tr)
        ev[r["key"]] = e
    acc1, snr = signed_acc1(ev)
    info = dict(decoder=f"trf_lagbank_{kind}", prep=PREP, held_out=held,
                fit_s=time.time() - t0, total_s=time.time() - t0,
                lookahead_s=la, channels=58, max_lag=max_lag, kind=kind,
                swap=sp, acc1_pooled=acc1, snr1_pooled=snr)
    save_pickle(dict(evidence={"causal": ev}, info=info), fold_file(exp, held))
    log(f"  fold {held} done in {info['total_s']:.0f}s acc1={acc1:.3f} snr={snr:.3f}")
    return filt, acc1, snr, sp


def avgc_probe(kind, max_lag, log):
    """Quick Path A probe: hold AVGC, fit on other 4, report acc1."""
    log(f"=== AVGC Path A probe kind={kind} max_lag_ms={max_lag/FS*1000:.0f} ===")
    filt, acc1, snr, sp = run_fold("AVGC", kind, max_lag, EXP_NAME + f"__probe_{kind}", log)
    ok = acc1 >= ACC1_GATE and sp["swap_delta"] >= 0.01
    log(f"  PROBE RESULT acc1={acc1:.4f} snr={snr:.3f} swapΔ={sp['swap_delta']:.4f} "
        f"gate_acc1>={ACC1_GATE} → {'PASS' if ok else 'FAIL'}")
    return ok, acc1, snr, sp, filt


def score_all(exp, kind, log):
    la = 0.75
    # Use filter lookahead from first fold info
    with open(fold_file(exp, "AVGC"), "rb") as fh:
        la = float(pickle.load(fh)["info"].get("lookahead_s", 0.75))
    ev, _ = S.load_folds(exp, "causal")
    per = {}
    for ds in DS_KEYS:
        A = np.concatenate([(e["r0"] - e["r1"]) * np.where(e["att"] == 0, 1.0, -1.0)
                            for d in DS_KEYS if d != ds for e in ev[d].values()])
        mu, sd = float(A.mean()), float(A.std())
        r = S.score_dataset(ds, ev, la, emis=(mu, sd * SCALE), p_stay=P_STAY)
        per[ds] = r
        log(f"  PRESET LODO {ds}  H {r['H']:.4f}  Acc {r['Acc']:.2f}  L {r['L']:.3f}")
    mH = float(np.mean([per[d]["H"] for d in DS_KEYS]))
    mA = float(np.mean([per[d]["Acc"] for d in DS_KEYS]))
    log(f"  PRESET LODO MEAN  H {mH:.4f}  Acc {mA:.2f}")

    # hp30
    hp_ev = {}
    for ds in DS_KEYS:
        hp_ev[ds] = {}
        for key, e in ev[ds].items():
            signed = (e["r0"] - e["r1"]).astype(np.float64)
            hp = V4.highpass(signed[:, None], tau=30.0, hop=0.25)[:, 0]
            hp_ev[ds][key] = dict(e)
            hp_ev[ds][key]["r0"] = (hp / 2).astype(np.float32)
            hp_ev[ds][key]["r1"] = (-hp / 2).astype(np.float32)
    hp_per = {}
    for ds in DS_KEYS:
        A = np.concatenate([(e["r0"] - e["r1"]) * np.where(e["att"] == 0, 1.0, -1.0)
                            for d in DS_KEYS if d != ds for e in hp_ev[d].values()])
        mu, sd = float(A.mean()), float(A.std())
        hp_per[ds] = S.score_dataset(ds, hp_ev, la, emis=(mu, sd * SCALE), p_stay=P_STAY)
    hp_mH = float(np.mean([hp_per[d]["H"] for d in DS_KEYS]))
    hp_mA = float(np.mean([hp_per[d]["Acc"] for d in DS_KEYS]))
    log(f"  HP30 LODO MEAN  H {hp_mH:.4f}  Acc {hp_mA:.2f}")

    # external: fit all-5 filters
    A = np.concatenate([(e["r0"] - e["r1"]) * np.where(e["att"] == 0, 1.0, -1.0)
                        for ds in DS_KEYS for e in ev[ds].values()])
    mu, sd = float(A.mean()), float(A.std())
    log(f"  external emissions mu {mu:.4f} sd {sd:.4f}")

    max_lag = int(round(MAX_LAG_MS / 1000 * FS))
    train = gather_train(None, PREP, log)
    filt = fit_filters(train, kind, max_lag, log)
    np.savez(os.path.join(CKPT_DIR, f"all5_{kind}.npz"),
             kind=filt.kind, max_lag=filt.max_lag, W=filt.W, ridge=filt.ridge, n_comp=filt.n_comp)
    del train

    E.CONF.update(prep=PREP, targets=["env"])
    os.makedirs(EXT_DIR, exist_ok=True)
    ext = {}
    hp_parts = []
    for ds in DS_KEYS:
        for e in ev[ds].values():
            signed = (e["r0"] - e["r1"]).astype(np.float64)
            hp = V4.highpass(signed[:, None], tau=30.0, hop=0.25)[:, 0]
            hp_parts.append(hp * np.where(e["att"] == 0, 1.0, -1.0))
    mu_hp = float(np.concatenate(hp_parts).mean())
    sd_hp = float(np.concatenate(hp_parts).std())

    for ds in ("AASD", "Carta2026"):
        path = os.path.join(EXT_DIR, f"ext_trf_lagbank_{ds}{TAG}_{kind}_evidence.pkl")
        if os.path.exists(path):
            with open(path, "rb") as fh:
                EV = pickle.load(fh)
            log(f"  loaded {path}")
        else:
            EV = {}
            trials = E.ext_trials(ds)
            log(f"  external {ds}: {len(trials)} trials")
            for i, tr in enumerate(trials):
                e, _ = evidence_trial(filt, tr)
                e.update(att128=tr["att128"], n128=tr["n128"], subject=tr["subject"])
                EV[tr["key"]] = e
                if (i + 1) % 40 == 0 or i + 1 == len(trials):
                    log(f"    {ds} {i + 1}/{len(trials)}")
            with open(path, "wb") as fh:
                pickle.dump(EV, fh)
        r = E.score(EV, mu, sd * SCALE, P_STAY, 0.75)
        EV_hp = {}
        for key, e in EV.items():
            signed = (e["r0"] - e["r1"]).astype(np.float64)
            hp = V4.highpass(signed[:, None], tau=30.0, hop=0.25)[:, 0]
            EV_hp[key] = dict(e)
            EV_hp[key]["r0"] = (hp / 2).astype(np.float32)
            EV_hp[key]["r1"] = (-hp / 2).astype(np.float32)
        r_hp = E.score(EV_hp, mu_hp, sd_hp * SCALE, P_STAY, 0.75)
        plain = E.plain(EV)
        ext[ds] = dict(raw=r, hp30=r_hp, plain=plain)
        log(f"  PRESET EXT {ds}  H {r['H']:.4f} Acc {r['Acc']:.2f}  plain_acc1 {plain['acc1']:.3f}")
        log(f"  PRESET EXT_hp30 {ds}  H {r_hp['H']:.4f} Acc {r_hp['Acc']:.2f}")
    return per, ext, mu, sd, mH, mA, hp_per, hp_mH, hp_mA, la


def write_results(kind, per, ext, mH, mA, hp_per, hp_mH, hp_mA, probe, path_note):
    lines = [
        "# results_trf_lagbank",
        "",
        f"**Method: trf-lagbank-clf / Path A ({kind})** — frozen train-only "
        f"{'ridge TRF (EEG→env)' if kind=='trf' else 'spatial CCA'} filters + lag-bank "
        f"attended−unattended correlation scores as HMM emissions (no deep net).",
        "",
        f"Distinct from decoder CCA/FBCCA/WF reconstruction benchmarks and from xcorr CNN.",
        f"Prep `eaP_n5_t60`, max_lag={MAX_LAG_MS} ms @ {FS} Hz, ridge_rel={RIDGE_REL}. "
        f"HMM p_stay {P_STAY} scale {SCALE}. Also causal hp30.",
        "",
        f"Code: `trf_lagbank_clf.py`. Checkpoints: `models/trf_lagbank/`. JSON: `{OUT_JSON}`.",
        "",
        f"## Path / probe",
        "",
        f"- Chosen: **Path A / {kind}** ({path_note})",
        f"- AVGC probe acc1={probe.get('acc1')} snr={probe.get('snr')} "
        f"swapΔ={probe.get('swap_delta')}",
        "",
        "## LODO (preset)",
        "",
        "| Held-out | H | Acc % | hp30 H/Acc |",
        "|---|---:|---:|---|",
    ]
    for ds in DS_KEYS:
        r, h = per[ds], hp_per[ds]
        lines.append(f"| {ds} | {r['H']:.4f} | {r['Acc']:.2f} | {h['H']:.4f}/{h['Acc']:.2f} |")
    lines += [
        f"| **mean** | **{mH:.4f}** | **{mA:.2f}** | **{hp_mH:.4f}/{hp_mA:.2f}** |",
        "",
        "## External (preset)",
        "",
        "| Set | H | Acc % | hp30 H/Acc | vs linear_hp30 53.12 / SW2 53.46 |",
        "|---|---:|---:|---|---|",
        f"| AASD | {ext['AASD']['raw']['H']:.4f} | {ext['AASD']['raw']['Acc']:.2f} | "
        f"{ext['AASD']['hp30']['H']:.4f}/{ext['AASD']['hp30']['Acc']:.2f} | "
        f"Δ {ext['AASD']['raw']['Acc']-LINEAR_HP30_AASD:+.2f} / "
        f"{ext['AASD']['raw']['Acc']-SW2_AASD:+.2f} |",
        f"| Carta2026 | {ext['Carta2026']['raw']['H']:.4f} | {ext['Carta2026']['raw']['Acc']:.2f} | "
        f"{ext['Carta2026']['hp30']['H']:.4f}/{ext['Carta2026']['hp30']['Acc']:.2f} | — |",
        "",
        "## Targets",
        "",
        "| Target | Result |",
        "|---|---|",
        f"| LODO mean H≥0.85 Acc≥78 | "
        f"{'met' if mH >= 0.85 and mA >= 78 else 'miss'} ({mH:.4f}/{mA:.2f}) |",
        f"| AASD Acc>60 vs 53.12/53.46 | "
        f"{'met' if ext['AASD']['raw']['Acc'] > 60 else 'miss'} "
        f"({ext['AASD']['raw']['Acc']:.2f}) |",
        "",
        "## NEXT IDEA",
        "",
        "If Acc flat: try dual TRF+CCA bank fused by train-only LDA on lag dims, or "
        "band-limited (1–8 Hz) envelopes before TRF fit — do not reopen xcorr CNN / "
        "vlaai seeds / fuse↔hp30 / SW2 bank grids.",
        "",
    ]
    open(RESULTS_MD, "w").write("\n".join(lines) + "\n")
    return RESULTS_MD


def write_abandon(reason, probe=None):
    lines = [
        "# results_trf_lagbank",
        "",
        "**Method: trf-lagbank-clf**. Path **abandoned**.",
        "",
        f"Reason: {reason}",
        "",
    ]
    if probe:
        lines.append(f"Probe: {probe}")
        lines.append("")
    lines += [
        "NEXT IDEA: band-limited TRF (1–8 Hz) or dual TRF↔CCA LDA bank; "
        "do not reopen xcorr CNN / decoder CCA/FBCCA as full architectures / vlaai seeds.",
        "",
    ]
    open(RESULTS_MD, "w").write("\n".join(lines) + "\n")


def update_claims(status, note):
    path = os.path.join(EXP, "multi_agent_opt", "CLAIMS.md")
    text = open(path).read()
    old = ("| 2026-10-29 14:35 +07 | trf-lagbank-clf | Frozen train-only multivariate TRF/CCA "
           "spatial filters + lag-bank attended−unattended scores → small linear/CNN head; eaP; "
           "swap+chance abort; ≠ decoder CCA/FBCCA benchmark, ≠ xcorr CNN | claimed | "
           "GPU Acc path; clear chance first. |")
    # Also match running
    for st0 in ("claimed", "running"):
        old_st = old.replace("| claimed |", f"| {st0} |")
        if old_st in text:
            new = old_st.replace(f"| {st0} |", f"| {status} |").replace(
                "GPU Acc path; clear chance first.", note)
            open(path, "w").write(text.replace(old_st, new, 1))
            return
    # fallback: append
    open(path, "a").write(
        f"\n| 2026-10-29 +07 | trf-lagbank-clf | trf-lagbank Path A | {status} | {note} |\n")


def main():
    os.makedirs(os.path.dirname(LOG_PATH), exist_ok=True)
    os.makedirs(CKPT_DIR, exist_ok=True)
    os.makedirs(os.path.dirname(OUT_JSON), exist_ok=True)
    log = log_to(LOG_PATH)
    max_lag = int(round(MAX_LAG_MS / 1000.0 * FS))
    log(f"trf-lagbank-clf start PATH={PATH} max_lag={max_lag} ({MAX_LAG_MS} ms) "
        f"ridge={RIDGE_REL} probe_only={PROBE_ONLY}")
    update_claims("running", "Path A CPU TRF/CCA lag-bank probe→score")

    kinds = {"trf": ["trf"], "cca": ["cca"], "both": ["trf", "cca"]}[PATH]
    probes = {}
    best = None
    for kind in kinds:
        ok, acc1, snr, sp, filt = avgc_probe(kind, max_lag, log)
        probes[kind] = dict(ok=ok, acc1=acc1, snr=snr, **sp)
        if ok and (best is None or acc1 > best[1]):
            best = (kind, acc1, snr, sp)
        # Path B only if Path A fails gate
        if not ok:
            train = gather_train("AVGC", PREP, log)
            mean_loss, b_ok = path_b_chance_probe(filt, train, log)
            probes[kind]["path_b_loss"] = mean_loss
            probes[kind]["path_b_ok"] = b_ok
            del train
            if not b_ok:
                log(f"  Path B also chance for {kind}; skip")

    if best is None:
        # If Path A failed gate but still >chance mildly, take best acc1 anyway if >=0.52
        cand = max(probes.items(), key=lambda kv: kv[1]["acc1"])
        if cand[1]["acc1"] >= 0.52 and cand[1]["swap_delta"] >= 0.01:
            kind = cand[0]
            log(f"  soft-pass: using {kind} acc1={cand[1]['acc1']:.4f} despite gate {ACC1_GATE}")
            best = (kind, cand[1]["acc1"], cand[1]["snr"], cand[1])
        else:
            write_abandon("AVGC Path A below chance gate and Path B chance-stuck", probes)
            update_claims("abandoned", f"probe fail {probes}")
            log("ABANDONED")
            return 3

    kind = best[0]
    if PROBE_ONLY:
        log(f"PROBE_ONLY done best={kind} {best[1]:.4f}")
        update_claims("running", f"probe PASS {kind} acc1={best[1]:.4f}; full score pending")
        return 0

    exp = f"{EXP_NAME}_{kind}"
    log(f"=== Full LODO kind={kind} exp={exp} ===")
    # AVGC fold may already exist under probe name — re-run all 5 into exp
    for held in DS_KEYS:
        run_fold(held, kind, max_lag, exp, log)

    per, ext, mu, sd, mH, mA, hp_per, hp_mH, hp_mA, la = score_all(exp, kind, log)
    probe = probes[kind]
    write_results(kind, per, ext, mH, mA, hp_per, hp_mH, hp_mA, probe,
                  "frozen lag-bank scores as HMM emissions")
    out = dict(
        method="trf-lagbank-clf", path="A", kind=kind, prep=PREP,
        max_lag_ms=MAX_LAG_MS, ridge_rel=RIDGE_REL, p_stay=P_STAY, scale=SCALE,
        lookahead_s=la, probe=probes, lodo=per, lodo_mean=dict(H=mH, Acc=mA),
        lodo_hp30={d: hp_per[d] for d in DS_KEYS},
        lodo_hp30_mean=dict(H=hp_mH, Acc=hp_mA),
        external=ext, emis=dict(mu=mu, sd=sd),
        baselines=dict(linear_hp30_aasd=LINEAR_HP30_AASD, sw2_aasd=SW2_AASD),
        exp=exp,
    )
    json.dump(out, open(OUT_JSON, "w"), indent=1, default=float)
    note = (f"Path A {kind}: LODO {mH:.4f}/{mA:.2f}; "
            f"AASD {ext['AASD']['raw']['Acc']:.2f}; Carta {ext['Carta2026']['raw']['Acc']:.2f}. "
            f"`results_trf_lagbank.md`.")
    update_claims("done", note)
    # ALREADY_TRIED + STATUS_GAPS appended by caller finish
    log(f"DONE {note}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
