"""Present-channel mask + electrode XY FiLM on the published 10 s vlaai_ws classifier.

STEP A confirmed zero-fill into the 58-channel b64ustc58 union (missing electrode names
are left as zeros; UHD lacks FCZ in that list). This run makes that metadata explicit:
each trial carries a binary present mask (eeg.std(0) > 0) and the planar 10-10 XY of
prepare_data._LAP_XY. A zero-init Linear(3 -> 2) maps [mask, x, y] to (gain, bias) so
the network starts identical to the SparrKULee-initialised baseline, then can learn to
treat absent (zero-filled) channels differently from real silence.

Recipe: prep eaP_n5_t60, window 10 s, 3 blocks, sw2, SparrKULee init, seed 20270101,
Fuglsang extra train. ~1000 steps/fold (4 epochs x 250). Headline HMM p_stay 0.994,
emission scale 0.8, emissions from LODO folds only (no AASD/Carta label tuning).
"""
import json
import os
import pickle
import time

import numpy as np
import torch

import decoders
import direct_classifier as DC
import eval_external as E
import prepare_data as PD
import score as S
from common import DS_KEYS, MONTAGES
from common_exp import CFG, EXP, FS, fold_file, load_trial, rows_of
from decoders import D, DEV, _targets
from run_experiment import load_external, log_to

EXP_NAME = "direct_clf__eaP_n5_t60_w10b3_ptSPK_chanmask"
INIT = ("/root/disk_3tb/EEG-sAAD-Grand-Challenge-Training-Dataset/experiments/decoder_lodo/models/"
        "pretrain_spk_direct_clf_w10_b3_s0.pt")
LOG_PATH = os.path.join(EXP, "experiments", "channel_mask_clf.log")
OUT_JSON = os.path.join(EXP, "results", f"{EXP_NAME}.json")
EXT_DIR = os.path.join(EXP, "results", "research")
ALL5_PT = os.path.join(EXP, "models", "all5_direct_clf_chanmask_s0.pt")
TAG = "_w10b3_ptSPK_chanmask"
P_STAY, SCALE = 0.994, 0.8
MAX_EPOCHS = 4  # 4 * 250 steps_per_epoch = 1000 steps/fold

_ORIG_INIT = DC.DirectNet.__init__
_ORIG_FWD = DC.DirectNet.forward
_ORIG_LOGITS = DC.logits
_ORIG_WINDOWS = DC.windows
_ORIG_FIT = DC.fit

NAMES = MONTAGES[CFG["montage"]]
XY = np.array([PD._LAP_XY[c] for c in NAMES], dtype=np.float32)


def present_mask(eeg):
    return (np.asarray(eeg, np.float64).std(0) > 0).astype(np.float32)


def attach_masks(trials):
    for tr in trials:
        if "_mask" not in tr:
            tr["_mask"] = present_mask(tr["eeg"])


def DirectNet_init(self, C, Fs, backbone="vlaai_ws"):
    _ORIG_INIT(self, C, Fs, backbone)
    self.ch_film = torch.nn.Linear(3, 2)
    torch.nn.init.zeros_(self.ch_film.weight)
    torch.nn.init.zeros_(self.ch_film.bias)
    self.register_buffer("ch_xy", torch.tensor(XY, dtype=torch.float32))
    self._infer_mask = None


def cond_eeg(self, E, mask):
    """E (B,T,58), mask (B,58). Zero-init FiLM => E * mask at start."""
    feat = torch.cat([mask.unsqueeze(-1), self.ch_xy.expand(mask.shape[0], -1, -1)], dim=-1)
    gb = self.ch_film(feat)
    gain = 1.0 + gb[..., 0]
    bias = gb[..., 1]
    m = mask.unsqueeze(1)
    return (E * gain.unsqueeze(1) + bias.unsqueeze(1)) * m


def resolve_mask(self, E):
    if getattr(self, "_infer_mask", None) is not None:
        mask = self._infer_mask
        if mask.ndim == 1:
            mask = mask.unsqueeze(0).expand(E.shape[0], -1)
        return mask.to(device=E.device, dtype=E.dtype)
    raw = getattr(DC.windows, "last_mask", None)
    if raw is not None and len(raw) == E.shape[0]:
        return torch.as_tensor(np.stack(raw), device=E.device, dtype=E.dtype)
    return (E.detach().std(1) > 1e-6).to(E.dtype)


def DirectNet_forward(self, E, S, feat=False):
    E = cond_eeg(self, E, resolve_mask(self, E))
    return _ORIG_FWD(self, E, S, feat=feat)


def windows_masked(trials, n, rng, aug, p_sw, zero_eeg):
    out = _ORIG_WINDOWS(trials, n, rng, aug, p_sw, zero_eeg)
    ti = windows_masked.last_ti
    windows_masked.last_mask = [trials[int(i)]["_mask"] for i in ti]
    return out


@torch.no_grad()
def logits_masked(net, x, S, step=1):
    mask = torch.from_numpy(present_mask(x)).to(DEV)
    net._infer_mask = mask
    try:
        return _ORIG_LOGITS(net, x, S, step=step)
    finally:
        net._infer_mask = None


def fit_masked(train, val, log, a, init_from=None):
    """Same as DC.fit but loads SparrKULee weights with strict=False (extra ch_film)."""
    attach_masks(train)
    attach_masks(val)
    Fs = train[0]["_S"].shape[-1]
    seed = D.get("train_seed", CFG["seed"])
    torch.manual_seed(seed)
    net = DC.DirectNet(58, Fs, a.backbone).to(DEV)
    if init_from:
        from common_exp import resolve_model
        init_from = resolve_model(init_from)
        sd = torch.load(init_from, map_location=DEV, weights_only=False)
        missing, unexpected = net.load_state_dict(sd, strict=False)
        log(f"  initialised from {init_from} (strict=False; missing {len(missing)} unexpected {len(unexpected)})")
    aug = decoders.DNNDec.__new__(decoders.DNNDec)
    aug.W, aug.fut, aug.hop, aug.C = DC.W, DC.FUT, DC.HOP, 58
    rs = np.random.default_rng(seed)
    Xv, Sv, yv = DC.windows(val, D["val_windows"], rs, None, a.p_switch, a.zero_eeg)
    params = list(net.parameters())
    opt = torch.optim.AdamW(params, lr=D["lr"], weight_decay=D["weight_decay"])
    sch = torch.optim.lr_scheduler.ReduceLROnPlateau(opt, mode="min", factor=0.5, patience=D["lr_patience"])
    bce = torch.nn.functional.binary_cross_entropy_with_logits
    best, bad, state = np.inf, 0, None
    for ep in range(D["max_epochs"]):
        t1, tl = time.time(), []
        net.train()
        for _ in range(D["steps_per_epoch"]):
            X, S, y = [z.to(DEV) for z in DC.windows(train, D["batch_size"], rs, aug, a.p_switch, a.zero_eeg)]
            loss = bce(net(X, S), y)
            tl.append(loss.item())
            opt.zero_grad()
            loss.backward()
            torch.nn.utils.clip_grad_norm_(params, 5.0)
            opt.step()
        net.eval()
        with torch.no_grad():
            lv = torch.cat([net(Xv[i:i + 256].to(DEV), Sv[i:i + 256].to(DEV)).cpu()
                            for i in range(0, len(Xv), 256)])
        vl, va = bce(lv, yv).item(), ((lv > 0).float() == yv).float().mean().item()
        if ep >= a.min_epochs:
            sch.step(vl)
        log(f"    epoch {ep:02d} train {np.mean(tl):.4f} val BCE {vl:.4f} acc {va:.3f} ({time.time() - t1:.0f}s)")
        if vl < best - 1e-4:
            best, bad, state = vl, 0, {k: v.detach().clone() for k, v in net.state_dict().items()}
        elif ep >= a.min_epochs:
            bad += 1
            if bad >= D["patience"]:
                break
    net.load_state_dict(state)
    net.eval()
    return net, best


def run_fold_masked(held, exp, log, a):
    """DC.run_fold with attach_masks + logits_masked (already patched as DC.logits)."""
    rng = np.random.default_rng(CFG["seed"] + DS_KEYS.index(held))
    train, val = [], []
    for ds in DS_KEYS:
        if ds == held:
            continue
        rows = rows_of(ds)
        vs = rng.choice(sorted(rows.subject.unique()))
        for _, r in rows.iterrows():
            (val if r["subject"] == vs else train).append(load_trial(a.prep, r["key"]))
    train += load_external(["Fuglsang2020"], a.prep, log)
    for tr in train + val:
        tr["_S"] = _targets(tr)
    attach_masks(train)
    attach_masks(val)
    log(f"  fold held-out={held}: {len(train)} train / {len(val)} val; "
        f"present-ch mean {np.mean([m.sum() for m in (t['_mask'] for t in train)]):.1f}/58")
    t0 = time.time()
    net, best = fit_masked(train, val, log, a, a.init_from)
    t_fit = time.time() - t0
    del train, val
    from e1_switch import draw_switch_times
    from common import SEED
    srng = np.random.default_rng(SEED + DS_KEYS.index(held))
    ev, ctl = {}, {k: {} for k in ("normal", "zero_eeg", "shift_eeg", "no_frontal")}
    for _, r in rows_of(held).iterrows():
        tr = load_trial(a.prep, r["key"])
        attach_masks([tr])
        Sfeat, 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], Sfeat[:, ::-1], Sfeat)
        xe = np.zeros_like(x) if a.zero_eeg else x
        lg, starts = DC.logits(net, xe, Ssw)
        truth = (att ^ st)[np.maximum(starts - DC.HOP, 0)]
        n10 = len(lg) // 40
        ev[r["key"]] = dict(
            r0=lg / 2, r1=-lg / 2, att=truth.astype(np.int8),
            t_end=(starts / FS + tr["t0"]).astype(np.float32),
            r0_10=lg[:n10 * 40].reshape(n10, 40).mean(1) / 2,
            r1_10=-lg[:n10 * 40].reshape(n10, 40).mean(1) / 2,
            att10=(truth[:n10 * 40].reshape(n10, 40).mean(1) > 0.5).astype(np.int8),
            pre_switched=True)
        xf = x.copy()
        xf[:, DC.FRONTAL] = 0.0
        for name, xc in (("normal", xe), ("zero_eeg", np.zeros_like(x)),
                         ("shift_eeg", np.roll(xe, len(x) // 2, 0)), ("no_frontal", xf)):
            l1, s1 = DC.logits(net, xc, Sfeat, step=4)
            ctl[name].setdefault(r["subj_uid"], []).append((l1 > 0) == (att[np.maximum(s1 - DC.HOP, 0)] == 0))
    controls = {k: dict(
        pooled=float(np.mean(np.concatenate([np.concatenate(v) for v in d.values()]))),
        subject_median=float(np.median([np.mean(np.concatenate(v)) for v in d.values()])))
        for k, d in ctl.items()}
    info = dict(decoder="direct_clf", prep=a.prep, held_out=held, fit_s=t_fit,
                total_s=time.time() - t0, lookahead_s=(DC.FUT + DC.HOP) / FS,
                best_val=best, channels=58, channel_mask=True, controls=controls)
    from common_exp import save_pickle
    save_pickle(dict(evidence={"causal": ev}, info=info), fold_file(exp, held))
    log(f"  fold held-out={held} done in {info['total_s']:.0f}s: controls "
        + "  ".join(f"{k} {v['pooled']:.3f}" for k, v in controls.items()))
    del net
    torch.cuda.empty_cache()


def install():
    DC.DirectNet.__init__ = DirectNet_init
    DC.DirectNet.forward = DirectNet_forward
    DC.windows = windows_masked
    DC.logits = logits_masked
    DC.fit = fit_masked


def configure():
    DC.W = int(10 * FS)
    DC.POS0, DC.LAB = DC.W - DC.FUT - DC.HOP, DC.W - FS
    D["targets"] = ["env", "onset", "dom"]
    D["augment"] = {**CFG["dnn_augment"], **CFG["aug_variants"]["cluster"], "clf_p_switch2": 0.5}
    D["train_seed"] = int(CFG["seed"])
    D["model_blocks"] = 3
    D["max_epochs"] = MAX_EPOCHS
    decoders.apply_overrides(["model_blocks=3"], ["clf_p_switch2=0.5"])


def namespace():
    from types import SimpleNamespace
    return SimpleNamespace(
        backbone="vlaai_ws", p_switch=0.5, zero_eeg=False, min_epochs=MAX_EPOCHS,
        init_from=INIT, prep="eaP_n5_t60", tag=TAG,
    )


def self_check():
    torch.manual_seed(0)
    net = DC.DirectNet(58, 3, "vlaai_ws").to(DEV)
    e = torch.randn(4, DC.W, 58, device=DEV)
    # simulate one absent channel
    e[:, :, 10] = 0.0
    s = torch.randn(4, DC.W, 2, 3, device=DEV)
    mask = present_mask(e[0].cpu().numpy())
    DC.windows.last_mask = [mask] * 4
    net.eval()
    with torch.no_grad():
        a = net(e, s)
        b = net(e, s.flip(2))
        # zero-init FiLM: identical to multiplying by mask only
        e2 = e * torch.as_tensor(mask, device=DEV).view(1, 1, -1)
        DC.windows.last_mask = [np.ones(58, np.float32)] * 4
        # force identity path via infer mask of all-ones on already-masked e
        net._infer_mask = torch.ones(58, device=DEV)
        c = net(e2, s)
        net._infer_mask = None
    if not torch.allclose(a, -b, atol=1e-4):
        raise RuntimeError(f"antisymmetry failed max {(a + b).abs().max().item()}")
    if not torch.allclose(a, c, atol=1e-4):
        raise RuntimeError(f"zero-init FiLM not identity max {(a - c).abs().max().item()}")
    del net
    if DEV.type == "cuda":
        torch.cuda.empty_cache()
    print("channel-mask self-check passed", flush=True)


def mark_claim(status, note=None):
    path = os.path.join(EXP, "multi_agent_opt", "CLAIMS.md")
    rows = open(path).read().splitlines()
    out = []
    for line in rows:
        if "| channel-mask |" in line and "STEP A: YES zero-fill" in line:
            parts = line.split("|")
            # | When | Agent | Method | Status | Note |
            if len(parts) >= 6:
                parts[4] = f" {status} "
                if note:
                    parts[5] = f" {note} "
                line = "|".join(parts)
        out.append(line)
    open(path, "w").write("\n".join(out) + ("\n" if rows and not rows[-1].endswith("\n") else ""))


def score_preset(log, a):
    la = (DC.FUT + DC.HOP) / FS
    ev, _ = S.load_folds(EXP_NAME, "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}  SDT {r['SDT']:.2f}")
    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}")
    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} scale {SCALE}")
    E.CONF.update(prep=a.prep, targets=list(D["targets"]))
    os.makedirs(EXT_DIR, exist_ok=True)
    os.makedirs(os.path.dirname(ALL5_PT), exist_ok=True)
    ev_paths = {ds: os.path.join(EXT_DIR, f"ext_clf_{ds}{TAG}_evidence.pkl") for ds in ("AASD", "Carta2026")}
    if any(not os.path.exists(p) for p in ev_paths.values()):
        if os.path.exists(ALL5_PT):
            net = DC.DirectNet(58, 3, "vlaai_ws").to(DEV)
            net.load_state_dict(torch.load(ALL5_PT, map_location=DEV, weights_only=False), strict=False)
            net.eval()
            log(f"  loaded {ALL5_PT}")
        else:
            rng = np.random.default_rng(CFG["seed"] + 99)
            train, val = [], []
            for ds in DS_KEYS:
                rows = rows_of(ds)
                vs = rng.choice(sorted(rows.subject.unique()))
                for _, r in rows.iterrows():
                    (val if r["subject"] == vs else train).append(load_trial(a.prep, r["key"]))
            train += load_external(["Fuglsang2020"], a.prep, log)
            for tr in train + val:
                tr["_S"] = _targets(tr)
            attach_masks(train)
            attach_masks(val)
            log(f"  all-5 train {len(train)} / val {len(val)}")
            net, best = fit_masked(train, val, log, a, a.init_from)
            torch.save(net.state_dict(), ALL5_PT)
            log(f"  saved {ALL5_PT} best val BCE {best:.4f}")
            del train, val
        for ds, path in ev_paths.items():
            if os.path.exists(path):
                continue
            EV = {}
            trials = E.ext_trials(ds)
            log(f"  external {ds}: {len(trials)} trials")
            for i, tr in enumerate(trials):
                lg, starts = DC.logits(net, tr["eeg"], _targets(tr))
                truth = tr["att"].astype(int)[np.maximum(starts - DC.HOP, 0)]
                EV[tr["key"]] = dict(
                    r0=lg / 2, r1=-lg / 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) % 40 == 0 or i + 1 == len(trials):
                    log(f"    {ds} {i + 1}/{len(trials)}")
            with open(path, "wb") as fh:
                pickle.dump(EV, fh)
            log(f"  wrote {path}")
        del net
        if DEV.type == "cuda":
            torch.cuda.empty_cache()
    ext = {}
    for ds, path in ev_paths.items():
        with open(path, "rb") as fh:
            EV = pickle.load(fh)
        r = E.score(EV, mu, sd * SCALE, P_STAY, 0.75)
        plain = E.plain(EV)
        ext[ds] = dict(**r, acc1=plain["acc1"])
        log(f"  PRESET EXT {ds}  H {r['H']:.4f}  Acc {r['Acc']:.2f}  L {r['L']:.3f}  "
            f"SDT {r['SDT']:.2f}  acc1 {plain['acc1']:.3f}")
    return per, ext, mu, sd, mH, mA


def write_results(per, ext, mH, mA):
    path = os.path.join(EXP, "multi_agent_opt", "results_channel_mask.md")
    lines = [
        "# results_channel_mask",
        "",
        "STEP A: **YES** zero-fill into `b64ustc58` (missing names → 0; UHD lacks FCZ).",
        "STEP A2: present-channel mask + planar XY FiLM on vlaai_ws 10 s (`eaP_n5_t60`, sw2), "
        f"{MAX_EPOCHS} epochs × 250 steps/fold, HMM p_stay {P_STAY} scale {SCALE}.",
        "",
        "## LODO (preset)",
        "",
        "| Held-out | H | Acc % | vs stem_ms96 H/Acc |",
        "|---|---:|---:|---|",
    ]
    stem = dict(AVGC=(0.7945, 74.06), DTU=(0.9061, 89.85), KUL=(0.8755, 82.33),
                UHD=(0.8407, 79.47), USTC=(0.9219, 86.36))
    for ds in DS_KEYS:
        r = per[ds]
        sh, sa = stem[ds]
        lines.append(f"| {ds} | {r['H']:.4f} | {r['Acc']:.2f} | stem {sh:.4f}/{sa:.2f} |")
    lines += [
        f"| **mean** | **{mH:.4f}** | **{mA:.2f}** | stem 0.8677/82.42 |",
        "",
        "## External (preset)",
        "",
        "| Set | H | Acc % | vs linear_hp30 | vs stem_ms96 |",
        "|---|---:|---:|---|---|",
        f"| AASD | {ext['AASD']['H']:.4f} | {ext['AASD']['Acc']:.2f} | "
        f"0.6422/53.12 | 0.6158/51.54 |",
        f"| Carta2026 | {ext['Carta2026']['H']:.4f} | {ext['Carta2026']['Acc']:.2f} | "
        f"0.6520/60.54 | 0.5695/51.03 |",
        "",
        f"Checkpoints/folds under experiment `{EXP_NAME}`. JSON `{OUT_JSON}`.",
        "",
    ]
    open(path, "w").write("\n".join(lines))
    return path


def main():
    install()
    configure()
    self_check()
    log = log_to(LOG_PATH)
    a = namespace()
    mark_claim("running", "GPU train under flock; results_channel_mask.md")
    log(f"=== {EXP_NAME} channel-mask FiLM device {DEV} max_epochs {MAX_EPOCHS} ===")
    for held in DS_KEYS:
        path = fold_file(EXP_NAME, held)
        if os.path.exists(path):
            log(f"  fold held-out={held}: cached {path}")
            continue
        run_fold_masked(held, EXP_NAME, log, a)
    per, ext, mu, sd, mH, mA = score_preset(log, a)
    out = dict(
        experiment=EXP_NAME, decoder="direct_clf", prep="eaP_n5_t60",
        targets=D["targets"], p_switch=0.5, init_from=INIT, min_epochs=MAX_EPOCHS,
        max_epochs=MAX_EPOCHS, backbone="vlaai_ws", window_s=10.0,
        train_seed=D["train_seed"], extra_train="Fuglsang2020", augment=D["augment"],
        channel_mask=True, electrode_xy=True,
        preset=dict(p_stay=P_STAY, scale=SCALE, emis_mu=mu, emis_sd=sd,
                    lodo={d: per[d] for d in DS_KEYS}, external=ext),
        mean_H=mH, mean_Acc=mA,
    )
    os.makedirs(os.path.dirname(OUT_JSON), exist_ok=True)
    with open(OUT_JSON, "w") as fh:
        json.dump(out, fh, indent=1, default=float)
    rpath = write_results(per, ext, mH, mA)
    note = (f"LODO mean H {mH:.4f} Acc {mA:.2f}. AASD {ext['AASD']['H']:.4f}/{ext['AASD']['Acc']:.2f}. "
            f"Carta {ext['Carta2026']['H']:.4f}/{ext['Carta2026']['Acc']:.2f}. `{rpath}`.")
    mark_claim("done", note)
    log(f"wrote {OUT_JSON} and {rpath}")
    log("=== done ===")


if __name__ == "__main__":
    main()
