"""Cross-dataset MixStyle on the published direct classifier.

The logit transformer stem_ms96 already reaches mean LODO H 0.8677 / Acc 82.42, but its
AASD frame accuracy is about 0.52. Another head on those four logits cannot reach Acc 60.
This run keeps that classifier recipe and adds MixStyle (Zhou et al.) on the vlaai_ws
hidden states so channel gain and variance are mixed across training datasets.

Recipe, unchanged: prep eaP_n5_t60, 10 s window, 3 blocks, 64 kernels, sw2 augmentation,
SparrKULee init, seed 20270101, Fuglsang as extra train, BCE, one validation subject per
dataset. MixStyle is training-only. One (partner, lambda) pair is shared by both stream
orders, so the antisymmetric logit is preserved. It is off for validation and for test.

Headline external HMM is the pre-specified safe point p_stay 0.998 (scale 1). p_stay 0.994
is reported as well and is not chosen from AASD or Carta labels.
"""
import json
import os

import numpy as np
import torch

import decoders
import direct_classifier as DC
import eval_external as E
import score as S
from common import DS_KEYS
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_mixstyle"
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", "technique_mixstyle.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_mixstyle_s0.pt")
MIX_P = 0.5
MIX_ALPHA = 0.1

_ORIG_BB = decoders.VLAAIwsPatched.forward
_ORIG_NET = DC.DirectNet.forward
_ORIG_WINDOWS = DC.windows


def domain_name(trial):
    grp = trial.get("_grp")
    if grp:
        return str(grp)
    return str(trial["key"]).split("-subject-")[0]


def windows_tagged(trials, n, rng, aug, p_sw, zero_eeg):
    # The original function stores the trial index on the name `windows`, which is this
    # wrapper once it is installed. Read that attribute after the call, not the saved original.
    out = _ORIG_WINDOWS(trials, n, rng, aug, p_sw, zero_eeg)
    ti = windows_tagged.last_ti
    windows_tagged.last_dom = [domain_name(trials[int(i)]) for i in ti]
    return out


def cross_perm(dom):
    """A partner index for every row, preferring a different dataset."""
    b = dom.shape[0]
    perm = torch.randperm(b, device=dom.device)
    same = dom[perm] == dom
    if bool(same.any()):
        alt = torch.arange(b, device=dom.device).roll(1)
        alt_ok = dom[alt] != dom
        perm = torch.where(same & alt_ok, alt, perm)
    return perm


def apply_mix(h, perm, lam):
    """h is (B, C, T). Statistics are over time, inside the classifier window."""
    mu = h.mean(dim=-1, keepdim=True)
    sig = h.var(dim=-1, unbiased=False, keepdim=True).clamp_min(1e-6).sqrt()
    normed = (h - mu) / sig
    mu_m = lam * mu + (1.0 - lam) * mu[perm]
    sig_m = lam * sig + (1.0 - lam) * sig[perm]
    return normed * sig_m + mu_m


def bb_forward(self, x):
    x = self.proj(x.transpose(1, 2))
    h = x
    pack = getattr(self, "_mix_pack", None)
    for i in range(self.nb_blocks):
        if self.use_skip and i > 0:
            h = h + x
        h = self.block(h)
        if pack is not None:
            h = apply_mix(h, pack[0], pack[1])
    return h.transpose(1, 2)


def net_forward(self, E, S, feat=False):
    self.bb._mix_pack = None
    if self.training:
        names = getattr(DC.windows, "last_dom", None)
        if names is not None and len(names) == E.shape[0] and float(torch.rand(())) < MIX_P:
            dom = torch.tensor(
                [sorted(set(names)).index(n) for n in names], device=E.device, dtype=torch.long)
            perm = cross_perm(dom)
            lam = torch.distributions.Beta(MIX_ALPHA, MIX_ALPHA).sample((E.shape[0], 1, 1))
            lam = lam.to(device=E.device, dtype=E.dtype)
            self.bb._mix_pack = (perm, lam)
    try:
        return _ORIG_NET(self, E, S, feat=feat)
    finally:
        self.bb._mix_pack = None


def install():
    decoders.VLAAIwsPatched.forward = bb_forward
    DC.DirectNet.forward = net_forward
    DC.windows = windows_tagged


def self_check():
    """Eval path stays MixStyle-free. A training forward stays finite and antisymmetric in the streams."""
    torch.manual_seed(0)
    net = DC.DirectNet(58, 3, "vlaai_ws").to(DEV)
    e = torch.randn(4, DC.W, 58, device=DEV)
    s = torch.randn(4, DC.W, 2, 3, device=DEV)
    DC.windows.last_dom = ["dataset-001", "dataset-003", "dataset-004", "dataset-005"]
    net.eval()
    with torch.no_grad():
        a = net(e, s)
        b = net(e, s.flip(2))
    if not torch.allclose(a, -b, atol=1e-4):
        raise RuntimeError(f"eval antisymmetry failed, max {float((a + b).abs().max())}")
    net.train()
    y = net(e, s)
    if not torch.isfinite(y).all():
        raise RuntimeError("training MixStyle forward is non-finite")
    del net
    if DEV.type == "cuda":
        torch.cuda.empty_cache()
    print("mixstyle self-check passed", flush=True)


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
    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=10,
        init_from=INIT, prep="eaP_n5_t60", tag="_w10b3_ptSPK_mixstyle",
    )


def run_lodo(log):
    a = namespace()
    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
        DC.run_fold(held, EXP_NAME, log, a)
    scored = S.score_experiment(EXP_NAME, "causal", (DC.FUT + DC.HOP) / FS)
    out = dict(
        experiment=EXP_NAME, decoder="direct_clf", prep="eaP_n5_t60",
        targets=D["targets"], p_switch=0.5, init_from=INIT, min_epochs=10,
        backbone="vlaai_ws", window_s=10.0, train_seed=D["train_seed"],
        extra_train="Fuglsang2020", augment=D["augment"], mixstyle=dict(p=MIX_P, alpha=MIX_ALPHA),
        causal=scored,
    )
    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)
    m = scored["mean"]
    log(f"  MEAN LODO Acc {m['Acc']:.2f}  SDT {m['SDT']:.2f}  L {m['L']:.3f}  H {m['H']:.4f}  acc1 {m['acc1']:.3f}")
    for ds, row in scored["per_dataset"].items():
        log(f"  LODO {ds}  H {row['H']:.4f}  Acc {row['Acc']:.2f}  L {row['L']:.3f}  SDT {row['SDT']:.2f}")
    log(f"wrote {OUT_JSON}")
    return scored


def run_external(log):
    os.makedirs(EXT_DIR, exist_ok=True)
    os.makedirs(os.path.dirname(ALL5_PT), exist_ok=True)
    a = namespace()
    ev_paths = {ds: os.path.join(EXT_DIR, f"ext_clf_{ds}_mixstyle_s0.pkl") for ds in ("AASD", "Carta2026")}
    missing = [ds for ds, p in ev_paths.items() if not os.path.exists(p)]
    if missing:
        if os.path.exists(ALL5_PT):
            fs = 3
            net = DC.DirectNet(58, fs, "vlaai_ws").to(DEV)
            net.load_state_dict(torch.load(ALL5_PT, map_location=DEV, weights_only=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("eaP_n5_t60", r["key"]))
            train += load_external(["Fuglsang2020"], "eaP_n5_t60", log)
            for tr in train + val:
                tr["_S"] = _targets(tr)
            log(f"all-5 MixStyle classifier: {len(train)} train / {len(val)} val")
            net, best = DC.fit(train, val, log, a, INIT)
            torch.save(net.state_dict(), ALL5_PT)
            log(f"saved {ALL5_PT} best val BCE {best:.4f}")
            del train, val
        for ds in missing:
            ev = {}
            for tr in E.ext_trials(ds):
                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"])
            with open(ev_paths[ds], "wb") as fh:
                import pickle
                pickle.dump(ev, fh)
            log(f"wrote {ev_paths[ds]} ({len(ev)} trials)")
        del net
        if DEV.type == "cuda":
            torch.cuda.empty_cache()
    import pickle
    evs = {}
    for ds, path in ev_paths.items():
        with open(path, "rb") as fh:
            evs[ds] = pickle.load(fh)
    folds, _ = S.load_folds(EXP_NAME, "causal")
    parts = [(e["r0"] - e["r1"]) * np.where(e["att"] == 0, 1.0, -1.0)
             for ds in DS_KEYS for e in folds[ds].values()]
    mu, sd = float(np.concatenate(parts).mean()), float(np.concatenate(parts).std())
    log(f"external emissions from LODO folds only  mu {mu:.4f}  sd {sd:.4f}")
    rows = []
    for ds, ev in evs.items():
        plain = E.plain(ev)
        for p in (0.998, 0.994):
            r = E.score(ev, mu, sd, p, 0.75)
            rec = dict(model="mixstyle_clf", ds=ds, p_stay=p, scale=1.0, preset=(p == 0.998),
                       acc1=plain["acc1"], **r)
            rows.append(rec)
            log(f"TEST {ds} p {p}  H {r['H']:.4f}  Acc {r['Acc']:.2f}  SDT {r['SDT']:.2f}  "
                f"L {r['L']:.3f}  acc1 {plain['acc1']:.3f}")
    csv_path = os.path.join(EXT_DIR, "technique_mixstyle_external.csv")
    import pandas as pd
    pd.DataFrame(rows).to_csv(csv_path, index=False)
    log(f"wrote {csv_path}")
    return rows


def main():
    install()
    configure()
    self_check()
    log = log_to(LOG_PATH)
    log(f"=== {EXP_NAME} MixStyle p {MIX_P} alpha {MIX_ALPHA} device {DEV} ===")
    if os.environ.get("SMOKE"):
        log("SMOKE: one held-out fold, 1 epoch")
        D["max_epochs"] = 1
        D["patience"] = 1
        a = namespace()
        a.min_epochs = 0
        DC.run_fold("USTC", EXP_NAME + "_smoke", log, a)
        log("SMOKE done")
        return
    run_lodo(log)
    run_external(log)
    log("=== done ===")


if __name__ == "__main__":
    main()
