"""Fused setup (reconstruction decoders + direct classifiers) on REAL attention switches (AASD, Carta2026).

Everything that is fitted comes from the corpus LODO folds (all five datasets; no label of the external sets): LDA
target weights and scale per decoder, scale per classifier, re-standardisation of the two ensemble means and the HMM
emissions of the fused evidence (corpus synthetic-switch evidence, aligned by fuse_evidence.features). The external
evidence comes from models trained on all five datasets (eval_external.py --tag _best, eval_external_clf.py); decoder
window j is paired with classifier hop j + DEC_OFFSET and labels are stamped by the classifier convention, as on the corpus.
usage: python fuse_external.py --dec CORPUS_EXP:SEED ... --clf CORPUS_EXP:EXT_TAG:SEED ... [--a ...] [--pstay ...] [--name label]
       appends to results/research/fuse_external.csv"""
import argparse
import os
import pickle

import numpy as np
import pandas as pd

import eval_external as E
from common_exp import EXP, BASE
from common import DS_KEYS
from fuse_evidence import LA, OFF, features

RES = os.path.join(EXP, "results", "research")

def decoder_external_file(name):
    local = os.path.join(RES, name)
    return local if os.path.exists(local) else os.path.join(BASE, "results", "research", name)

def build_external(decs, clfs, a_list, datasets, dec_tag="_best"):
    """Fused evidence of the external sets: {ds: {a: {trial: evidence dict}}} and the corpus emissions {a: (mu, sd)}.
    decs: [[corpus_exp, seed]], clfs: [[corpus_exp, ext_tag, seed]]."""
    # ---- corpus: weights, scales, emissions
    Fd = [features(d[0], clfs[0][0]) for d in decs]
    Fc = [features(decs[0][0], c[0]) for c in clfs]
    keys = [(ds, k) for ds in DS_KEYS for k in Fd[0][ds]]
    sg = {dk: np.where(Fd[0][dk[0]][dk[1]]["att"] == 0, 1.0, -1.0) for dk in keys}
    cat = lambda z: np.concatenate([z[dk] * sg[dk] for dk in keys])
    W, zd, zc, sdc_j = [], [], [], []
    for F in Fd:
        X = np.concatenate([F[ds][k]["X"][:, :-1] * sg[(ds, k)][:, None] for ds, k in keys])
        w = np.linalg.solve(np.cov(X.T), X.mean(0))
        W.append(w / (X @ w).std())
        zd.append({(ds, k): F[ds][k]["X"][:, :-1] @ W[-1] for ds, k in keys})
    for F in Fc:
        sdc_j.append(np.concatenate([F[ds][k]["X"][:, -1] * sg[(ds, k)] for ds, k in keys]).std())
        zc.append({(ds, k): F[ds][k]["X"][:, -1] / sdc_j[-1] for ds, k in keys})
    md = {dk: np.mean([z[dk] for z in zd], 0) for dk in keys}
    mc = {dk: np.mean([z[dk] for z in zc], 0) for dk in keys}
    sdd, sdc = cat(md).std(), cat(mc).std()
    emis = {}
    for x in a_list:
        f = cat({dk: (1 - x) * md[dk] / sdd + x * mc[dk] / sdc for dk in keys})
        emis[x] = (float(f.mean()), float(f.std()))
    # ---- external sets
    out = {}
    for ds in datasets:
        dn = f"aasd_c1{dec_tag}" if ds == "AASD" else f"ext_c1_{ds}{dec_tag}"
        Ed = [pickle.load(open(decoder_external_file(f"{dn}_evidence_s{d[1]}.pkl"), "rb")) for d in decs]
        Ec = [pickle.load(open(os.path.join(RES, f"ext_clf_{ds}{c[1]}_evidence_s{c[2]}.pkl"), "rb")) for c in clfs]
        out[ds] = {}
        for x in a_list:
            ev = {}
            for k in Ed[0]:
                d = np.mean([(e[k]["r0f"].astype(float) - e[k]["r1f"].astype(float)) @ w for e, w in zip(Ed, W)], 0) / sdd
                c = np.mean([(e[k]["r0"] - e[k]["r1"]).astype(float) / s for e, s in zip(Ec, sdc_j)], 0) / sdc
                ec, te = Ec[0][k], Ed[0][k]["t_end"].astype(float)
                n = min(len(d), len(c) - OFF)
                assert np.allclose(te[:n], ec["t_end"][OFF:OFF + n] + (4 - OFF) * 0.25, atol=1e-3), k
                ev[k] = dict(r0=(1 - x) * d[:n] + x * c[OFF:OFF + n], r1=np.zeros(n), att=ec["att"][OFF:OFF + n], t_end=ec["t_end"][OFF:OFF + n],
                             att128=ec["att128"], n128=ec["n128"], subject=ec["subject"])
            out[ds][x] = ev
    return out, emis


if __name__ == "__main__":
    ap = argparse.ArgumentParser()
    ap.add_argument("--dec", nargs="+", required=True)
    ap.add_argument("--clf", nargs="+", required=True)
    ap.add_argument("--a", nargs="+", type=float, default=[0.0, 0.3, 0.5, 0.7, 1.0])
    ap.add_argument("--pstay", nargs="+", type=float, default=[0.9997, 0.998, 0.995, 0.99, 0.98, 0.95])
    ap.add_argument("--dataset", default="AASD,Carta2026")
    ap.add_argument("--name", default="")
    ap.add_argument("--dec-tag", default="_best", help="tag of the decoders' external evidence files (eval_external.py --tag)")
    a = ap.parse_args()
    decs, clfs = [x.split(":") for x in a.dec], [x.split(":") for x in a.clf]
    EV, emis = build_external(decs, clfs, a.a, a.dataset.split(","), a.dec_tag)
    rows = []
    for ds in EV:
        for x in a.a:
            ev = EV[ds][x]
            pl = E.plain(ev)
            for p in a.pstay:
                r = E.score(ev, *emis[x], p, LA)
                rows.append(dict(name=a.name, ds=ds, a=x, p_stay=p, H=r["H"], Acc=r["Acc"], SDT=r["SDT"], nSwitches=r["nSwitches"],
                                 nUndetected=r["nUndetected"], acc_step=pl["acc1"], n_dec=len(decs), n_clf=len(clfs)))
    df = pd.DataFrame(rows)
    out = os.path.join(RES, "fuse_external.csv")
    df.to_csv(out, mode="a", header=not os.path.exists(out), index=False)
    pd.set_option("display.width", 220)
    print(f"=== {a.name}: {len(decs)} decoders, {len(clfs)} classifiers; REAL switches ===")
    for ds, g in df.groupby("ds"):
        print(f"\n{ds}: H by mixing share a (rows) and p_stay (columns)\n", g.pivot(index="a", columns="p_stay", values="H").round(4).to_string())
        print(f"{ds}: Acc\n", g.pivot(index="a", columns="p_stay", values="Acc").round(1).to_string())
        print(f"{ds}: SDT\n", g.pivot(index="a", columns="p_stay", values="SDT").round(1).to_string())
        print(f"{ds}: single-step accuracy by a:", g.groupby("a").acc_step.first().round(3).to_dict())
