"""Fold-safe temperature / Platt calibration on frozen stem_ms96 (+ SWA) logits.

Fit T (temperature) and/or Platt (a, b) on LODO non-held-out corpus folds only.
Never use AASD/Carta labels. Apply calibrated logits through fixed HMM
(p_stay 0.994, scale 0.8) and through causal tau-30 high-pass (hp30).

LODO: per held-out H, load stem_ms96__held-H (or SWA held-H), infer all corpus,
fit on datasets != H, score H. External: fit on all five corpus streams from
SWA all5 (and stem held-AVGC fine-tuned is not used; stem external uses SWA
all5 counterpart for SWA arm, stem arm uses stem held-* mean emissions from
an all-five SWA or stem-AVGC init — here stem external uses SWA all5 features
path but we also score stem with a corpus-fit from stem held-AVGC streams on
all five after loading stem held-AVGC as a proxy only if STEM_ALL5 is absent;
prefer models/logit_tf_v2 has no all5 — use SWA all5 for stem-external emissions
fit after applying stem-fitted params from all five via held-AVGC? Honest:
external stem arm fits calib on union of five LODO non-held trains is wrong.
Standard: train one model on all five. We use SWA all5 for the SWA arm; for
stem arm we fit calib params on the five corpus streams produced by averaging
the five stem held-* predictions on each trial (leave-one-out stream average
would leak). Safer: fit calib on each held-*'s train folds, apply that fold's
params only to LODO; for external use params fit on all five using SWA all5
streams (SWA arm) and separately fit on all five using stem by loading each
held-* and scoring only its train folds then pooling logits (no held leak).

usage:
  python stem_ms96_calib.py
"""
import json
import os
import pickle
import time

import numpy as np
import torch
from scipy.optimize import minimize_scalar, minimize
from scipy.special import expit

import ensemble_clf_nested as N
import ensemble_clf_transformer_v2 as V
import ensemble_clf_transformer_v4 as V4
import eval_external as E
from common import DS_KEYS

EXP = os.path.dirname(os.path.abspath(__file__))
STEM_DIR = os.path.join(EXP, "models", "logit_tf_v2")
SWA_DIR = os.path.join(EXP, "models", "stem_ms96_swa")
LOG = os.path.join(EXP, "experiments", "stem_ms96_calib.log")
OUT = os.path.join(EXP, "results", "research", "stem_ms96_calib.json")
CKPT_DIR = os.path.join(EXP, "models", "stem_ms96_calib")
EVID = "/root/disk_3tb/EEG-sAAD-Grand-Challenge-Training-Dataset/experiments/decoder_lodo/results/research"
TEST_NAMES = ("AASD", "Carta2026")

WINDOW = 96
P_STAY = 0.994
SCALE = 0.8
# subsample frames for NLL fit (speed); still uses only train-fold labels
MAX_FIT = int(os.environ.get("CALIB_MAX_FIT", "400000"))
SEED = 20270101
HELD_LIST = os.environ.get("CALIB_HELD", "").strip()
HELD_KEYS = [h.strip() for h in HELD_LIST.split(",") if h.strip()] or list(DS_KEYS)
DO_SWA = os.environ.get("CALIB_SWA", "1") == "1"


def log_line(text):
    print(text, flush=True)
    os.makedirs(os.path.dirname(LOG), exist_ok=True)
    with open(LOG, "a") as fh:
        fh.write(text + "\n")


def load_stem(path, device):
    blob = torch.load(path, map_location="cpu", weights_only=False)
    _name, _family, window, d_model, nlayers, stem, _lr = blob["cfg"]
    if window != WINDOW:
        raise RuntimeError(f"unexpected window {window} in {path}")
    c_in = int(np.asarray(blob["mu"]).shape[0])
    net = V.LogitTransformer(c_in, window, d_model=d_model, nlayers=nlayers, stem=bool(stem))
    good_mask = net.mask.detach().clone()
    net.load_state_dict(blob["state"])
    net.mask.copy_(good_mask)
    net = net.to(device).eval()
    for p in net.parameters():
        p.requires_grad_(False)
    mu = np.asarray(blob["mu"], np.float32)
    sd = np.asarray(blob["sd"], np.float32)
    return net, mu, sd


def collect_xy(streams, att, datasets, max_fit=MAX_FIT, rng=None):
    """Binary labels y=(att==0); logits from streams. Subsample for fit speed."""
    rng = rng or np.random.default_rng(SEED)
    xs, ys = [], []
    for ds in datasets:
        labels = att[ds]
        for key, d in streams[ds].items():
            z = np.asarray(d, np.float64).reshape(-1)
            y = (np.asarray(labels[key]) == 0).astype(np.float64).reshape(-1)
            n = min(len(z), len(y))
            xs.append(z[:n])
            ys.append(y[:n])
    x = np.concatenate(xs)
    y = np.concatenate(ys)
    if len(x) > max_fit:
        idx = rng.choice(len(x), size=max_fit, replace=False)
        x, y = x[idx], y[idx]
    return x, y


def nll_temp(T, x, y):
    T = max(float(T), 1e-3)
    p = expit(x / T)
    p = np.clip(p, 1e-7, 1.0 - 1e-7)
    return float(-(y * np.log(p) + (1.0 - y) * np.log(1.0 - p)).mean())


def fit_temperature(x, y):
    res = minimize_scalar(lambda T: nll_temp(T, x, y), bounds=(0.05, 20.0), method="bounded",
                          options=dict(xatol=1e-4))
    T = float(res.x)
    return dict(T=T, nll=float(res.fun), nll0=nll_temp(1.0, x, y))


def nll_platt(ab, x, y):
    a, b = float(ab[0]), float(ab[1])
    p = expit(a * x + b)
    p = np.clip(p, 1e-7, 1.0 - 1e-7)
    return float(-(y * np.log(p) + (1.0 - y) * np.log(1.0 - p)).mean())


def fit_platt(x, y):
    res = minimize(lambda ab: nll_platt(ab, x, y), x0=np.array([1.0, 0.0]),
                   method="L-BFGS-B", bounds=[(1e-3, 20.0), (-10.0, 10.0)])
    a, b = float(res.x[0]), float(res.x[1])
    return dict(a=a, b=b, nll=float(res.fun), nll0=nll_platt([1.0, 0.0], x, y))


def apply_temp(streams, T):
    out = {}
    for ds, table in streams.items():
        out[ds] = {k: np.asarray(v, np.float64) / T for k, v in table.items()}
    return out


def apply_platt(streams, a, b):
    out = {}
    for ds, table in streams.items():
        out[ds] = {k: a * np.asarray(v, np.float64) + b for k, v in table.items()}
    return out


def apply_temp_one(stream, T):
    return {k: np.asarray(v, np.float64) / T for k, v in stream.items()}


def apply_platt_one(stream, a, b):
    return {k: a * np.asarray(v, np.float64) + b for k, v in stream.items()}


def hp30_stream(stream):
    out = {}
    for key, d in stream.items():
        out[key] = V4.highpass(np.asarray(d, np.float64)[:, None]).reshape(-1).astype(np.float64)
    return out


def hp30_all(streams):
    return {ds: hp30_stream(streams[ds]) for ds in streams}


def score_lodo(streams, held):
    return N.official(held, N.to_ev(streams), P_STAY, SCALE)


def emissions_from(streams, att, datasets=None):
    datasets = list(datasets) if datasets is not None else list(streams)
    parts = []
    for ds in datasets:
        labels = att[ds]
        for key, d in streams[ds].items():
            parts.append(np.asarray(d, np.float64) * np.where(labels[key] == 0, 1.0, -1.0))
    A = np.concatenate(parts)
    return float(A.mean()), float(A.std())


def score_external(stream, meta, mu_e, sd_e):
    ev = {}
    for key, d in stream.items():
        m = meta[key]
        d = np.asarray(d, np.float64)
        ev[key] = dict(r0=d, r1=np.zeros(len(d)), att=m["att"], t_end=m["t_end"],
                       att128=m["att128"], n128=m["n128"], subject=m["subject"])
    return E.score(ev, mu_e, sd_e * SCALE, P_STAY, 0.75)


def load_external():
    E.CONF.update(prep="eaP_n5_t60", targets=["env", "onset", "dom"])
    logits, meta = {}, {}
    for ds in TEST_NAMES:
        evs = []
        for i in range(4):
            path = os.path.join(EVID, f"ext_clf_{ds}_w10b3sw2P_evidence_s{i}.pkl")
            with open(path, "rb") as fh:
                evs.append(pickle.load(fh))
        logits[ds], meta[ds] = {}, {}
        for key in evs[0]:
            cols = [(ev[key]["r0"] - ev[key]["r1"]).astype(np.float32) for ev in evs]
            e0 = evs[0][key]
            logits[ds][key] = np.stack(cols, 1)
            meta[ds][key] = dict(
                att=np.asarray(e0["att"]), t_end=np.asarray(e0["t_end"], np.float64),
                att128=np.asarray(e0["att128"]), n128=int(e0["n128"]), subject=int(e0["subject"]))
        log_line(f"  external {ds}: {len(logits[ds])} trials")
    return logits, meta


def score_variants(streams, held, temp, platt):
    """Score raw / temp / platt and each + hp30 on LODO held-out."""
    rows = {}
    raw = score_lodo(streams, held)
    rows["raw"] = raw
    st = apply_temp(streams, temp["T"])
    rows["temp"] = score_lodo(st, held)
    sp = apply_platt(streams, platt["a"], platt["b"])
    rows["platt"] = score_lodo(sp, held)

    hp = hp30_all(streams)
    rows["raw_hp30"] = score_lodo(hp, held)
    rows["temp_hp30"] = score_lodo(hp30_all(st), held)
    rows["platt_hp30"] = score_lodo(hp30_all(sp), held)
    return rows


def score_ext_variants(stream, meta, corpus, att, temp, platt):
    rows = {}
    variants = {
        "raw": stream,
        "temp": apply_temp_one(stream, temp["T"]),
        "platt": apply_platt_one(stream, platt["a"], platt["b"]),
    }
    corp = {
        "raw": corpus,
        "temp": apply_temp(corpus, temp["T"]),
        "platt": apply_platt(corpus, platt["a"], platt["b"]),
    }
    for name, st in variants.items():
        mu_e, sd_e = emissions_from(corp[name], att)
        rows[name] = score_external(st, meta, mu_e, sd_e)
        st_hp = hp30_stream(st)
        corp_hp = hp30_all(corp[name])
        mu_h, sd_h = emissions_from(corp_hp, att)
        rows[f"{name}_hp30"] = score_external(st_hp, meta, mu_h, sd_h)
    return rows


def run_backbone(tag, ckpt_fn, feat_all, ext_feat, ext_meta, device, att):
    """ckpt_fn(held) -> path; for external held='all5'."""
    log_line(f"=== backbone {tag} ===")
    lodo = {}
    fit_params = {}
    for held in HELD_KEYS:
        path = ckpt_fn(held)
        log_line(f"{tag} LODO held-out={held} ckpt {path}")
        model, mu, sd = load_stem(path, device)
        t0 = time.time()
        streams = V.infer_all(model, feat_all, mu, sd, WINDOW, device)
        train_ds = [d for d in DS_KEYS if d != held]
        x, y = collect_xy(streams, att, train_ds)
        temp = fit_temperature(x, y)
        platt = fit_platt(x, y)
        log_line(f"  fit n={len(x)} temp T={temp['T']:.4f} nll {temp['nll']:.4f} (raw {temp['nll0']:.4f}) "
                 f"platt a={platt['a']:.4f} b={platt['b']:.4f} nll {platt['nll']:.4f}")
        rows = score_variants(streams, held, temp, platt)
        for k, r in rows.items():
            log_line(f"  LODO_{k} {held}  H {r['H']:.4f} Acc {r['Acc']:.2f}")
        lodo[held] = rows
        fit_params[held] = dict(temp=temp, platt=platt)
        del model, streams
        if device.type == "cuda":
            torch.cuda.empty_cache()
        log_line(f"  ({time.time() - t0:.0f}s)")

    means = {}
    for k in ("raw", "temp", "platt", "raw_hp30", "temp_hp30", "platt_hp30"):
        means[k] = dict(
            H=float(np.mean([lodo[d][k]["H"] for d in HELD_KEYS])),
            Acc=float(np.mean([lodo[d][k]["Acc"] for d in HELD_KEYS])),
        )
        log_line(f"{tag} LODO_mean_{k} H {means[k]['H']:.4f} Acc {means[k]['Acc']:.2f}")

    # External: fit calib on all five corpus using all5 (SWA) or pooled train-only
    # stem: no published all5; pool logits from each held-* on its train sets only.
    if tag == "swa":
        path = ckpt_fn("all5")
        model, mu, sd = load_stem(path, device)
        corpus = V.infer_all(model, feat_all, mu, sd, WINDOW, device)
        x, y = collect_xy(corpus, att, list(DS_KEYS))
        temp = fit_temperature(x, y)
        platt = fit_platt(x, y)
        log_line(f"{tag} external fit n={len(x)} T={temp['T']:.4f} a={platt['a']:.4f} b={platt['b']:.4f}")
        external = {}
        for ds in TEST_NAMES:
            st = V.infer_all(model, {ds: ext_feat[ds]}, mu, sd, WINDOW, device)[ds]
            external[ds] = score_ext_variants(st, ext_meta[ds], corpus, att, temp, platt)
            for k, r in external[ds].items():
                log_line(f"{tag} EXT_{k} {ds}  H {r['H']:.4f} Acc {r['Acc']:.2f}")
        del model
        if device.type == "cuda":
            torch.cuda.empty_cache()
        fit_params["external"] = dict(temp=temp, platt=platt)
    else:
        # stem: pool train-fold logits across five held-* models (no held-out leak)
        xs, ys = [], []
        corpus_pool = {d: {} for d in DS_KEYS}
        counts = {d: {} for d in DS_KEYS}
        for held in DS_KEYS:
            path = ckpt_fn(held)
            model, mu, sd = load_stem(path, device)
            streams = V.infer_all(model, feat_all, mu, sd, WINDOW, device)
            train_ds = [d for d in DS_KEYS if d != held]
            x, y = collect_xy(streams, att, train_ds, max_fit=MAX_FIT // 5 + 1)
            xs.append(x)
            ys.append(y)
            for ds in train_ds:
                for key, v in streams[ds].items():
                    if key not in corpus_pool[ds]:
                        corpus_pool[ds][key] = np.asarray(v, np.float64).copy()
                        counts[ds][key] = 1
                    else:
                        corpus_pool[ds][key] += np.asarray(v, np.float64)
                        counts[ds][key] += 1
            del model, streams
            if device.type == "cuda":
                torch.cuda.empty_cache()
        for ds in DS_KEYS:
            for key in corpus_pool[ds]:
                corpus_pool[ds][key] /= counts[ds][key]
        x = np.concatenate(xs)
        y = np.concatenate(ys)
        if len(x) > MAX_FIT:
            rng = np.random.default_rng(SEED)
            idx = rng.choice(len(x), size=MAX_FIT, replace=False)
            x, y = x[idx], y[idx]
        temp = fit_temperature(x, y)
        platt = fit_platt(x, y)
        log_line(f"{tag} external fit (pooled train folds) n={len(x)} "
                 f"T={temp['T']:.4f} a={platt['a']:.4f} b={platt['b']:.4f}")
        # Infer external with each held-* and average (equal ensemble of LODO models)
        ext_streams = {ds: None for ds in TEST_NAMES}
        for held in DS_KEYS:
            path = ckpt_fn(held)
            model, mu, sd = load_stem(path, device)
            for ds in TEST_NAMES:
                st = V.infer_all(model, {ds: ext_feat[ds]}, mu, sd, WINDOW, device)[ds]
                if ext_streams[ds] is None:
                    ext_streams[ds] = {k: np.asarray(v, np.float64) for k, v in st.items()}
                else:
                    for k, v in st.items():
                        ext_streams[ds][k] += np.asarray(v, np.float64)
            del model
            if device.type == "cuda":
                torch.cuda.empty_cache()
        for ds in TEST_NAMES:
            for k in ext_streams[ds]:
                ext_streams[ds][k] /= len(DS_KEYS)
        external = {}
        for ds in TEST_NAMES:
            external[ds] = score_ext_variants(
                ext_streams[ds], ext_meta[ds], corpus_pool, att, temp, platt)
            for k, r in external[ds].items():
                log_line(f"{tag} EXT_{k} {ds}  H {r['H']:.4f} Acc {r['Acc']:.2f}")
        fit_params["external"] = dict(temp=temp, platt=platt)

    return dict(lodo=lodo, lodo_mean=means, external=external, fit=fit_params)


def main():
    os.makedirs(os.path.dirname(LOG), exist_ok=True)
    os.makedirs(CKPT_DIR, exist_ok=True)
    open(LOG, "w").close()
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    log_line(f"device {device} p_stay {P_STAY} scale {SCALE} max_fit {MAX_FIT} "
             f"held {HELD_KEYS} do_swa {DO_SWA}")
    torch.backends.cuda.matmul.allow_tf32 = True
    torch.backends.cudnn.allow_tf32 = True
    V.assert_causal(device)
    N.load_all()
    att = N.G["att"]
    feat_all = V.build_features()["ms"]
    ext_logits, ext_meta = load_external()
    ext_feat = V4.build_features_from(ext_logits)["ms"]

    out = dict(
        method="fold-safe temperature + Platt calibration on frozen stem_ms96 (+ SWA) logits",
        p_stay=P_STAY, scale=SCALE, max_fit=MAX_FIT,
    )

    def stem_ckpt(held):
        return os.path.join(STEM_DIR, f"stem_ms96__held-{held}.pt")

    out["stem"] = run_backbone("stem", stem_ckpt, feat_all, ext_feat, ext_meta, device, att)

    if DO_SWA:
        def swa_ckpt(held):
            if held == "all5":
                return os.path.join(SWA_DIR, "all5.pt")
            return os.path.join(SWA_DIR, f"held-{held}.pt")

        out["swa"] = run_backbone("swa", swa_ckpt, feat_all, ext_feat, ext_meta, device, att)

    # persist fit params as lightweight "checkpoints"
    with open(os.path.join(CKPT_DIR, "fit_params.json"), "w") as fh:
        json.dump({k: out[k]["fit"] for k in out if k in ("stem", "swa")}, fh, indent=1, default=float)

    with open(OUT, "w") as fh:
        json.dump(out, fh, indent=1, default=float)
    log_line(f"wrote {OUT}")


if __name__ == "__main__":
    main()
