"""Multi-τ causal HP on IRM↔synth stem, soft-blend with AccFuse↔hp30 tip.

Novel vs closed multi-τ on 4-seed/SW2 and vs τ=30-only stem_hp30_accfuse:
apply τ∈{10,15,20,25,35,40,50} (and soft/bank) to stem fuse BEFORE blending
into tip at small w. Goal: keep Carta H≥0.65 at a w that still clears
LODO H>0.85 Acc>75 — τ=30 stem needed w≈0.35 which killed Carta.

Selection: LODO Acc>75 then H. External is acceptance gate only.
"""
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
from stem_hp30_accfuse import (
    TIP_ALPHA,
    apply_hp30_signed,
    blend_signed,
    emis_from,
    ev_to_signed,
    load_stem_ext,
    load_stem_honest,
    select,
)

RUN_ID = "multitau_stem_hp_accfuse_20261108"
RUN_DIR = os.path.join(EXP, "experiments", "multi_agent_opt", "runs", RUN_ID)
OUT_JSON = os.path.join(EXP, "results", "research", "multitau_stem_hp_accfuse.json")
RESULTS_MD = os.path.join(EXP, "multi_agent_opt", "results_multitau_stem_hp_accfuse.md")
TAUS = (10, 15, 20, 25, 35, 40, 50)
WS = (0.15, 0.20, 0.25, 0.30, 0.35, 0.40)
P_CANDS = [(0.994, 0.8), (0.996, 0.8), (0.998, 0.8)]


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 soft_hp(streams, tau):
    """0.5*raw + 0.5*hp(tau)."""
    hp = apply_hp30_signed(streams, tau=tau)
    out = {}
    for ds in streams:
        out[ds] = {}
        for k in streams[ds]:
            out[ds][k] = 0.5 * streams[ds][k] + 0.5 * hp[ds][k]
    return out


def bank_mean(streams, taus):
    hps = [apply_hp30_signed(streams, tau=t) for t in taus]
    out = {}
    for ds in streams:
        out[ds] = {}
        for k in streams[ds]:
            out[ds][k] = np.mean([h[ds][k] for h in hps], 0)
    return out


def main():
    t0 = time.time()
    os.makedirs(RUN_DIR, exist_ok=True)
    open(os.path.join(RUN_DIR, "run.log"), "w").close()
    log("=== multi-τ stem HP ↔ AccFuse tip ===")

    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)
    stem_s = load_stem_honest()
    tmpl = af

    # External tip + stem
    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)
    stem_ext_s = load_stem_ext()

    variants = {}
    for tau in TAUS:
        variants[f"hp{tau}"] = apply_hp30_signed(stem_s, tau=tau)
        variants[f"soft_hp{tau}"] = soft_hp(stem_s, tau)
    variants["bank_mean_15_25_40"] = bank_mean(stem_s, (15, 25, 40))
    variants["bank_mean_10_20_40"] = bank_mean(stem_s, (10, 20, 40))

    # Precompute ext variants
    ext_vars = {}
    for name, _ in list(variants.items()):
        if name.startswith("soft_hp"):
            tau = int(name.replace("soft_hp", ""))
            ext_vars[name] = soft_hp(stem_ext_s, tau)
        elif name.startswith("hp"):
            tau = int(name.replace("hp", ""))
            ext_vars[name] = apply_hp30_signed(stem_ext_s, tau=tau)
        elif name == "bank_mean_15_25_40":
            ext_vars[name] = bank_mean(stem_ext_s, (15, 25, 40))
        elif name == "bank_mean_10_20_40":
            ext_vars[name] = bank_mean(stem_ext_s, (10, 20, 40))

    rows = []
    for vname, stem_v in variants.items():
        log(f"=== {vname} ===")
        for w in WS:
            ev = blend_signed(tip_s, stem_v, w, tmpl)
            pgrid = []
            for p, sc in P_CANDS:
                per, mH, mA = score_lodo(ev, p_stay=p, scale=sc)
                pgrid.append(dict(p=p, sc=sc, H=mH, Acc=mA))
            ok75 = [r for r in pgrid if r["Acc"] > 75]
            best_p = sorted(ok75 or pgrid, key=lambda r: (r["H"], r["Acc"]), reverse=True)[0]
            # Only score external if LODO near target (save time)
            near = best_p["Acc"] > 74 and best_p["H"] > 0.83
            ext = {}
            if near:
                mu, sd = emis_from(ev)
                ext_ev = blend_signed(tip_ext_s, ext_vars[vname], w, tip_ext)
                for ds in TEST_NAMES:
                    r = E.score(ext_ev[ds], mu, sd * best_p["sc"], best_p["p"], LOOKAHEAD)
                    ext[ds] = {k: float(r[k]) for k in ("H", "Acc", "L", "SDT") if k in r}
            pass_new = bool(
                near
                and best_p["H"] > 0.85
                and best_p["Acc"] > 75
                and ext
                and (ext["Carta2026"]["H"] >= 0.65 or ext["AASD"]["H"] >= 0.65)
            )
            row = dict(variant=vname, w=w, lodo=best_p, external=ext or None, pass_new=pass_new)
            rows.append(row)
            if near and ext:
                log(
                    f"  w={w:.2f} LODO {best_p['H']:.4f}/{best_p['Acc']:.2f}@p{best_p['p']} "
                    f"AASD {ext['AASD']['H']:.4f} Carta {ext['Carta2026']['H']:.4f} pass={pass_new}"
                )
            else:
                log(f"  w={w:.2f} LODO {best_p['H']:.4f}/{best_p['Acc']:.2f} (skip ext)")

    passers = [r for r in rows if r["pass_new"]]
    near_lodo = [r for r in rows if r["lodo"]["H"] > 0.85 and r["lodo"]["Acc"] > 75]
    if passers:
        chosen = sorted(passers, key=lambda r: (r["lodo"]["H"], r["lodo"]["Acc"]), reverse=True)[0]
    elif near_lodo:
        # LODO pick; also report best Carta among them
        chosen = sorted(near_lodo, key=lambda r: (r["lodo"]["H"], r["lodo"]["Acc"]), reverse=True)[0]
        with_ext = [r for r in near_lodo if r["external"]]
        if with_ext:
            best_carta = sorted(with_ext, key=lambda r: r["external"]["Carta2026"]["H"], reverse=True)[0]
            log(f"NO PASS; LODO-pick {chosen['variant']} w={chosen['w']}; "
                f"best_carta_among_lodo {best_carta['variant']} w={best_carta['w']} "
                f"CartaH={best_carta['external']['Carta2026']['H']:.4f}")
    else:
        chosen = sorted(rows, key=lambda r: (r["lodo"]["H"], r["lodo"]["Acc"]), reverse=True)[0]

    out = dict(
        method="multitau_stem_hp_accfuse",
        rows=rows, chosen=chosen,
        any_pass=bool(passers),
        elapsed_s=time.time() - t0,
    )
    with open(OUT_JSON, "w") as fh:
        json.dump(out, fh, indent=1, default=float)

    lines = [
        "# results_multitau_stem_hp_accfuse\n\n",
        f"any_pass={bool(passers)}; chosen={chosen.get('variant')} w={chosen.get('w')}\n\n",
        "| variant | w | LODO | AASD H | Carta H | pass |\n",
        "|---|---:|---:|---:|---:|:---:|\n",
    ]
    for r in rows:
        if not r["external"]:
            continue
        lines.append(
            f"| {r['variant']} | {r['w']:.2f} | {r['lodo']['H']:.4f}/{r['lodo']['Acc']:.2f} | "
            f"{r['external']['AASD']['H']:.4f} | {r['external']['Carta2026']['H']:.4f} | {r['pass_new']} |\n"
        )
    Path(RESULTS_MD).write_text("".join(lines))
    log(f"DONE any_pass={bool(passers)} elapsed={time.time()-t0:.1f}s")


if __name__ == "__main__":
    main()
