"""Train ensemble_clf_transformer_v2 on data/cnd + Broderick2018 + Fuglsang2020
and score the held-out real-switch sets AASD and Carta2026.

The four recipes and the training step count are the script defaults (STEPS,
default 800; BATCH, default 512). Nested leave-one-dataset-out is not repeated:
the requested test sets are AASD and Carta2026, so each recipe is fit once on
the pooled training tables. HMM p_stay / scale are reported on the script grid.
The headline point is the pre-specified pair used by the original acc74
selection for this architecture (p_stay 0.994, scale 0.8), chosen without
looking at AASD or Carta2026.

usage (from this directory):
  python ensemble_clf_transformer_v2.py --mode external
  python ensemble_clf_transformer_v2.py --mode external --stage test
"""
import json
import os
import time

import numpy as np
import pandas as pd
import torch

import ensemble_clf_transformer_v2 as V
import eval_external as E
import external_logit_adapter as A

HERE = os.path.dirname(os.path.abspath(__file__))
CKPT = os.path.join(HERE, "models", "logit_tf_v2_external")
OUT = os.path.join(HERE, "results", "research", "ensemble_clf_transformer_v2_external.csv")
EMIS_PATH = os.path.join(CKPT, "emissions.json")
PRESET = (0.994, 0.8)
LOOKAHEAD = 0.75


def emissions(streams, att):
    parts = []
    for ds, table in streams.items():
        labels = att[ds]
        for key, d in table.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 pack_ev(stream, meta):
    ev = {}
    for key, d in stream.items():
        m = meta[key]
        d = np.asarray(d, np.float64)
        if len(d) != len(m["t_end"]):
            raise RuntimeError(f"{key}: prediction {len(d)} != t_end {len(m['t_end'])}")
        ev[key] = dict(r0=d, r1=np.zeros(len(d), np.float64), att=m["att"], t_end=m["t_end"],
                       att128=m["att128"], n128=m["n128"], subject=m["subject"])
    return ev


def score_grid(name, stream, meta, mu, sd):
    rows = []
    ev = pack_ev(stream, meta)
    plain = E.plain(ev)
    for p in V.P_STAY:
        for scale in V.SCALES:
            t0 = time.time()
            r = E.score(ev, mu, sd * float(scale), float(p), LOOKAHEAD)
            rec = dict(model=name, p_stay=float(p), scale=float(scale), preset=bool((p, scale) == PRESET),
                       acc1=plain["acc1"], snr1=plain["snr1"], seconds=time.time() - t0, **r)
            rows.append(rec)
    return rows


def load_features():
    print("loading logit tables", flush=True)
    train_logits, train_att = A.load_train()
    test_logits, test_meta = A.load_test()
    base = {}
    base.update(train_logits)
    base.update(test_logits)
    print("building causal features", flush=True)
    feat = V.build_features_from(base)
    return train_att, test_meta, feat


def fit_all(device, train_att, test_meta, feat):
    os.makedirs(CKPT, exist_ok=True)
    cfgs = V.wanted_configs()
    train_names = list(A.TRAIN_DATASETS)
    saved = {}
    train_streams = {}
    for cfg in cfgs:
        name, family, window = cfg[0], cfg[1], cfg[2]
        model, mu, sd = V.train_model(cfg, train_names, feat, device, V.STEPS, att=train_att)
        path = os.path.join(CKPT, f"{name}.pt")
        train_feat = {ds: feat[family][ds] for ds in train_names}
        streams = V.infer_all(model, train_feat, mu, sd, window, device)
        mu_e, sd_e = emissions(streams, train_att)
        torch.save({"state": model.state_dict(), "mu": mu, "sd": sd, "cfg": cfg,
                    "train": train_names, "emis_mu": mu_e, "emis_sd": sd_e,
                    "steps": V.STEPS, "batch": V.BATCH}, path)
        print(f"  saved {path}  emis mu {mu_e:.4f} sd {sd_e:.4f}", flush=True)
        saved[name] = dict(cfg=cfg, mu=mu, sd=sd, emis_mu=mu_e, emis_sd=sd_e, path=path)
        train_streams[name] = streams
        del model
        if device.type == "cuda":
            torch.cuda.empty_cache()
    if len(train_streams) >= 2:
        avg = V.average_streams([train_streams[n] for n in train_streams])
        mu_e, sd_e = emissions(avg, train_att)
        saved["ens_all"] = dict(emis_mu=mu_e, emis_sd=sd_e, members=list(train_streams))
        print(f"  ens_all emis mu {mu_e:.4f} sd {sd_e:.4f}", flush=True)
    emis = {name: {"mu": rec["emis_mu"], "sd": rec["emis_sd"]} for name, rec in saved.items()}
    with open(EMIS_PATH, "w") as fh:
        json.dump(emis, fh, indent=1)
    return saved


def _load_model(path, device):
    ckpt = torch.load(path, map_location="cpu", weights_only=False)
    cfg = tuple(ckpt["cfg"])
    c_in = 8 if cfg[1] == "hp" else 24
    model = V.LogitTransformer(c_in, cfg[2], d_model=cfg[3], nlayers=cfg[4], stem=cfg[5]).to(device)
    model.load_state_dict(ckpt["state"])
    model.eval()
    ckpt["model"] = model
    ckpt["cfg"] = cfg
    return ckpt


def evaluate(device, test_meta, feat, saved=None):
    os.makedirs(os.path.dirname(OUT), exist_ok=True)
    if saved is None:
        with open(EMIS_PATH) as fh:
            emis = json.load(fh)
        saved = {}
        for name in [c[0] for c in V.wanted_configs()]:
            ckpt = _load_model(os.path.join(CKPT, f"{name}.pt"), device)
            saved[name] = dict(cfg=ckpt["cfg"], mu=ckpt["mu"], sd=ckpt["sd"],
                               emis_mu=emis[name]["mu"], emis_sd=emis[name]["sd"], model=ckpt["model"])
        if "ens_all" in emis:
            saved["ens_all"] = dict(emis_mu=emis["ens_all"]["mu"], emis_sd=emis["ens_all"]["sd"],
                                    members=[c[0] for c in V.wanted_configs()])
    rows = []
    test_streams = {}
    single = [n for n in saved if n != "ens_all"]
    for name in single:
        rec = saved[name]
        cfg = rec["cfg"]
        family, window = cfg[1], cfg[2]
        if "model" in rec:
            model = rec["model"]
        else:
            model = _load_model(rec["path"], device)["model"]
        table = {ds: feat[family][ds] for ds in A.TEST_DATASETS}
        test_streams[name] = V.infer_all(model, table, rec["mu"], rec["sd"], window, device)
        del model
        if device.type == "cuda":
            torch.cuda.empty_cache()
        for ds in A.TEST_DATASETS:
            for row in score_grid(name, test_streams[name][ds], test_meta[ds], rec["emis_mu"], rec["emis_sd"]):
                row["ds"] = ds
                rows.append(row)
                if row["preset"]:
                    print(f"TEST {ds} {name} p {row['p_stay']} sc {row['scale']}  "
                          f"H {row['H']:.4f} Acc {row['Acc']:.2f} SDT {row['SDT']:.2f} L {row['L']:.3f} "
                          f"acc1 {row['acc1']:.3f}", flush=True)
    if "ens_all" in saved and len(test_streams) >= 2:
        members = saved["ens_all"]["members"]
        avg = V.average_streams([test_streams[n] for n in members])
        mu_e, sd_e = saved["ens_all"]["emis_mu"], saved["ens_all"]["emis_sd"]
        for ds in A.TEST_DATASETS:
            for row in score_grid("ens_all", avg[ds], test_meta[ds], mu_e, sd_e):
                row["ds"] = ds
                rows.append(row)
                if row["preset"]:
                    print(f"TEST {ds} ens_all p {row['p_stay']} sc {row['scale']}  "
                          f"H {row['H']:.4f} Acc {row['Acc']:.2f} SDT {row['SDT']:.2f} L {row['L']:.3f} "
                          f"acc1 {row['acc1']:.3f}", flush=True)
    df = pd.DataFrame(rows)
    df.to_csv(OUT, index=False)
    print(f"wrote {OUT}", flush=True)
    preset = df[df.preset]
    print("preset means (p_stay 0.994, scale 0.8):", flush=True)
    for model, sub in preset.groupby("model"):
        m = sub[["H", "Acc", "SDT", "L", "acc1"]].mean()
        print(f"  {model:16s}  H {m.H:.4f}  Acc {m.Acc:.2f}  SDT {m.SDT:.2f}  L {m.L:.3f}  acc1 {m.acc1:.3f}",
              flush=True)
    return df


def run_probe(device):
    print(f"probe device {device}", flush=True)
    built = A.probe()
    print("probe front-end done", flush=True)
    if device.type == "cuda":
        torch.cuda.empty_cache()
    logits = {name: blob["logits"] for name, blob in built.items()}
    feat = V.build_features_from(logits)
    cfg = V.CONFIGS[0]
    t0 = time.time()
    V.train_model(cfg, list(logits), feat, device, steps=30)
    print(f"probe 30 steps of {cfg[0]} on {len(logits)} trials: {time.time() - t0:.1f}s", flush=True)


def run(stage):
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    torch.backends.cuda.matmul.allow_tf32 = True
    torch.backends.cudnn.allow_tf32 = True
    torch.set_num_threads(4)
    print(f"device {device}  steps {V.STEPS}  batch {V.BATCH}  stage {stage}", flush=True)
    print("train sets: data/cnd (AVGC DTU KUL UHD USTC) + Broderick2018 + Fuglsang2020", flush=True)
    print("test sets: AASD + Carta2026", flush=True)
    print("synthetic switches: corpus mean 60 s; Broderick and Fuglsang mean 20 s "
          "(their trials are ~50-60 s, so the 60 s schedule would not switch)", flush=True)
    if stage == "probe":
        run_probe(device)
        return
    if stage in ("all", "logits", "train"):
        print("building training logit cache", flush=True)
        A.ensure_train()
    if stage == "logits":
        print("logit cache ready", flush=True)
        return
    train_att, test_meta, feat = load_features()
    saved = None
    if stage in ("all", "train"):
        V.assert_causal(device)
        saved = fit_all(device, train_att, test_meta, feat)
    if stage in ("all", "test"):
        evaluate(device, test_meta, feat, saved)
