"""HMM calibration for REAL switches, cross-fitted between AASD and Carta2026 (Kieu 2 of REAL_SWITCH_STRATEGY.md).

The 2-state HMM treats the overlapping-window evidence as independent (autocorrelation 0.92-0.95 at 0.25 s) and uses a dwell prior of
125 s (p_stay 0.998) against real dwell times of 10-25 s. Two knobs repair this without any new model:
  p_stay  per-0.25-s-step stay probability
  scale   emission sd multiplier (log-likelihood tempered by 1 / scale^2; scale > 1 trusts the evidence less per step)
Evidence = mean of the 4 classifier logits (the classifiers trained on the 5 corpus datasets + Fuglsang, no AASD / Carta label);
emission mu, sd from the corpus (4-seed mean, out-of-fold, synthetic switches) as before.
For each target set T in {AASD, Carta2026}: (p_stay, scale) is
  corpus  the usual choice (0.998, 1)                                  -> the reference
  cross   the grid point with the best H on the OTHER real set          -> honest estimate for a new real set
  oracle  the grid point with the best H on T itself (upper bound, not an estimate)
  common  the grid point with the best mean H over BOTH sets (what a single submission would use; slightly optimistic)
usage: python hmm_real_crossfit.py -> results/research/hmm_real_crossfit.csv"""
import os
import pickle
import sys

import numpy as np
import pandas as pd

import eval_external as E
import score as S
from common import DS_KEYS
from common_exp import EXP

R = os.path.join(EXP, "results", "research")
REF = ["direct_clf__eaP_n5_t60_w10b3_ptSPK" + t + "_aug-sw2" for t in ("", "_s1", "_s2", "_s3")]
P_STAY = (0.998, 0.99, 0.97, 0.95, 0.9)
SCALES = (1.0, 1.5, 2.0, 3.0, 4.0, 6.0)


def main():
    evs = [S.load_folds(e, "causal")[0] for e in REF]
    A = np.concatenate([np.mean([(ev[ds][k]["r0"] - ev[ds][k]["r1"]) for ev in evs], 0) * np.where(evs[0][ds][k]["att"] == 0, 1.0, -1.0)
                        for ds in DS_KEYS for k in evs[0][ds]])
    mu, sd = float(A.mean()), float(A.std())
    ev = {}
    for ds in ("AASD", "Carta2026"):
        E4 = [pickle.load(open(os.path.join(R, f"ext_clf_{ds}_w10b3sw2P_evidence_s{i}.pkl"), "rb")) for i in range(4)]
        ev[ds] = {k: dict(E4[0][k], r0=np.mean([(e[k]["r0"] - e[k]["r1"]) for e in E4], 0), r1=np.zeros(len(E4[0][k]["r0"]))) for k in E4[0]}
    rows = []
    for ds in ev:
        for p in P_STAY:
            for sc in SCALES:
                r = E.score(ev[ds], mu, sd * sc, p, 0.75)
                rows.append(dict(ds=ds, p_stay=p, scale=sc, H=r["H"], Acc=r["Acc"], SDT=r["SDT"], L=r["L"]))
                print(rows[-1], flush=True)
    df = pd.DataFrame(rows)
    df.to_csv(os.path.join(R, "hmm_real_crossfit.csv"), index=False)
    piv = df.pivot_table(index=["p_stay", "scale"], columns="ds", values="H")
    piv["mean"] = piv.mean(1)
    print("\nH per (p_stay, scale)\n", piv.round(3).to_string())
    out = []
    for t, o in (("AASD", "Carta2026"), ("Carta2026", "AASD")):
        ref = df[(df.ds == t) & (df.p_stay == 0.998) & (df.scale == 1.0)].iloc[0]
        cross = piv[o].idxmax()
        common = piv["mean"].idxmax()
        orc = piv[t].idxmax()
        out.append((t, "corpus (0.998, 1)", ref.H, ref.Acc, ref.SDT))
        for name, key in (("cross-fit " + str(cross), cross), ("common " + str(common), common), ("oracle " + str(orc), orc)):
            r = df[(df.ds == t) & (df.p_stay == key[0]) & (df.scale == key[1])].iloc[0]
            out.append((t, name, r.H, r.Acc, r.SDT))
    print("\n" + pd.DataFrame(out, columns=["target", "choice (p_stay, scale)", "H", "Acc", "SDT"]).round(3).to_string(index=False))


if __name__ == "__main__":
    main()
