"""huyle STQD-v1 classifier.

Separate EEG/speech encoders, candidate-conditioned attention, antisymmetric output.
The source-compatible LODO sampler, pretraining, augmentation, evidence and
scoring interface are retained from decoder_lodo/direct_classifier.py.
"""
import argparse
import json
import os
import time

import numpy as np
import torch

import decoders
from common_exp import CFG, EXP, FS, fold_file, load_trial, rows_of, save_pickle
from common import DS_KEYS, MONTAGES, SEED
from decoders import D, DEV, _targets
from e1_switch import draw_switch_times
from run_experiment import load_external, log_to
from score import score_experiment

W, HOP, FUT = int(D["window_s"] * FS), int(D["infer_hop_s"] * FS), int(D["causal_future_s"] * FS)
POS0, LAB = W - FUT - HOP, W - FS                    # left padding at inference; label position inside a window
FRONTAL = [i for i, c in enumerate(MONTAGES[CFG["montage"]]) if c in ("FP1", "FPZ", "FP2", "AF3", "AF4", "F7", "F8")]


from stqd_model import STQDNet, VARIANTS

# DirectNet is intentionally NOT the old concatenated VLAAI model.  We retain the
# same evidence file schema, trial/window schedule, training splits and controls.
DirectNet = STQDNet


def windows(trials, n, rng, aug, p_sw, zero_eeg):
    lens = np.array([len(t["eeg"]) - W for t in trials])
    p = np.maximum(lens, 0).astype(float)
    ti = rng.choice(len(trials), n, p=p / p.sum())
    windows.last_ti = ti                                                     # trial of every window (adversarial domain loss)
    X = np.empty((n, W, 58), np.float32)
    S = np.empty((n, W) + trials[0]["_S"].shape[1:], np.float32)
    y = np.empty(n, np.float32)
    A = (D.get("augment") or {}) if aug is not None else {}                  # classifier-specific augmentations (training only)
    lag, p_fd, p_sw2 = int(A.get("clf_lag_jitter", 0)), A.get("clf_p_feat_drop", 0.0), A.get("clf_p_switch2", 0.0)
    for j, i in enumerate(ti):
        s = rng.integers(0, lens[i] + 1)
        tr = trials[i]
        s2 = int(np.clip(s + rng.integers(-lag, lag + 1), 0, lens[i])) if lag else s   # EEG-stimulus timing jitter
        X[j], S[j] = tr["eeg"][s:s + W], tr["_S"][s2:s2 + W]
        col = int(tr["att"][s + LAB])
        if rng.random() < p_sw:                                              # synthetic switch inside the window
            tau = int(rng.integers(FS // 2, W - FS // 2))
            S[j, tau:] = S[j, tau:, ::-1]
            col = 1 - col if tau <= LAB else col
            if p_sw2 and rng.random() < p_sw2 and W - FS // 2 - tau > 2 * FS:  # a second switch (back) >= 2 s later
                tau2 = int(rng.integers(tau + 2 * FS, W - FS // 2))
                S[j, tau2:] = S[j, tau2:, ::-1]
                col = 1 - col if tau2 <= LAB else col
        if p_fd and rng.random() < p_fd:                                     # drop one stimulus feature of both streams
            S[j, :, :, rng.integers(S.shape[-1])] = 0.0
        y[j] = 1.0 if col == 0 else 0.0
    if aug is not None:
        X = aug._augment(X, rng)
    if zero_eeg:
        X[:] = 0.0
    return torch.from_numpy(X), torch.from_numpy(S), torch.from_numpy(y)


@torch.no_grad()
def logits(net, x, S, step=1):
    """Causal decisions: hop start a -> window x[a - POS0 : a + HOP + FUT] (zero padded), every `step` hops."""
    T = len(x)
    xp = np.concatenate([np.zeros((POS0, 58), np.float32), x.astype(np.float32), np.zeros((FUT + HOP, 58), np.float32)])
    Sp = np.concatenate([np.zeros((POS0,) + S.shape[1:], np.float32), S, np.zeros((FUT + HOP,) + S.shape[1:], np.float32)])
    starts = np.arange(0, T, HOP * step)
    out = np.empty(len(starts), np.float32)
    for i in range(0, len(starts), 128):
        a = starts[i:i + 128]
        out[i:i + len(a)] = net(torch.from_numpy(np.stack([xp[k:k + W] for k in a])).to(DEV),
                                torch.from_numpy(np.stack([Sp[k:k + W] for k in a])).to(DEV)).cpu().numpy()
    return out, starts


def fit(train, val, log, a, init_from=None):
    """Train a DirectNet (binary cross-entropy, early stopping on the validation windows). Returns (net, best val)."""
    Fs = train[0]["_S"].shape[-1]
    seed = D.get("train_seed", CFG["seed"])
    torch.manual_seed(seed)
    net = DirectNet(variant=a.variant, channels=58, window=W, target_index=LAB).to(DEV)
    if init_from:
        net.load_state_dict(torch.load(init_from, map_location=DEV))
        log(f"  initialised from {init_from}")
    aug = decoders.DNNDec.__new__(decoders.DNNDec)                          # only for its _augment (EEG augmentation)
    aug.W, aug.fut, aug.hop, aug.C = W, FUT, HOP, 58
    rs = np.random.default_rng(seed)
    Xv, Sv, yv = windows(val, D["val_windows"], rs, None, a.p_switch, a.zero_eeg)
    adv, step, params = None, 0, list(net.parameters())
    if D.get("adv"):
        dom, n_dom = decoders.domain_ids(train, D["adv"])
        adv = decoders.AdvHead(net.head.in_features, n_dom).to(DEV)
        params += list(adv.parameters())
        log(f"  adversarial {D['adv']} loss: {n_dom} classes, lambda {D.get('adv_lambda', 0.05)}")
    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, da = time.time(), [], []
        net.train()
        for _ in range(D["steps_per_epoch"]):
            X, S, y = [z.to(DEV) for z in windows(train, D["batch_size"], rs, aug, a.p_switch, a.zero_eeg)]
            if adv is None:
                loss = bce(net(X, S), y)
                tl.append(loss.item())
            else:
                lg, f = net(X, S, feat=True)
                loss = bce(lg, y)
                tl.append(loss.item())
                dl = adv(f, decoders.adv_lambda(step))
                dt = torch.from_numpy(dom[windows.last_ti]).to(DEV)
                loss = loss + torch.nn.functional.cross_entropy(dl, dt)
                da.append((dl.argmax(1) == dt).float().mean().item())
                step += 1
            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 + 64].to(DEV), Sv[i:i + 64].to(DEV)).cpu() for i in range(0, len(Xv), 64)])
        vl, va = bce(lv, yv).item(), ((lv > 0).float() == yv).float().mean().item()
        if ep >= a.min_epochs:                     # the antisymmetric net often sits at chance for several epochs before it
            sch.step(vl)                           # takes off: no LR decay / early stopping during the first min_epochs
        log(f"    epoch {ep:02d} train {np.mean(tl):.4f} val BCE {vl:.4f} acc {va:.3f} ({time.time() - t1:.0f}s)"
            + (f" | adversary acc {np.mean(da):.3f}" if da else ""))
        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(held, exp, log, a):
    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)
    log(f"  fold held-out={held}: {len(train)} train / {len(val)} val trials, {train[0]['_S'].shape[-1]} stimulus features per stream, STQD {a.variant}")
    t0 = time.time()
    net, best = fit(train, val, log, a, a.init_from)
    t_fit = time.time() - t0
    del train, val
    # ---- held-out dataset: synthetic-switch evidence (schedule of score.py replayed) + shortcut controls
    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"])
        S, 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)                                                 # the scorer's start guess (same stream)
        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], S[:, ::-1], S)
        xe = np.zeros_like(x) if a.zero_eeg else x
        lg, starts = logits(net, xe, Ssw)
        truth = (att ^ st)[np.maximum(starts - HOP, 0)]                     # attended column 1 s before the window end
        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[:, 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 = logits(net, xc, S, step=4)
            ctl[name].setdefault(r["subj_uid"], []).append((l1 > 0) == (att[np.maximum(s1 - 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", variant=a.variant, prep=a.prep, held_out=held, fit_s=t_fit, total_s=time.time() - t0, lookahead_s=(FUT + HOP) / FS,
                best_val=best, channels=58, controls=controls)
    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 (5-s decisions, pooled accuracy) "
        + "  ".join(f"{k} {v['pooled']:.3f}" for k, v in controls.items()))
    del net
    torch.cuda.empty_cache()


if __name__ == "__main__":
    ap = argparse.ArgumentParser()
    ap.add_argument("--tag", default="")
    ap.add_argument("--variant", choices=sorted(VARIANTS), default="stqd_full", help="STQD v1 architectural ablation")
    ap.add_argument("--train-seed", type=int, default=None)
    ap.add_argument("--targets", default="env,onset,dom", help="stimulus features given for each stream")
    ap.add_argument("--p-switch", type=float, default=0.3, help="share of training windows with a synthetic switch inside")

    ap.add_argument("--window-s", type=float, default=None, help="classifier window in s (default config dnn.window_s = 5); "
                    "the read-out timing (0.75 s lookahead, label 1 s before the window end) is unchanged")
    ap.add_argument("--min-epochs", type=int, default=0, help="epochs without LR decay / early stopping (training stability)")
    ap.add_argument("--max-epochs", type=int, default=None)
    ap.add_argument("--init-from", default=None, help="DirectNet state_dict to start every fold from")
    ap.add_argument("--pretrain-spk", action="store_true", help="pretrain on SparrKULee (single talker): stream A = the "
                    "presented speech, stream B = its mismatched (half-run shifted) envelope, i.e. match-mismatch; "
                    "saves models/pretrain_spk_direct_clf_s<seed>.pt. Needs GLIMPSE_THR=onorm (dom for SparrKULee)")
    ap.add_argument("--set", nargs="*", default=[], help="hyper-parameter overrides of config dnn: key=value ...")
    ap.add_argument("--aug-set", nargs="*", default=[], help="overrides of the augmentation settings: key=value ...")
    ap.add_argument("--adv", default=None, choices=["dataset", "subject"], help="adversarial loss (gradient reversal) removing "
                    "dataset / subject information from the backbone features")
    ap.add_argument("--adv-lambda", type=float, default=0.05)
    ap.add_argument("--prep", default="mat_reac", help="mat_reac (EA state carried across a subject's trials) | mat_rea (EA restarts "
                    "in every trial: needs no subject / trial ID at test time); the SparrKULee pretraining always uses mat_reac")
    ap.add_argument("--aug-variant", default="cluster", help="EEG augmentation: dnn_augment + config aug_variants[NAME]")
    ap.add_argument("--zero-eeg", action="store_true", help="envelope-only model (EEG zeroed in training and test)")
    a = ap.parse_args()
    if a.adv:
        ap.error("--adv is not supported in STQD v1; domain-adversarial ablations require a new validated feature head")
    if a.window_s is not None and a.window_s != 10.0:
        ap.error("STQD v1 is specified for the 10 s window (320 samples)")
    a.backbone = "stqd"
    if a.window_s:
        W = int(a.window_s * FS)
        POS0, LAB = W - FUT - HOP, W - FS
    D["targets"] = a.targets.split(",")
    D["augment"] = {**CFG["dnn_augment"], **CFG["aug_variants"][a.aug_variant]}
    decoders.apply_overrides(a.set, a.aug_set)
    if a.adv:
        D.update(adv=a.adv, adv_lambda=a.adv_lambda)
    if a.train_seed is not None:
        D["train_seed"] = a.train_seed
    if a.max_epochs:
        D["max_epochs"] = a.max_epochs
    torch.set_num_threads(4)
    if a.pretrain_spk:
        seed = D.get("train_seed", 0)
        D["train_seed"] = seed
        log = log_to(os.path.join(EXP, "logs", f"pretrain_spk_direct_clf{a.tag}_s{seed}.log"))
        tr = load_external(["SparrKULee"], "mat_reac", log)
        for t in tr:
            t["_S"] = _targets(t)
        subs = sorted({t["key"].split("_")[1] for t in tr})
        vs = set(np.random.default_rng(1234).choice(subs, 4, replace=False))
        log(f"pretrain direct_clf seed {seed} on SparrKULee: {len(subs) - 4} train subjects, val {sorted(vs)}")
        net, best = fit([t for t in tr if t["key"].split("_")[1] not in vs], [t for t in tr if t["key"].split("_")[1] in vs], log, a)
        os.makedirs(os.path.join(EXP, "models"), exist_ok=True)
        out = os.path.join(EXP, "models", f"pretrain_spk_direct_clf{a.tag}_s{seed}.pt")
        torch.save(net.state_dict(), out)
        log(f"saved {out} (best val BCE {best:.4f})")
        raise SystemExit
    exp = f"direct_clf__{a.prep}{a.tag}"
    log = log_to(os.path.join(EXP, "logs", f"{exp}.log"))
    log(f"=== experiment {exp}, variant {a.variant} ===")
    for held in DS_KEYS:
        if os.path.exists(fold_file(exp, held)):
            log(f"  fold held-out={held}: cached")
            continue
        run_fold(held, exp, log, a)
    out = dict(experiment=exp, decoder="direct_clf", variant=a.variant, prep=a.prep, targets=D["targets"], zero_eeg=a.zero_eeg, p_switch=a.p_switch, init_from=a.init_from, min_epochs=a.min_epochs, backbone=a.backbone, window_s=W / FS,
               train_seed=D.get("train_seed", CFG["seed"]), extra_train="Fuglsang2020", augment=D["augment"],
               switch=f"HMM p_stay={CFG['switch']['p_stay']} per 0.25-s step, SYNTHETIC switches applied to the classifier INPUT, LODO",
               causal=score_experiment(exp, "causal", (FUT + HOP) / FS))
    out["controls"] = {d: out["causal"]["info"][d]["controls"] for d in DS_KEYS}
    m = out["causal"]["mean"]
    log(f"  MEAN over held-out datasets: Acc {m['Acc']:.2f}  SDT {m['SDT']:.2f}  L {m['L']:.3f}  H {m['H']:.3f}  (window acc {m['acc1']:.3f})")
    for k in ("normal", "zero_eeg", "shift_eeg", "no_frontal"):
        log(f"  control {k:10s}: " + "  ".join(f"{d} {out['controls'][d][k]['pooled']:.3f}" for d in DS_KEYS)
            + f"  | mean {np.mean([out['controls'][d][k]['pooled'] for d in DS_KEYS]):.3f}")
    with open(os.path.join(EXP, "results", f"{exp}.json"), "w") as fh:
        json.dump(out, fh, indent=1, default=float)
    log(f"=== done {exp} ===")
