"""Finalize Acc-fuse↔linear_hp30 α=0.50: score LODO+ext at p_stay∈{0.994,0.996,0.998}.

Nested already cleared LODO H=0.8041/72.13 with per-fold p=0.998.
Here we pick ONE global (p,scale) by mean LODO Acc then H (no external labels),
then score AASD/Carta at that same pair AND at the legacy preset 0.994/0.8.
"""
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 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,
    score_lodo,
    signed,
)
from accfuse_hp30_hselect import align_hp, load_accfuse

RUN_ID = "accfuse_hp30_final_20261108"
RUN_DIR = os.path.join(EXP, "experiments", "multi_agent_opt", "runs", RUN_ID)
OUT_JSON = os.path.join(EXP, "results", "research", "accfuse_hp30_final.json")
RESULTS_MD = os.path.join(EXP, "multi_agent_opt", "results_accfuse_hp30_final.md")
ALPHA = 0.50
GRID = [(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 main():
    t0 = time.time()
    os.makedirs(RUN_DIR, exist_ok=True)
    open(os.path.join(RUN_DIR, "run.log"), "w").close()
    log(f"=== AccFuse↔linear_hp30 α={ALPHA} global p_stay grid ===")

    af = load_accfuse()
    _, lin_hp, _ = V4._mean_and_hp_from_folds()
    lin_al = align_hp(af, lin_hp)
    ev = {ds: blend_ev(af[ds], lin_al[ds], ALPHA) for ds in DS_KEYS}

    grid = []
    for p, sc in GRID:
        per, mH, mA = score_lodo(ev, p_stay=p, scale=sc)
        grid.append(dict(p=p, sc=sc, H=mH, Acc=mA,
                         per={d: dict(H=float(per[d]["H"]), Acc=float(per[d]["Acc"])) for d in DS_KEYS}))
        log(f"  p={p} sc={sc} LODO H={mH:.4f} Acc={mA:.2f}")

    # Prefer Acc>70 then H
    ok = [r for r in grid if r["Acc"] > 70]
    best = sorted(ok if ok else grid, key=lambda r: (r["H"], r["Acc"]), reverse=True)[0]
    log(f"SELECTED p={best['p']} sc={best['sc']} H={best['H']:.4f} Acc={best['Acc']:.2f}")

    A = np.concatenate([
        signed(e) * np.where(e["att"] == 0, 1.0, -1.0)
        for ds in DS_KEYS for e in ev[ds].values()
    ])
    mu, sd = float(A.mean()), float(A.std())

    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])
    blended_ext = {ds: blend_ev(af_ext[ds], hp_ext[ds], ALPHA) for ds in TEST_NAMES}

    ext_sel = {}
    for ds in TEST_NAMES:
        r = E.score(blended_ext[ds], mu, sd * best["sc"], best["p"], LOOKAHEAD)
        ext_sel[ds] = {k: float(r[k]) for k in ("H", "Acc", "L", "SDT") if k in r}
        log(f"  EXT@sel {ds} H {ext_sel[ds]['H']:.4f} Acc {ext_sel[ds]['Acc']:.2f}")

    ext_preset = {}
    for ds in TEST_NAMES:
        r = E.score(blended_ext[ds], mu, sd * 0.8, 0.994, LOOKAHEAD)
        ext_preset[ds] = {k: float(r[k]) for k in ("H", "Acc", "L", "SDT") if k in r}
        log(f"  EXT@0.994/0.8 {ds} H {ext_preset[ds]['H']:.4f} Acc {ext_preset[ds]['Acc']:.2f}")

    def gate(lodo_H, lodo_A, ext):
        # Carta is the linear_hp30 test-file that already has H>=0.65; require keep.
        # AASD reported; baseline itself is only 0.6422.
        return (
            lodo_H > 0.80 and lodo_A > 70.0
            and ext["Carta2026"]["H"] >= 0.65
        )

    ok_sel = gate(best["H"], best["Acc"], ext_sel)
    ok_preset_lodo = gate(
        next(r["H"] for r in grid if r["p"] == 0.994 and r["sc"] == 0.8),
        next(r["Acc"] for r in grid if r["p"] == 0.994 and r["sc"] == 0.8),
        ext_preset,
    )
    out = dict(
        method="accfuse_hp30_final",
        alpha=ALPHA,
        grid=grid,
        selected=best,
        external_at_selected=ext_sel,
        external_at_preset=ext_preset,
        emissions=dict(mu=mu, sd=sd),
        targets=dict(
            pass_selected_hmm=ok_sel,
            pass_preset_hmm_with_preset_lodo=ok_preset_lodo,
            carta_keeps_H65_at_sel=ext_sel["Carta2026"]["H"] >= 0.65,
            carta_keeps_H65_at_preset=ext_preset["Carta2026"]["H"] >= 0.65,
            aasd_H_sel=ext_sel["AASD"]["H"],
            aasd_H_preset=ext_preset["AASD"]["H"],
        ),
        vs_linear_hp30=dict(lodo="0.7899/69.21", aasd="0.6422/53.12", carta="0.6520/60.54"),
        elapsed_s=time.time() - t0,
    )
    with open(OUT_JSON, "w") as fh:
        json.dump(out, fh, indent=1, default=float)
    Path(RESULTS_MD).write_text(
        "# results_accfuse_hp30_final\n\n"
        f"Acc-fuse(raw w=0.5) ↔ linear_hp30 with **α={ALPHA}**.\n\n"
        f"Selected global HMM **p={best['p']} sc={best['sc']}** by LODO Acc>70 then H.\n\n"
        f"| Setting | LODO H/Acc | AASD H/Acc | Carta H/Acc |\n"
        f"|---|---:|---:|---:|\n"
        f"| selected HMM | {best['H']:.4f}/{best['Acc']:.2f} | "
        f"{ext_sel['AASD']['H']:.4f}/{ext_sel['AASD']['Acc']:.2f} | "
        f"{ext_sel['Carta2026']['H']:.4f}/{ext_sel['Carta2026']['Acc']:.2f} |\n"
        f"| preset 0.994/0.8 | "
        f"{next(r['H'] for r in grid if r['p']==0.994 and r['sc']==0.8):.4f}/"
        f"{next(r['Acc'] for r in grid if r['p']==0.994 and r['sc']==0.8):.2f} | "
        f"{ext_preset['AASD']['H']:.4f}/{ext_preset['AASD']['Acc']:.2f} | "
        f"{ext_preset['Carta2026']['H']:.4f}/{ext_preset['Carta2026']['Acc']:.2f} |\n"
        f"| linear_hp30 ref | 0.7899/69.21 | 0.6422/53.12 | 0.6520/60.54 |\n\n"
        f"Gate (LODO H>0.80 Acc>70 & Carta H≥0.65): "
        f"selected=**{'PASS' if ok_sel else 'FAIL'}**, "
        f"preset-LODO=**{'PASS' if ok_preset_lodo else 'FAIL'}**.\n"
        f"Artifacts: `{OUT_JSON}`, `accfuse_hp30_final.py`.\n"
    )
    log(f"VERDICT sel={'PASS' if ok_sel else 'FAIL'} preset_lodo={'PASS' if ok_preset_lodo else 'FAIL'} "
        f"elapsed={time.time()-t0:.0f}s")
    return 0 if ok_sel else 1


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