"""C1: evaluate the best configuration on AASD with REAL spontaneous attention switches.

Train vlaai_ws (prep mat_reac, aug 'cluster', targets env+onset, contrastive loss) on ALL five corpus datasets
(one validation subject per dataset for early stopping), run it causally on the converted AASD trials
(external_data/AASD/cache/mat_reac, same compliant pipeline), and score with the official scorer port on the
real key-press labels. HMM emissions (mu, sd) come from the corpus LODO evidence of the same configuration (no
AASD labels). p_stay: 0.998 (as on the corpus) plus a sensitivity sweep, since AASD listeners switch every ~10 s.

usage: python eval_external.py --seed 0   -> results/research/aasd_c1_s{seed}.json (+ evidence .pkl)
"""
import argparse
import json
import os
import pickle
import time

import numpy as np
import pandas as pd

import decoders
import score as S
from common_exp import CFG, EXP, GLIMPSE_THR, evidence_from_features, load_trial, rows_of
from common import DS_KEYS, FS as FS128, SEED
from e1_switch import expand, integ_hmm
from scorer import aggregate, fast_score_subject

BEST = "vlaai_ws__mat_reac_aug_cluster_onset"
CONF = dict(decoder="vlaai_ws", prep="mat_reac", targets=["env", "onset"], ref=BEST)   # overridden by CLI
EXT = os.path.join(EXP, "..", "external_data")


def log(m):
    print(f"{time.strftime('%F %T')} {m}", flush=True)


def ext_trials(ds, subjects=None):
    """Converted trials of an external evaluation set (+ its dominance target when GLIMPSE_THR's cache exists)."""
    c = os.path.join(EXT, ds, "cache")
    I = pd.read_csv(os.path.join(c, "index.csv"))
    if subjects is not None:
        I = I[I.subject.isin(subjects)]
    out = []
    for _, r in I.iterrows():
        z = np.load(os.path.join(c, CONF["prep"], r["key"] + ".npz"))
        tr = dict(eeg=z["eeg"], env=z["env"], att=z["att"], t0=float(z["t0"]), key=r["key"], att128=z["att128"],
                  n128=int(z["n128"]), subject=int(r["subject"]))
        g = os.path.join(c, f"glimpse{GLIMPSE_THR}", r["key"] + ".npz")
        if os.path.exists(g):
            zg = np.load(g)
            tr.update({k: zg[k] for k in zg.files if k != "m"})
        out.append(tr)
    return out


def train(seed, extra=(), extra_train=(), init_from=None):
    decoders.D.update(train_seed=seed, targets=CONF["targets"],
                      augment={**CFG["dnn_augment"], **CFG["aug_variants"]["cluster"]})
    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():
            if CONF["decoder"] in decoders.DNN and r["subject"] == vs:
                val.append(load_trial(CONF["prep"], r["key"]))
            else:
                train_.append(load_trial(CONF["prep"], r["key"]))
    train_ += list(extra)
    if extra_train:
        from run_experiment import load_external
        train_ += load_external(list(extra_train), CONF["prep"], log)
    log(f"train on all 5 datasets (+{len(extra)} eval-set trials, extra {list(extra_train)}): {len(train_)} train / {len(val)} val trials")
    dec = decoders.make(CONF["decoder"], 58, log)
    if init_from:
        from run_experiment import init_net
        init_net(dec, init_from, log)
    dec.fit(train_, val, [(0, 0)] * len(train_))
    return dec


def evidence(dec, ds, subjects=None):
    ev = {}
    for tr in ext_trials(ds, subjects):
        A, B0, B1 = dec.features(tr, "causal") if CONF["decoder"] in decoders.DNN else dec.features(tr)
        e = evidence_from_features(A, B0, B1, tr["att"], tr["t0"])
        e.update(att128=tr["att128"], n128=tr["n128"], subject=tr["subject"])
        ev[tr["key"]] = e
    return ev


def emissions(weights="equal"):
    """HMM emissions (and LDA target weights) from the corpus LODO evidence of the reference experiment: no label of
    the external set is used."""
    ev, _ = S.load_folds(CONF["ref"], "causal")
    sg = lambda e: np.where(e["att"] == 0, 1.0, -1.0)
    w = None
    if weights == "lda":
        X = np.concatenate([(e["r0f"].astype(float) - e["r1f"].astype(float)) * sg(e)[:, None] for ds in DS_KEYS for e in ev[ds].values()])
        w = np.linalg.solve(np.cov(X.T), X.mean(0))
        w = w / np.abs(w).sum()
        A = X @ w
    else:
        A = np.concatenate([(e["r0"] - e["r1"]) * sg(e) for ds in DS_KEYS for e in ev[ds].values()])
    return float(A.mean()), float(A.std()), w


def score(ev, mu, sd, p_stay, lookahead=0.75):
    rng = np.random.default_rng(SEED)
    per = {}
    for k in sorted(ev):
        e = ev[k]
        d = (e["r0"] - e["r1"]).astype(float)
        dec = integ_hmm(d, dict(p_stay=p_stay, mu=mu, sd=sd))
        labs = expand(dec, e["t_end"].astype(float), lookahead, e["n128"], int(rng.integers(0, 2)))
        per.setdefault(e["subject"], []).append((labs, e["att128"].astype(float)))
    res = []
    for s, lst in per.items():
        pred = np.concatenate([a for a, _ in lst]); gt = np.concatenate([b for _, b in lst])
        trial = np.concatenate([np.full(len(a), i) for i, (a, _) in enumerate(lst)])
        res.append(fast_score_subject(pred, gt, trial, fs=FS128))
    a = aggregate(res)
    return dict(Acc=a["Acc"], SDT=a["SDT"], L=a["L"], H=a["H"], nSwitches=a["nSwitches"], nUndetected=a["nUndetected"])


def plain(ev):
    per = {}
    for e in ev.values():
        d = (e["r0"] - e["r1"])[::4]; sg = np.where(e["att"][::4] == 0, 1.0, -1.0)
        per.setdefault(e["subject"], []).append(d * sg)
    acc = [np.mean(np.concatenate(v) > 0) for v in per.values()]
    snr = [np.mean(np.concatenate(v)) / np.std(np.concatenate(v)) for v in per.values()]
    return dict(acc1=float(np.median(acc)), snr1=float(np.median(snr)))


if __name__ == "__main__":
    ap = argparse.ArgumentParser()
    ap.add_argument("--seed", type=int, default=0)
    ap.add_argument("--aasd-folds", type=int, default=0, help="K>0: also train on the eval set, subject-wise K-fold (#1)")
    ap.add_argument("--dataset", default="AASD", help="external evaluation set(s), comma list: AASD,Carta2026 (one training)")
    ap.add_argument("--baseline", action="store_true", help="organisers' baseline: linear backward WF on prep 'mat' (no EA, envelope only)")
    ap.add_argument("--targets", default="env,onset")
    ap.add_argument("--extra-train", default="", help="comma list of external TRAINING sets (e.g. Fuglsang2020)")
    ap.add_argument("--init-from", default=None)
    ap.add_argument("--evidence-weights", default="equal", choices=["equal", "lda"])
    ap.add_argument("--ref", default=None, help="corpus LODO experiment of the same configuration (emissions, LDA weights)")
    ap.add_argument("--tag", default="", help="suffix of the output files (a new configuration needs a new tag)")
    ap.add_argument("--prep", default="mat_reac", help="prep of the training data and of the external sets (e.g. eaP_n5_t60: no carry)")
    a = ap.parse_args()
    CONF.update(targets=a.targets.split(","), ref=a.ref or BEST, prep=a.prep)
    if a.baseline:
        CONF.update(decoder="wf", prep="mat", targets=["env"], ref="wf__mat")
    out = os.path.join(EXP, "results", "research")
    os.makedirs(out, exist_ok=True)
    LA = decoders.LINEAR[CONF["decoder"]].lookahead_s if CONF["decoder"] in decoders.LINEAR else CFG["dnn"]["evidence_lookahead_s"]
    sets = a.dataset.split(",")
    name = lambda ds: ("aasd_c1" if ds == "AASD" else f"ext_c1_{ds}") + (f"_ft{a.aasd_folds}" if a.aasd_folds else "") + \
        ("_baselineWF" if a.baseline else "") + a.tag
    evp = {ds: os.path.join(out, f"{name(ds)}_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])}
    xt = [x for x in a.extra_train.split(",") if x]
    if a.aasd_folds:
        for ds in [d for d in sets if d not in EV]:
            subs = sorted(pd.read_csv(os.path.join(EXT, ds, "cache", "index.csv")).subject.unique())
            EV[ds] = {}
            for k, test in enumerate(np.array_split(np.random.default_rng(0).permutation(subs), a.aasd_folds)):
                log(f"{ds} fold {k + 1}/{a.aasd_folds}: test subjects {sorted(test.tolist())}")
                dec = train(a.seed, ext_trials(ds, [s_ for s_ in subs if s_ not in test]), xt, a.init_from)
                EV[ds].update(evidence(dec, ds, list(test)))
            pickle.dump(EV[ds], open(evp[ds], "wb"))
    elif any(ds not in EV for ds in sets):
        dec = train(a.seed, (), xt, a.init_from)                    # one training, every missing evaluation set
        for ds in [d for d in sets if d not in EV]:
            EV[ds] = evidence(dec, ds)
            pickle.dump(EV[ds], open(evp[ds], "wb"))
    from aasd_synthetic import score_syn
    for wmode in (["equal", "lda"] if a.evidence_weights == "lda" else ["equal"]):
        mu, sd, w = emissions(wmode)
        for ds in sets:
            ev = EV[ds] if w is None else {k: {**e, "r0": e["r0f"].astype(float) @ w, "r1": e["r1f"].astype(float) @ w} for k, e in EV[ds].items()}
            res = dict(seed=a.seed, dataset=ds, config=f"{CONF['decoder']} / {CONF['prep']} / {CONF['targets']} / extra {xt} / init {a.init_from}",
                       evidence_weights=wmode, target_weights=None if w is None else [float(x) for x in w],
                       emissions=dict(mu=mu, sd=sd), plain=plain(ev), by_p_stay={})
            for p in (0.998, 0.995, 0.99, 0.98, 0.97, 0.95, 0.9):
                res["by_p_stay"][str(p)] = score(ev, mu, sd, p, LA)
            res["synthetic_p0.998"] = score_syn(ev, mu, sd, 0.998, np.random.default_rng(SEED), LA)
            b = res["by_p_stay"]
            log(f"{ds} [{wmode}] plain {res['plain']} | REAL p0.998 Acc {b['0.998']['Acc']:.2f} H {b['0.998']['H']:.3f} | "
                f"p0.99 Acc {b['0.99']['Acc']:.2f} H {b['0.99']['H']:.3f} | p0.95 H {b['0.95']['H']:.3f} | "
                f"SYNTHETIC p0.998 {res['synthetic_p0.998']}")
            json.dump(res, open(os.path.join(out, f"{name(ds)}{'_wlda' if wmode == 'lda' else ''}_s{a.seed}.json"), "w"), indent=1, default=float)
