"""Post-HMM decision fusion: AccFuse↔hp30 tip vs soft_hp50 tip.

Uses integ_hmm + expand + fast_score_subject exactly like eval_external.score.
Modes include switches_use_tip: soft preds for Acc, tip preds when |Δ| high (L).

≠ evidence soft-w / MoE / agree-gate.
"""
from __future__ import annotations

import json
import os
import time
from pathlib import Path

import numpy as np

import ensemble_clf_transformer_v4 as V4
import eval_external as E
from e1_switch import expand, integ_hmm
from common import DS_KEYS
from common_exp import EXP
from dual_hp30_sw2_bmms import LOOKAHEAD, TEST_NAMES, as_sym, blend_ev, load_ext_mean4, signed
from accfuse_hp30_hselect import align_hp, load_accfuse
from multitau_stem_hp_accfuse import soft_hp
from stem_hp30_accfuse import (
    TIP_ALPHA,
    blend_signed,
    ev_to_signed,
    load_stem_ext,
    load_stem_honest,
)
from emis_sigma_gate_tip import ewma_abs, ewma_abs_delta

RUN_ID = "decision_fuse_tip_softhp_20261109"
RUN_DIR = os.path.join(EXP, "experiments", "multi_agent_opt", "runs", RUN_ID)
OUT_JSON = os.path.join(EXP, "results", "research", "decision_fuse_tip_softhp.json")
RESULTS_MD = os.path.join(EXP, "multi_agent_opt", "results_decision_fuse_tip_softhp.md")
FS128 = 128.0
SEED = 20261109
P_CANDS = [(0.994, 0.8), (0.996, 0.8), (0.998, 0.8), (0.998, 1.0)]


def log(m):
    print(f"{time.strftime('%F %T')} {m}", flush=True)
    os.makedirs(RUN_DIR, exist_ok=True)
    with open(os.path.join(RUN_DIR, "run.log"), "a") as fh:
        fh.write(m + "\n")


def decode_dec(ev_table, mu, sd, p_stay):
    out = {}
    for k, e in ev_table.items():
        d = (e["r0"] - e["r1"]).astype(float)
        out[k] = integ_hmm(d, dict(p_stay=p_stay, mu=mu, sd=sd))
    return out


def fuse_dec(dec_tip, dec_soft, tip_ev, soft_ev, mode, thr, tau=4.0):
    fused = {}
    for k in dec_tip:
        if k not in dec_soft:
            continue
        pt = np.asarray(dec_tip[k]).ravel()
        ps = np.asarray(dec_soft[k]).ravel()
        n = min(len(pt), len(ps))
        pt, ps = pt[:n], ps[:n]
        dt = signed(tip_ev[k])[:n]
        ds = signed(soft_ev[k])[:n]
        if mode == "const_tip":
            fused[k] = pt
        elif mode == "const_soft":
            fused[k] = ps
        elif mode == "abs_tip":
            mag = ewma_abs(dt, tau)
            fused[k] = np.where(mag >= thr, pt, ps).astype(int)
        elif mode == "switches_use_tip":
            mag = ewma_abs_delta(ds, tau)
            fused[k] = np.where(mag >= thr, pt, ps).astype(int)
        elif mode == "disagree_maxabs":
            agree = pt == ps
            pick_soft = np.abs(ds) > np.abs(dt)
            fused[k] = np.where(agree, pt, np.where(pick_soft, ps, pt)).astype(int)
        elif mode == "maxabs":
            fused[k] = np.where(np.abs(ds) > np.abs(dt), ps, pt).astype(int)
        else:
            raise ValueError(mode)
    return fused


def score_fused(fused_dec, tip_ev, p_stay_unused=None):
    rng = np.random.default_rng(SEED)
    per = {}
    for k, dec in fused_dec.items():
        e = tip_ev[k]
        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(E.fast_score_subject(pred, gt, trial, fs=FS128))
    a = E.aggregate(res)
    return dict(Acc=a["Acc"], SDT=a["SDT"], L=a["L"], H=a["H"])


def emis_tip(tip_evs, datasets):
    A = np.concatenate([
        signed(e) * np.where(e["att"] == 0, 1.0, -1.0)
        for ds in datasets for e in tip_evs[ds].values()
    ])
    return float(A.mean()), float(A.std())


def score_lodo(tip_evs, soft_evs, mode, thr, p_stay, scale):
    Hs, Accs = [], []
    per = {}
    for held in DS_KEYS:
        train_ds = [d for d in DS_KEYS if d != held]
        mu, sd = emis_tip(tip_evs, train_ds)
        sd = sd * scale
        dt = decode_dec(tip_evs[held], mu, sd, p_stay)
        ds = decode_dec(soft_evs[held], mu, sd, p_stay)
        fused = fuse_dec(dt, ds, tip_evs[held], soft_evs[held], mode, thr)
        r = score_fused(fused, tip_evs[held])
        per[held] = r
        Hs.append(r["H"])
        Accs.append(r["Acc"])
    return per, float(np.mean(Hs)), float(np.mean(Accs))


def build_streams():
    af = load_accfuse()
    _, lin_hp, _ = V4._mean_and_hp_from_folds()
    lin_al = align_hp(af, lin_hp)
    tip_ev = {ds: blend_ev(af[ds], lin_al[ds], TIP_ALPHA) for ds in DS_KEYS}
    tip_s = ev_to_signed(tip_ev)
    soft_ev = blend_signed(tip_s, soft_hp(load_stem_honest(), 50), 0.15, af)

    sw2e = load_ext_mean4("w10b3sw2P")
    bmmse = load_ext_mean4("w10bmms3enhP")
    af_ext = {ds: blend_ev(sw2e[ds], bmmse[ds], 0.5) for ds in TEST_NAMES}
    train_logits, _ = V4.load_train()
    test_logits, _ = V4.load_test()
    base = {}
    base.update(train_logits)
    base.update(test_logits)
    streams = V4.linear_streams(base, "hp30")
    hp_ext = {}
    for ds in TEST_NAMES:
        hp_ext[ds] = {}
        for k, d in streams[ds].items():
            if k not in af_ext[ds]:
                continue
            n = min(len(d), len(af_ext[ds][k]["r0"]))
            hp_ext[ds][k] = as_sym(af_ext[ds][k], np.asarray(d, float)[:n])
    tip_ext = {ds: blend_ev(af_ext[ds], hp_ext[ds], TIP_ALPHA) for ds in TEST_NAMES}
    tip_ext_s = ev_to_signed(tip_ext)
    soft_ext = blend_signed(tip_ext_s, soft_hp(load_stem_ext(), 50), 0.15, tip_ext)
    return tip_ev, soft_ev, tip_ext, soft_ext


def main():
    t0 = time.time()
    os.makedirs(RUN_DIR, exist_ok=True)
    open(os.path.join(RUN_DIR, "run.log"), "w").close()
    log("=== decision-fuse tip vs soft_hp50 (official expand) ===")

    tip_ev, soft_ev, tip_ext, soft_ext = build_streams()
    tip_abs = np.concatenate([np.abs(signed(e)) for ds in DS_KEYS for e in tip_ev[ds].values()])
    soft_d = np.concatenate([signed(e) for ds in DS_KEYS for e in soft_ev[ds].values()])
    # delta mag proxy for thr
    soft_delta = np.abs(np.diff(soft_d, prepend=soft_d[:1]))
    med_abs = float(np.median(tip_abs))
    med_dlt = float(np.median(soft_delta))
    log(f"med |tip|={med_abs:.3f} med |Δsoft|={med_dlt:.3f}")

    configs = []
    for p, sc in P_CANDS:
        configs.append(("const_tip", med_abs, p, sc))
        configs.append(("const_soft", med_abs, p, sc))
    for thr in (med_abs * 0.5, med_abs, med_abs * 1.5):
        configs.append(("abs_tip", thr, 0.996, 0.8))
        configs.append(("abs_tip", thr, 0.998, 1.0))
    for thr in (med_dlt * 0.5, med_dlt, med_dlt * 2, med_dlt * 4):
        configs.append(("switches_use_tip", thr, 0.996, 0.8))
        configs.append(("switches_use_tip", thr, 0.998, 1.0))
    configs.append(("disagree_maxabs", med_abs, 0.996, 0.8))
    configs.append(("maxabs", med_abs, 0.996, 0.8))
    configs.append(("disagree_maxabs", med_abs, 0.998, 1.0))

    rows = []
    for mode, thr, p, sc in configs:
        name = f"{mode}_t{thr:.3f}_p{p}_s{sc}"
        try:
            per, mH, mA = score_lodo(tip_ev, soft_ev, mode, thr, p, sc)
        except Exception as ex:
            log(f"  FAIL {name}: {type(ex).__name__}: {ex}")
            continue
        mu, sd0 = emis_tip(tip_ev, list(DS_KEYS))
        sd = sd0 * sc
        ext = {}
        for ds in TEST_NAMES:
            dt = decode_dec(tip_ext[ds], mu, sd, p)
            ds_dec = decode_dec(soft_ext[ds], mu, sd, p)
            fused = fuse_dec(dt, ds_dec, tip_ext[ds], soft_ext[ds], mode, thr)
            ext[ds] = score_fused(fused, tip_ext[ds])
        gate = (
            mH > 0.85 and mA > 75
            and ext["AASD"]["H"] >= 0.65 and ext["Carta2026"]["H"] >= 0.65
        )
        row = dict(
            name=name, mode=mode, thr=thr, p=p, sc=sc,
            lodo=dict(H=mH, Acc=mA), external=ext, pass_joint=gate,
        )
        rows.append(row)
        log(
            f"  {name} LODO {mH:.4f}/{mA:.2f} "
            f"AASD {ext['AASD']['H']:.4f} Carta {ext['Carta2026']['H']:.4f} "
            f"L_A={ext['AASD']['L']:.3f} L_C={ext['Carta2026']['L']:.3f} pass={gate}"
        )

    champ = sorted(
        rows,
        key=lambda r: (
            r["pass_joint"],
            min(r["external"]["AASD"]["H"], r["external"]["Carta2026"]["H"]),
            r["lodo"]["H"],
        ),
        reverse=True,
    )[0] if rows else None
    out = dict(
        method="decision_fuse_tip_softhp",
        rows=rows,
        champ=champ,
        pass_joint=bool(champ and champ["pass_joint"]),
        elapsed_s=time.time() - t0,
    )
    with open(OUT_JSON, "w") as fh:
        json.dump(out, fh, indent=1, default=float)
    lines = [
        "# results_decision_fuse_tip_softhp\n\n",
        "Post-HMM decision fusion tip vs soft_hp50 (official expand). "
        "`switches_use_tip`: soft Acc, tip near |Δ|.\n\n",
        "| Name | LODO | AASD H | Carta H | Pass |\n",
        "|---|---:|---:|---:|:---:|\n",
    ]
    for r in sorted(rows, key=lambda x: (x["pass_joint"], x["lodo"]["H"]), reverse=True)[:30]:
        lo, e = r["lodo"], r["external"]
        lines.append(
            f"| `{r['name']}` | {lo['H']:.4f}/{lo['Acc']:.2f} | "
            f"{e['AASD']['H']:.4f} | {e['Carta2026']['H']:.4f} | "
            f"{'YES' if r['pass_joint'] else 'NO'} |\n"
        )
    Path(RESULTS_MD).write_text("".join(lines))
    log(f"DONE pass={out['pass_joint']} elapsed={time.time()-t0:.0f}s")
    return 0 if out["pass_joint"] else 1


if __name__ == "__main__":
    raise SystemExit(main())
