"""T5 in-trial distractor ranking + linear_hp30 readout (inference-only).

NEW vs ALREADY_TRIED / proposals:
  - Score each stream vs shared past-envelope distractors N_k (shift ≥8 s),
    d = mean_k[f(E,A,N_k) − f(E,B,N_k)], then causal τ=30 HP.
  - ≠ SparrKULee match/mismatch pretrain, ≠ CF-Null, ≠ seed-disagree gates,
    ≠ SW2↔hp30 soft blends, ≠ speech-resid FiLM.
  - SCREEN: seed0 all5, first N AASD trials, frame Acc vs baseline mean logit.
    Proceed to full LODO+ext only if AASD frame Acc lifts ≥ +1.0 pt.

usage:
  GLIMPSE_THR=onorm SCREEN=1 .../python -u t5_distractor_hp30.py
  FULL=1 ...  # after screen pass
"""
from __future__ import annotations

import json
import os
import time

import numpy as np
import torch

os.environ.setdefault("GLIMPSE_THR", "onorm")

import direct_classifier as DC
import ensemble_clf as EC
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, DATA_EXP, EXP, FS, load_trial, rows_of
from decoders import D, DEV, _targets
from e1_switch import draw_switch_times

RUN_ID = "t5_distractor_hp30_20261107"
RUN_DIR = os.path.join(EXP, "experiments", "multi_agent_opt", "runs", RUN_ID)
LOG = os.path.join(RUN_DIR, "run.log")
OUT_JSON = os.path.join(EXP, "results", "research", "t5_distractor_hp30.json")
OUT_MD = os.path.join(EXP, "multi_agent_opt", "results_t5_distractor_hp30.md")
CKPT_DIR = os.path.join(EXP, "models", "t5_distractor_hp30")

P_STAY, SCALE = 0.994, 0.8
LOOKAHEAD = 0.75
HP_TAU = 30.0
SHIFT_S = float(os.environ.get("SHIFT_S", "8"))
K = int(os.environ.get("K", "2"))
SCREEN_N = int(os.environ.get("SCREEN_N", "80"))
SCREEN = os.environ.get("SCREEN", "1") == "1"
FULL = os.environ.get("FULL", "0") == "1"
TEST_NAMES = ("AASD", "Carta2026")


def log(msg: str) -> None:
    print(f"{time.strftime('%F %T')} {msg}", flush=True)
    os.makedirs(RUN_DIR, exist_ok=True)
    with open(LOG, "a") as fh:
        fh.write(msg + "\n")


def configure():
    D["window_s"] = 10.0
    D["model_blocks"] = 3
    D["targets"] = ["env", "onset", "dom"]
    D["causal_future_s"] = float(CFG["dnn"].get("causal_future_s", 0.5))
    DC.HOP = int(0.25 * FS)
    DC.FUT = int(D["causal_future_s"] * FS)
    DC.W = int(10 * FS)
    DC.POS0, DC.LAB = DC.W - DC.FUT - DC.HOP, DC.W - FS


def model_path(name):
    for root in (os.path.join(DATA_EXP, "models"), os.path.join(EXP, "models")):
        p = os.path.join(root, name)
        if os.path.exists(p):
            return p
    raise FileNotFoundError(name)


def load_net(path):
    net = DC.DirectNet(58, 3, "vlaai_ws").to(DEV)
    net.load_state_dict(torch.load(path, map_location=DEV, weights_only=False))
    net.eval()
    return net


def shift_feat(S, lag):
    """Causal past shift of both streams by `lag` samples (zeros at start)."""
    if lag <= 0:
        return S
    out = np.zeros_like(S)
    out[lag:] = S[:-lag]
    return out


@torch.no_grad()
def distractor_logit(net, x, S):
    """d = mean_k [ g(E,A,N_k) - g(E,B,N_k) ] with N_k past shifts of A/B."""
    lag0 = int(SHIFT_S * FS)
    lags = [lag0 * (i + 1) for i in range(K)]
    # baseline pairwise AB
    parts = []
    # Standard A vs B
    lg_ab, starts = DC.logits(net, x, S)
    parts.append(lg_ab)
    # Distractors: replace unattended-side with past of each stream
    A = S[:, 0:1, :]
    B = S[:, 1:2, :]
    for lag in lags:
        Np = shift_feat(S, lag)
        # A vs N (past mix): use N as stream B
        S_an = np.concatenate([A, Np[:, 1:2, :]], 1)
        lg_an, _ = DC.logits(net, x, S_an)
        # N vs B: use N as stream A  
        S_nb = np.concatenate([Np[:, 0:1, :], B], 1)
        lg_nb, _ = DC.logits(net, x, S_nb)
        # Ranking: how much A beats distractor minus how much B beats distractor
        # lg_an ≈ g(E,A,N)-g(E,N,A); lg_nb ≈ g(E,N,B)-g(E,B,N) = -(g(E,B,N)-g(E,N,B))
        parts.append(0.5 * (lg_an - lg_nb))
    d = np.mean(parts, 0)
    return d, starts


def frame_acc(d, att, starts):
    lab = att[np.maximum(starts - DC.HOP, 0)] == 0
    return float(((d > 0) == lab).mean())


def screen_aasd(net):
    trials = E.ext_trials("AASD")[:SCREEN_N]
    log(f"SCREEN AASD n={len(trials)} K={K} shift={SHIFT_S}s")
    acc_base, acc_t5, acc_t5hp = [], [], []
    for i, tr in enumerate(trials):
        S = _targets(tr)
        x = tr["eeg"]
        att = tr["att"].astype(int)
        lg, starts = DC.logits(net, x, S)
        d5, _ = distractor_logit(net, x, S)
        hp = V4.highpass(d5[:, None], tau=HP_TAU, hop=0.25)[:, 0]
        acc_base.append(frame_acc(lg, att, starts))
        acc_t5.append(frame_acc(d5, att, starts))
        acc_t5hp.append(frame_acc(hp, att, starts))
        if (i + 1) % 20 == 0:
            log(f"  {i+1}/{len(trials)} base={np.mean(acc_base):.4f} t5={np.mean(acc_t5):.4f} t5hp={np.mean(acc_t5hp):.4f}")
    out = dict(
        base=float(np.mean(acc_base)),
        t5=float(np.mean(acc_t5)),
        t5_hp30=float(np.mean(acc_t5hp)),
        n=len(trials),
    )
    out["delta_pt"] = (out["t5_hp30"] - out["base"]) * 100.0
    log(f"SCREEN frame Acc base={out['base']:.4f} t5={out['t5']:.4f} "
        f"t5_hp30={out['t5_hp30']:.4f} Δ={out['delta_pt']:+.2f} pt")
    return out


def held_evidence(nets, held):
    srng = np.random.default_rng(SEED + DS_KEYS.index(held))
    ev = {}
    for _, r in rows_of(held).iterrows():
        tr = load_trial("eaP_n5_t60", r["key"])
        S0, x, att, T128 = _targets(tr), tr["eeg"], tr["att"].astype(int), int(r["n"])
        times = draw_switch_times(srng, T128 / 128)
        srng.integers(0, 2)
        st128, cur, prev = np.zeros(T128, bool), False, 0
        for tt in times:
            s0 = min(T128 - 1, int(round(tt * 128)))
            st128[prev:s0] = cur
            cur, prev = not cur, s0
        st128[prev:] = cur
        st = st128[np.minimum(T128 - 1, np.round((np.arange(len(x)) / FS + tr["t0"]) * 128).astype(int))]
        Ssw = np.where(st[:, None, None], S0[:, ::-1], S0)
        parts = []
        starts = None
        for net in nets:
            d, starts = distractor_logit(net, x, Ssw)
            parts.append(d)
        d = np.mean(parts, 0)
        hp = V4.highpass(d[:, None], tau=HP_TAU, hop=0.25)[:, 0]
        truth = (att ^ st)[np.maximum(starts - DC.HOP, 0)]
        ev[r["key"]] = dict(
            r0=hp / 2, r1=-hp / 2, att=truth.astype(np.int8),
            t_end=(starts / FS + tr["t0"]).astype(np.float32), pre_switched=True,
        )
    return ev


def score_lodo(ev):
    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, LOOKAHEAD, emis=(mu, sd * SCALE), p_stay=P_STAY)
        per[ds] = {k: float(r[k]) for k in ("H", "Acc", "L", "SDT")}
        log(f"  LODO {ds} H={per[ds]['H']:.4f} Acc={per[ds]['Acc']:.2f}")
    mean = {k: float(np.mean([per[d][k] for d in DS_KEYS])) for k in ("H", "Acc", "L", "SDT")}
    log(f"  LODO MEAN H={mean['H']:.4f} Acc={mean['Acc']:.2f}")
    return per, mean


def main():
    t0 = time.time()
    os.makedirs(CKPT_DIR, exist_ok=True)
    configure()
    E.CONF.update(prep="eaP_n5_t60", targets=list(D["targets"]))
    log(f"=== T5 distractor+hp30 SCREEN={SCREEN} FULL={FULL} K={K} ===")

    all5 = model_path("all5_direct_clf_w10b3sw2P_s0.pt")
    net0 = load_net(all5)

    if SCREEN:
        scr = screen_aasd(net0)
        if scr["delta_pt"] < 1.0:
            log("SCREEN FAIL: AASD frame Acc lift < +1 pt — abort full")
            payload = dict(method="t5_distractor_hp30", screen=scr, verdict="FAIL_SCREEN",
                           elapsed_s=time.time() - t0)
            with open(OUT_JSON, "w") as fh:
                json.dump(payload, fh, indent=2)
            with open(OUT_MD, "w") as fh:
                fh.write(
                    "# results_t5_distractor_hp30\n\n"
                    f"SCREEN AASD n={scr['n']}: base={scr['base']:.4f} t5_hp30={scr['t5_hp30']:.4f} "
                    f"Δ={scr['delta_pt']:+.2f} pt (< +1 abort).\n"
                    "Do not rerun this distractor K/shift grid on frozen SW2+hp30.\n"
                )
            return 1
        log("SCREEN PASS — continuing FULL")
        do_full = True
    else:
        do_full = FULL
        scr = None

    if not do_full:
        return 0

    # Full: 4 all5 seeds for external; 4 fold soups per held for LODO would be 20 nets —
    # use 4 all5 for external and LODO fold seed0-3 published fold weights per held.
    fold_tags = [
        "fold_direct_clf__eaP_n5_t60_w10b3_ptSPK_aug-sw2_wts__{h}.pt",
        "fold_direct_clf__eaP_n5_t60_w10b3_ptSPK_s1_aug-sw2_wts__{h}.pt",
        "fold_direct_clf__eaP_n5_t60_w10b3_ptSPK_s2_aug-sw2_wts__{h}.pt",
        "fold_direct_clf__eaP_n5_t60_w10b3_ptSPK_s3_aug-sw2_wts__{h}.pt",
    ]
    lodo = {}
    for held in DS_KEYS:
        nets = [load_net(model_path(t.format(h=held))) for t in fold_tags]
        log(f"LODO held={held}")
        lodo[held] = held_evidence(nets, held)
        del nets
        torch.cuda.empty_cache()
    per, mean = score_lodo(lodo)

    # external all5 × 4 seeds
    nets = [load_net(model_path(f"all5_direct_clf_w10b3sw2P_s{i}.pt")) for i in range(4)]
    mu, sd = None, None
    A = np.concatenate([
        (e["r0"] - e["r1"]) * np.where(e["att"] == 0, 1.0, -1.0)
        for ds in DS_KEYS for e in lodo[ds].values()
    ])
    mu, sd = float(A.mean()), float(A.std())
    ext = {}
    for ds in TEST_NAMES:
        trials = E.ext_trials(ds)
        log(f"EXT {ds} n={len(trials)}")
        EV = {}
        for i, tr in enumerate(trials):
            parts = []
            starts = None
            for net in nets:
                d, starts = distractor_logit(net, tr["eeg"], _targets(tr))
                parts.append(d)
            d = np.mean(parts, 0)
            hp = V4.highpass(d[:, None], tau=HP_TAU, hop=0.25)[:, 0]
            truth = tr["att"].astype(int)[np.maximum(starts - DC.HOP, 0)]
            EV[tr["key"]] = dict(
                r0=hp / 2, r1=-hp / 2, att=truth.astype(np.int8),
                t_end=(starts / FS + tr["t0"]).astype(np.float32),
                att128=tr["att128"], n128=tr["n128"], subject=tr["subject"],
            )
            if (i + 1) % 100 == 0:
                log(f"  {ds} {i+1}/{len(trials)}")
        r = E.score(EV, mu, sd * SCALE, P_STAY, LOOKAHEAD)
        ext[ds] = {k: float(r[k]) for k in ("H", "Acc", "L", "SDT")}
        log(f"EXT {ds} H={ext[ds]['H']:.4f} Acc={ext[ds]['Acc']:.2f}")

    ok = (mean["H"] > 0.80 and mean["Acc"] > 70 and
          all(ext[ds]["H"] > 0.60 and ext[ds]["Acc"] > 60 for ds in TEST_NAMES))
    payload = dict(
        method="t5_distractor_hp30", screen=scr, lodo=dict(per=per, mean=mean),
        ext=ext, verdict="PASS" if ok else "FAIL", elapsed_s=time.time() - t0,
        inherited="causal τ=30 HP on distractor-ranked logits",
    )
    with open(OUT_JSON, "w") as fh:
        json.dump(payload, fh, indent=2)
    with open(OUT_MD, "w") as fh:
        fh.write(
            "# results_t5_distractor_hp30\n\n"
            f"LODO {mean['H']:.4f}/{mean['Acc']:.2f}\n"
            f"AASD {ext['AASD']['H']:.4f}/{ext['AASD']['Acc']:.2f}\n"
            f"Carta {ext['Carta2026']['H']:.4f}/{ext['Carta2026']['Acc']:.2f}\n"
            f"Verdict: {payload['verdict']}\n"
        )
    log(f"VERDICT {payload['verdict']}")
    return 0 if ok else 1


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