"""Decision stage that does not need to know the switch rate: a BANK of 2-state HMM forward filters with different
p_stay, combined online by Bayesian model averaging.

Every filter k (transition p_stay_k, the same Gaussian emissions) yields its filtering posterior P_k(t) and the
predictive likelihood c_k(t) of the evidence at step t. The bank's posterior is sum_k w_k(t) P_k(t) with
log w_k(t) = lam * log w_k(t-1) + log c_k(t)  (lam = 1: all evidence of the trial so far; lam < 1: forgetting).
Causal and within-trial (the weights restart in every trial: Terms 6.3); the bank is one fixed model for all samples.
Compared with single p_stay values on  (a) the corpus LODO folds with SYNTHETIC switches (score.py's schedule) and
(b) REAL switches (AASD, Carta2026; fuse_external.build_external), same fused evidence
(2 decoders + 2 five-second + 2 ten-second classifiers, DEC_OFFSET of fuse_evidence).
usage: python hmm_bank.py [--a 0.3 0.5]  -> results/research/hmm_bank.csv"""
import argparse
import os

import numpy as np
import pandas as pd
from joblib import Parallel, delayed

import fuse_external as FE
import fuse_general as FG
from common_exp import EXP, rows_of
from common import CACHE as EDA_CACHE, DS_KEYS, FS as FS128, SEED
from e1_switch import expand, hmm_filter, synth_trial
from fuse_evidence import LA
from scorer import aggregate, fast_score_subject

D = "vlaai_ws__mat_reac_aug_cluster_onset_dom_xFUG_ptSPK"
C, W = "direct_clf__mat_reac_ptSPKv2", "direct_clf__mat_reac_w10_ptSPK"
BANK6, BANK3 = (0.95, 0.98, 0.99, 0.995, 0.998, 0.9997), (0.98, 0.998, 0.9997)
INTEG = [(f"single p={p}", ("single", p)) for p in (0.95, 0.98, 0.99, 0.995, 0.998, 0.9997)] + \
        [(f"bank6 lam={l}", ("bank", BANK6, l)) for l in (1.0, 0.999, 0.996)] + [(f"bank3 lam={l}", ("bank", BANK3, l)) for l in (1.0, 0.999)]


def bank_filter(d, mu, sd, ps, lam):
    """P(column 1) of the model-averaged bank; ps: p_stay per filter, lam: forgetting of the log model evidence."""
    ll = np.stack([-0.5 * ((d - mu) / sd) ** 2, -0.5 * ((d + mu) / sd) ** 2], 1)
    lik = np.exp(ll - ll.max(1, keepdims=True))              # common factor per step: does not change the weights
    ps = np.asarray(ps, float)
    post, lw, out = np.full((len(ps), 2), 0.5), np.zeros(len(ps)), np.empty(len(d))
    for t in range(len(d)):
        if t > 0:
            p0 = post[:, 0] * ps + post[:, 1] * (1 - ps)
            post = np.stack([p0, 1 - p0], 1)
        j = post * lik[t]
        c = j.sum(1)
        post = j / c[:, None]
        lw = lam * lw + np.log(c)
        w = np.exp(lw - lw.max())
        out[t] = (w / w.sum()) @ post[:, 1]
    return out


def decide(d, mu, sd, spec):
    if spec[0] == "single":
        return (hmm_filter(-0.5 * ((d - mu) / sd) ** 2, -0.5 * ((d + mu) / sd) ** 2, spec[1]) > 0.5).astype(int)
    return (bank_filter(d, mu, sd, spec[1], spec[2]) > 0.5).astype(int)


def subjects_score(per):
    res = []
    for lst in per.values():
        pred, gt = np.concatenate([a for a, _ in lst]), 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))
    return aggregate(res)


def score_corpus(held, ev, spec):
    """ev[ds][key] = (d, att, t_end); emissions from the other four datasets; schedule and stamping as score.score_dataset."""
    A = np.concatenate([d * np.where(att == 0, 1.0, -1.0) for ds in DS_KEYS if ds != held for d, att, _ in ev[ds].values()])
    mu, sd = float(A.mean()), float(A.std())
    rng = np.random.default_rng(SEED + DS_KEYS.index(held))
    per = {}
    for _, r in rows_of(held).iterrows():
        T = int(r["n"])
        d, _, t_end = ev[held][r["key"]]
        att = np.load(os.path.join(EDA_CACHE, "cnd", f"{r['key']}.npz"))["att_col"][:T].astype(int)
        st, _, _ = synth_trial(r["key"], T, d, t_end, rng)             # the evidence is already switched (pre_switched)
        guess = int(rng.integers(0, 2))
        per.setdefault(r["subj_uid"], []).append((expand(decide(d, mu, sd, spec), t_end, LA, T, guess), np.where(st, 1 - att, att)))
    a = subjects_score(per)
    return dict(H=a["H"], Acc=a["Acc"], SDT=a["SDT"])


def score_real(ev, mu, sd, spec):
    rng = np.random.default_rng(SEED)
    per = {}
    for k in sorted(ev):
        e = ev[k]
        d = (e["r0"] - e["r1"]).astype(float)
        labs = expand(decide(d, mu, sd, spec), e["t_end"].astype(float), LA, e["n128"], int(rng.integers(0, 2)))
        per.setdefault(e["subject"], []).append((labs, e["att128"].astype(float)))
    a = subjects_score(per)
    return dict(H=a["H"], Acc=a["Acc"], SDT=a["SDT"])


if __name__ == "__main__":
    ap = argparse.ArgumentParser()
    ap.add_argument("--a", nargs="+", type=float, default=[0.3, 0.5])
    a = ap.parse_args()
    nj = int(os.environ.get("SCORE_JOBS", 3))
    Z, meta = FG.build([D, D + "_s1"], [C, C + "_s1", W, W + "_s1"])
    EV, emis = FE.build_external([[D, "0"], [D + "_s1", "1"]], [[C, "_v2", "0"], [C + "_s1", "_v2", "1"], [W, "_w10", "0"], [W + "_s1", "_w10", "1"]],
                                 a.a, ["AASD", "Carta2026"])
    rows = []
    for x in a.a:
        evc = {h: {ds: {k: ((1 - x) * Z[h][0][ds][k] + x * Z[h][1][ds][k], meta[ds][k][0], meta[ds][k][1].astype(float)) for k in meta[ds]}
                   for ds in DS_KEYS} for h in DS_KEYS}
        jobs = [(n, s, h) for n, s in INTEG for h in DS_KEYS]
        out = Parallel(n_jobs=nj)(delayed(score_corpus)(h, evc[h], s) for n, s, h in jobs)
        rows += [dict(a=x, integrator=n, set=h, **r) for (n, s, h), r in zip(jobs, out)]
        jobs = [(n, s, ds) for n, s in INTEG for ds in EV]
        out = Parallel(n_jobs=nj)(delayed(score_real)(EV[ds][x], *emis[x], s) for n, s, ds in jobs)
        rows += [dict(a=x, integrator=n, set=ds, **r) for (n, s, ds), r in zip(jobs, out)]
    df = pd.DataFrame(rows)
    df.to_csv(os.path.join(EXP, "results", "research", "hmm_bank.csv"), index=False)
    pd.set_option("display.width", 220)
    for x in a.a:
        g = df[df.a == x].pivot(index="integrator", columns="set", values="H").reindex([n for n, _ in INTEG])
        g.insert(0, "corpus mean (synthetic)", g[DS_KEYS].mean(1))
        g["real mean"] = g[["AASD", "Carta2026"]].mean(1)
        print(f"\n=== H, classifier share a = {x} ===\n", g.round(4).to_string())
        s = df[df.a == x].pivot(index="integrator", columns="set", values="SDT").reindex([n for n, _ in INTEG])
        print("SDT (s):\n", s.round(1).to_string())
