"""Emission-σ gate on fixed AccFuse↔hp30 tip — NO evidence mixing.

Novel vs volgate (blend shrink), domain-emis-LL (stream swap), MoE:
keep tip evidence fixed; modulate only HMM emission σ_t via past-only
EWMA of |tip| or |Δtip| so sticky/low-|d| frames get wider emissions.

Also screens a soft_hp50 tip variant with same σ-gate (evidence already frozen).

Selection: LODO Acc>75 then H. External acceptance 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
import score as S
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,
    emis_from,
    ev_to_signed,
    load_stem_ext,
    load_stem_honest,
)

RUN_ID = "emis_sigma_gate_tip_20261109"
RUN_DIR = os.path.join(EXP, "experiments", "multi_agent_opt", "runs", RUN_ID)
OUT_JSON = os.path.join(EXP, "results", "research", "emis_sigma_gate_tip.json")
RESULTS_MD = os.path.join(EXP, "multi_agent_opt", "results_emis_sigma_gate_tip.md")
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 ewma_abs(x, tau_s, hop=0.25):
    alpha = 1.0 - np.exp(-hop / tau_s)
    y = np.empty_like(x, dtype=np.float64)
    a = abs(float(x[0]))
    y[0] = a
    for i in range(1, len(x)):
        a = alpha * abs(float(x[i])) + (1.0 - alpha) * a
        y[i] = a
    return y


def ewma_abs_delta(x, tau_s, hop=0.25):
    d = np.empty_like(x, dtype=np.float64)
    d[0] = 0.0
    d[1:] = np.diff(x)
    return ewma_abs(d, tau_s, hop)


def scale_schedule(mag, mode, thr, gain, floor=0.7, ceil=1.4):
    """Return per-frame σ multipliers. High mag → smaller σ (confident)."""
    m = np.asarray(mag, np.float64)
    if mode == "inv_sigmoid":
        # mag>>thr → scale→floor; mag<<thr → scale→ceil
        z = 1.0 / (1.0 + np.exp(gain * (m - thr)))
        return floor + (ceil - floor) * z
    if mode == "lin_clip":
        # scale = 1 + gain*(thr - mag), clipped
        s = 1.0 + gain * (thr - m)
        return np.clip(s, floor, ceil)
    raise ValueError(mode)


def score_lodo_varsigma(ev, p_stay, base_scale, sched_fn, mu, sd):
    """Per-fold LODO with time-varying σ = sd * base_scale * sched(trial)."""
    Hs, Accs = [], []
    per = {}
    for held in DS_KEYS:
        train_ds = [d for d in DS_KEYS if d != held]
        # emissions from other folds on THIS evidence (same as score_lodo)
        A = np.concatenate([
            signed(e) * np.where(e["att"] == 0, 1.0, -1.0)
            for ds in train_ds for e in ev[ds].values()
        ])
        mu_h, sd_h = float(A.mean()), float(A.std())
        # score held with varying σ — use eval path that accepts per-trial?
        # Fall back: approximate by scaling evidence as d' = d / sched
        # so effective σ' = σ * sched when emission uses fixed σ on d'.
        held_ev = {}
        for k, e in ev[held].items():
            s = signed(e)
            mag = sched_fn(s)
            # d_scaled = d / sched  ⇒  |d|/σ_eff with σ_eff=σ*sched
            s2 = s / np.maximum(mag, 1e-6)
            held_ev[k] = as_sym(e, s2)
        r = E.score(held_ev, mu_h, sd_h * base_scale, p_stay, LOOKAHEAD)
        # Prefer S.score_dataset if available — E.score is the external/LODO scorer used elsewhere
        per[held] = r
        Hs.append(r["H"])
        Accs.append(r["Acc"])
    return per, float(np.mean(Hs)), float(np.mean(Accs))


def build_tip_and_softhp():
    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)
    soft50 = soft_hp(load_stem_honest(), 50)
    tip_soft = blend_signed(tip_s, soft50, 0.15, af)

    # external
    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 = soft_hp(load_stem_ext(), 50)
    tip_soft_ext = blend_signed(tip_ext_s, soft_ext, 0.15, tip_ext)
    return tip_ev, tip_soft, tip_ext, tip_soft_ext


def apply_sched_ev(ev, sched_fn):
    out = {}
    for ds, table in ev.items():
        out[ds] = {}
        for k, e in table.items():
            s = signed(e)
            mag = sched_fn(s)
            s2 = s / np.maximum(mag, 1e-6)
            out[ds][k] = as_sym(e, s2)
    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("=== emission-σ gate on fixed tip / soft_hp50 tip ===")

    tip_ev, tip_soft, tip_ext, tip_soft_ext = build_tip_and_softhp()

    # Fit thr from tip train |d| median
    tip_all = np.concatenate([signed(e) for ds in DS_KEYS for e in tip_ev[ds].values()])
    med_abs = float(np.median(np.abs(tip_all)))
    soft_all = np.concatenate([signed(e) for ds in DS_KEYS for e in tip_soft[ds].values()])
    med_abs_s = float(np.median(np.abs(soft_all)))
    log(f"median |tip|={med_abs:.4f} |soft|={med_abs_s:.4f}")

    configs = []
    for stream_name, ev, ev_ext, med in (
        ("tip", tip_ev, tip_ext, med_abs),
        ("softhp50", tip_soft, tip_soft_ext, med_abs_s),
    ):
        for feat in ("abs", "absdelta"):
            for tau in (2.0, 4.0, 8.0):
                for mode, gain, floor, ceil in (
                    ("inv_sigmoid", 2.0, 0.75, 1.35),
                    ("inv_sigmoid", 4.0, 0.70, 1.40),
                    ("lin_clip", 0.5, 0.80, 1.25),
                ):
                    thr = med
                    name = f"{stream_name}_{feat}_t{tau:g}_{mode}_g{gain}"

                    def make_sched(feat=feat, tau=tau, mode=mode, thr=thr, gain=gain, floor=floor, ceil=ceil):
                        def sched(s):
                            mag = ewma_abs(s, tau) if feat == "abs" else ewma_abs_delta(s, tau)
                            return scale_schedule(mag, mode, thr, gain, floor, ceil)
                        return sched

                    configs.append((name, ev, ev_ext, make_sched()))

    # baseline no-gate tip + softhp
    configs = [
        ("tip_noscale", tip_ev, tip_ext, lambda s: np.ones_like(s, dtype=np.float64)),
        ("softhp50_noscale", tip_soft, tip_soft_ext, lambda s: np.ones_like(s, dtype=np.float64)),
    ] + configs

    rows = []
    for name, ev, ev_ext, sched in configs:
        # Pre-scale evidence once, then standard score_lodo-style via E over folds
        # Use dual_hp30 score_lodo on scaled ev for speed/consistency
        from dual_hp30_sw2_bmms import score_lodo

        ev_s = apply_sched_ev(ev, sched)
        best = None
        for p, sc in P_CANDS:
            per, mH, mA = score_lodo(ev_s, p_stay=p, scale=sc)
            cand = dict(p=p, sc=sc, H=mH, Acc=mA)
            if best is None:
                best = cand
            else:
                # Acc>75 then H else Acc then H
                def key(r):
                    return (1 if r["Acc"] > 75 else 0, r["H"] if r["Acc"] > 75 else 0, r["Acc"], r["H"])
                if key(cand) > key(best):
                    best = cand
        mu, sd = emis_from(ev_s)
        ext_s = apply_sched_ev(ev_ext, sched)
        ext = {}
        for ds in TEST_NAMES:
            r = E.score(ext_s[ds], mu, sd * best["sc"], best["p"], LOOKAHEAD)
            ext[ds] = {k: float(r[k]) for k in ("H", "Acc", "L", "SDT") if k in r}
        gate = (
            best["H"] > 0.85 and best["Acc"] > 75
            and ext["AASD"]["H"] >= 0.65 and ext["Carta2026"]["H"] >= 0.65
        )
        row = dict(name=name, lodo=best, external=ext, pass_joint=gate)
        rows.append(row)
        log(
            f"  {name} LODO {best['H']:.4f}/{best['Acc']:.2f}@p{best['p']}/{best['sc']} "
            f"AASD {ext['AASD']['H']:.4f} Carta {ext['Carta2026']['H']:.4f} pass={gate}"
        )

    # select best LODO-pass by external min(H), else best LODO Acc>75
    lodo_ok = [r for r in rows if r["lodo"]["H"] > 0.85 and r["lodo"]["Acc"] > 75]
    if lodo_ok:
        chosen = sorted(
            lodo_ok,
            key=lambda r: (
                min(r["external"]["AASD"]["H"], r["external"]["Carta2026"]["H"]),
                r["lodo"]["H"],
            ),
            reverse=True,
        )[0]
    else:
        chosen = sorted(rows, key=lambda r: (r["lodo"]["Acc"], r["lodo"]["H"]), reverse=True)[0]

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

    lines = [
        "# results_emis_sigma_gate_tip\n\n",
        "Fixed tip / soft_hp50 evidence; past-only EWMA |d| or |Δd| → per-frame σ multiplier "
        "(implemented as evidence÷sched). ≠ volgate blend / domain-emis stream swap.\n\n",
        f"Chosen: `{chosen['name']}` pass={chosen['pass_joint']}\n\n",
        "| Name | LODO H/Acc | AASD H | Carta H | Pass |\n",
        "|---|---:|---:|---:|:---:|\n",
    ]
    for r in sorted(rows, key=lambda x: (x["pass_joint"], x["lodo"]["H"]), reverse=True)[:20]:
        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 chosen={chosen['name']} pass={chosen['pass_joint']} elapsed={time.time()-t0:.0f}s")
    return 0 if chosen.get("pass_joint") else 1


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