"""Direct classifier on REAL attention switches (AASD, Carta2026): counterpart of eval_external.py.

Train the direct classifier (direct_classifier.py: prep mat_reac, stimulus features env,onset,dom, synthetic switches in
30% of the training windows) on ALL five corpus datasets + Fuglsang2020 (one validation subject per dataset, the split
of eval_external.py), run it causally on the converted external trials and keep the evidence (no synthetic switch: the
labels are the real key presses). Alone it is scored with HMM emissions from the corpus LODO folds of --ref.
usage: GLIMPSE_THR=norm python eval_external_clf.py --seed 0 [--window-s 10] --init-from models/pretrain_spk_direct_clf_v2_s0.pt
           --ref direct_clf__mat_reac_ptSPKv2 --tag _v2   -> results/research/ext_clf_<DS><tag>_evidence_s<seed>.pkl (+ .json)"""
import argparse
import json
import os
import pickle
from types import SimpleNamespace

import numpy as np
import torch

import direct_classifier as DC
import eval_external as E
import score as S
from common_exp import CFG, EXP, FS, load_trial, rows_of
from common import DS_KEYS
from decoders import D, _targets
from run_experiment import load_external

def cv_split(keys, k, i):
    """C7: held-out subjects and held-out stimuli of fold i. Key = <set>_S<subject>_T<trial>; in Carta2026 the same T is the same audio with the SAME
    switch schedule for every subject, so a split by subject alone would let the network memorise stimulus -> label. Training keeps neither the
    subjects nor the T of the fold; the fold is evaluated on (held subjects) x (held T)."""
    subj, tok = sorted({x.split("_")[1] for x in keys}), sorted({x.split("_")[2] for x in keys})
    return set(subj[i::k]), set(tok[i::k])


if __name__ == "__main__":
    ap = argparse.ArgumentParser()
    ap.add_argument("--seed", type=int, default=0)
    ap.add_argument("--window-s", type=float, default=None)
    ap.add_argument("--init-from", default=None)
    ap.add_argument("--min-epochs", type=int, default=10)
    ap.add_argument("--ref", required=True, help="corpus LODO classifier experiment of the same configuration (emissions)")
    ap.add_argument("--dataset", default="AASD,Carta2026")
    ap.add_argument("--backbone", default="vlaai_ws")
    ap.add_argument("--tag", default="")
    ap.add_argument("--prep", default="mat_reac")
    ap.add_argument("--set", nargs="*", default=[], help="overrides of config dnn, e.g. model_blocks=3")
    ap.add_argument("--aug-set", nargs="*", default=[], help="overrides of the augmentation, e.g. clf_p_switch2=0.5")
    ap.add_argument("--p-switch", type=float, default=0.3)
    ap.add_argument("--train-ext", default="", help="comma list of external sets with REAL switches added to the TRAINING data (AASD, Carta2026; one "
                    "subject of each goes to the validation set); must not contain a set of --dataset (A1 of REAL_SWITCH_STRATEGY.md)")
    ap.add_argument("--cv-k", type=int, default=0, help="C7: k > 0 trains also on the evaluated set, without the subjects and the stimuli of fold --cv-fold")
    ap.add_argument("--cv-fold", type=int, default=0)
    a = ap.parse_args()
    if a.window_s:
        DC.W = int(a.window_s * FS)
        DC.POS0, DC.LAB = DC.W - DC.FUT - DC.HOP, DC.W - FS
    D.update(targets=["env", "onset", "dom"], train_seed=a.seed, augment={**CFG["dnn_augment"], **CFG["aug_variants"]["cluster"]})
    import decoders
    decoders.apply_overrides(a.set, a.aug_set)
    D["fm_held"] = "ALL"                                                     # H1: the front pretrained on all five datasets
    E.CONF.update(prep=a.prep, targets=D["targets"])
    torch.set_num_threads(4)
    out = os.path.join(EXP, "results", "research")
    sets = a.dataset.split(",")
    evp = {ds: os.path.join(out, f"ext_clf_{ds}{a.tag}_evidence_s{a.seed}.pkl") for ds in sets}
    EV = {ds: pickle.load(open(evp[ds], "rb")) for ds in sets if os.path.exists(evp[ds])}
    if any(ds not in EV for ds in sets):
        rng = np.random.default_rng(CFG["seed"] + 99)                       # the split of eval_external.train
        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"]))
        if not D.get("no_fuglsang"):                                         # --set no_fuglsang=1: the organisers' five datasets only
            train += load_external(["Fuglsang2020"], a.prep, E.log)
        for nm in [x for x in a.train_ext.split(",") if x]:
            assert a.cv_k or nm not in sets, f"{nm} is both a training and an evaluation set"
            ext = load_external([nm], a.prep, E.log)
            if a.cv_k and nm in sets:
                hs, ht = cv_split([t["key"] for t in ext], a.cv_k, a.cv_fold)
                ext = [t for t in ext if t["key"].split("_")[1] not in hs and t["key"].split("_")[2] not in ht]
            vs = rng.choice(sorted({t["key"].split("_")[1] for t in ext}))            # one validation subject (early stopping)
            for t in ext:
                t["_real"] = True                                                  # real switches (A2 sampling)
                (val if t["key"].split("_")[1] == vs else train).append(t)
        for tr in train + val:
            tr["_S"] = _targets(tr)
            DC.fm_attach(tr)
        E.log(f"direct classifier, window {DC.W / FS:g} s, seed {a.seed}: {len(train)} train / {len(val)} val trials")
        net, best = DC.fit(train, val, E.log, SimpleNamespace(backbone=a.backbone, p_switch=a.p_switch, zero_eeg=False, min_epochs=a.min_epochs), a.init_from)
        torch.save(net.state_dict(), os.path.join(EXP, "models", f"all5_direct_clf{a.tag}_s{a.seed}.pt"))
        del train, val
        for ds in [d for d in sets if d not in EV]:
            EV[ds] = {}
            trials = E.ext_trials(ds)
            if a.cv_k:
                hs, ht = cv_split([t["key"] for t in trials], a.cv_k, a.cv_fold)
                trials = [t for t in trials if t["key"].split("_")[1] in hs and t["key"].split("_")[2] in ht]
            for tr in trials:
                DC.fm_attach(tr, net)
                lg, starts = DC.logits(net, DC.wide(tr), _targets(tr))
                truth = tr["att"].astype(int)[np.maximum(starts - DC.HOP, 0)]      # attended column 1 s before the window end
                EV[ds][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 DC.logits.last_sw is not None:                                   # A4: P(switch in the last 5 s) per hop
                    EV[ds][tr["key"]]["sw"] = DC.logits.last_sw
            pickle.dump(EV[ds], open(evp[ds], "wb"))
    import time
    from common_exp import fold_file
    for _ in range(720):                                  # the corpus LODO run of --ref may still be training (job queue)
        if all(os.path.exists(fold_file(a.ref, ds)) for ds in DS_KEYS):
            break
        time.sleep(30)
    ev, _ = S.load_folds(a.ref, "causal")
    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())
    for ds in sets:
        res = dict(seed=a.seed, dataset=ds, window_s=DC.W / FS, init_from=a.init_from, emissions=dict(mu=mu, sd=sd), plain=E.plain(EV[ds]),
                   by_p_stay={str(p): E.score(EV[ds], mu, sd, p, 0.75) for p in (0.9997, 0.998, 0.995, 0.99, 0.98, 0.95, 0.9)})
        b = res["by_p_stay"]
        E.log(f"{ds} classifier {DC.W / FS:g} s: plain {res['plain']} | REAL " + "  ".join(f"p{p} H {v['H']:.3f} Acc {v['Acc']:.1f}" for p, v in b.items()))
        if all("sw" in e for e in EV[ds].values()):                          # A4: HMM with the stay probability gated by the transition detector
            from hmm_gated import score_gated
            res["gated"] = {f"{ph}/{pl}": score_gated(EV[ds], mu, sd, ph, pl) for ph, pl in ((0.998, 0.95), (0.998, 0.9), (0.998, 0.8), (0.99, 0.9), (0.99, 0.7))}
            E.log(f"{ds} GATED (p_hi/p_lo) " + "  ".join(f"{k} H {v['H']:.3f} Acc {v['Acc']:.1f} SDT {v['SDT']:.1f}" for k, v in res["gated"].items()))
        json.dump(res, open(os.path.join(out, f"ext_clf_{ds}{a.tag}_s{a.seed}.json"), "w"), indent=1, default=float)
