"""Sample-level Markov switching model (MSM) for switch detection, after Yao, Geirnaert, Tuytelaars & Bertrand
(arXiv 2602.13447, 2026), adapted to the challenge constraints. Runs on the GPU, causal, on the saved causal
reconstructions of an experiment run with --save-recon.

Model (32 Hz, per trial, state s_t in {0, 1} = attended envelope column):
    y_t = beta * e_{s_t}(t) + gamma * e_{1-s_t}(t) + c + r_t,     r_t = phi * r_{t-1} + eps_t,   eps ~ N(0, sigma^2)
  y = decoder reconstruction, normalised by a causal running SD (EW, 30 s); e = the two envelopes (as in the prep).
  (beta, gamma, c, phi, sigma) are fitted by least squares on the OTHER four datasets (true labels, no test data).
  Inference: Hamilton filter over (s_{t-1}, s_t) (exact for the AR(1) residual), log-likelihood scaled by `temp`
  (the residual is still correlated beyond lag 1); switch probability per sample from p_stay per 0.25 s.
  Decision at sample u uses EEG up to u + 0.75 s (the decoder lookahead) -> stamped from u - 0.25 s (1-s budget),
  exactly like the window pipeline. Synthetic switches: identical schedule to score.py (same rng order); a switch
  swaps the roles of the two envelope columns, as the window pipeline flips the sign of d.
Config selection nested (best mean H on the other 4 datasets). Compared with the window HMM of the same run.

usage: python msm.py EXP [EXP ...]      ->  results/msm_<exp>.csv, results/msm_<exp>.json, logs/msm.log
"""
import itertools
import json
import os
import pickle
import sys
import time

import numpy as np
import pandas as pd
import torch
from joblib import Parallel, delayed

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from common_exp import EXP, LAB, fold_file, load_trial, rows_of  # noqa: E402
from common import CACHE as EDA_CACHE, DS_KEYS, FS as FS128, SEED  # noqa: E402
from e1_switch import synth_trial  # noqa: E402
from integrators import expand  # noqa: E402
from scorer import aggregate, fast_score_subject  # noqa: E402

DEV = torch.device("cuda" if torch.cuda.is_available() else "cpu")
FS = 32
LOG = open(os.path.join(EXP, "logs", "msm.log"), "a", buffering=1)
GRID = [dict(p_stay=p, temp=t) for p, t in itertools.product((0.995, 0.997, 0.998, 0.999, 0.9995),
                                                              (0.02, 0.05, 0.1, 0.2, 0.5, 1.0))]


def log(m):
    line = f"{time.strftime('%F %T')} {m}"
    print(line, flush=True)
    LOG.write(line + "\n")


def running_sd_norm(y, tau_s=30.0):
    a = 1.0 / (tau_s * FS)
    m, v = y[:FS].mean(), y[:FS].var() + 1e-12
    out = np.empty_like(y)
    for t in range(len(y)):                       # statistics from samples < t (first 1 s initialises)
        out[t] = (y[t] - m) / np.sqrt(v)
        m += a * (y[t] - m)
        v += a * ((y[t] - m) ** 2 - v)
    return out


def load(exp, prep):
    data = {}
    for ds in DS_KEYS:
        f = pickle.load(open(fold_file(exp, ds), "rb"))
        rng = np.random.default_rng(SEED + DS_KEYS.index(ds))
        subj = {}
        for _, r in rows_of(ds).iterrows():
            T = int(r["n"])
            tr = load_trial(prep, r["key"])
            ev = f["evidence"]["causal"][r["key"]]
            st, _, _ = synth_trial(r["key"], T, (ev["r0"] - ev["r1"]).astype(float), ev["t_end"].astype(float), rng)
            guess = int(rng.integers(0, 2))
            att128 = np.load(os.path.join(EDA_CACHE, "cnd", f"{r['key']}.npz"))["att_col"][:T].astype(int)
            y = running_sd_norm(f["recon"][r["key"]].astype(np.float64))
            e = tr["env"].astype(np.float64)
            n = min(len(y), len(e))
            y, e, att = y[:n], e[:n], tr["att"][:n].astype(int)
            i128 = np.minimum(np.round((tr["t0"] + np.arange(n) / FS) * FS128).astype(int), T - 1)
            sw = st[i128]                                                   # synthetic swap at 32 Hz
            e_syn = np.where(sw[:, None], e[:, ::-1], e)
            subj.setdefault(r["subj_uid"], []).append(dict(
                y=y, e=e, att=att, e_syn=e_syn, t_end=tr["t0"] + (np.arange(n) + 1) / FS, T=T, guess=guess,
                gt=np.where(st, 1 - att128, att128)))
        data[ds] = subj
    return data


def fit(data, dss):
    """Least squares on true labels: y ~ e_att, e_un, 1; AR(1) on the residual."""
    X, Y, R0, R1 = [], [], [], []
    for ds in dss:
        for trs in data[ds].values():
            for t in trs:
                ea = np.take_along_axis(t["e"], t["att"][:, None], 1)[:, 0]
                eu = np.take_along_axis(t["e"], 1 - t["att"][:, None], 1)[:, 0]
                X.append(np.c_[ea, eu, np.ones_like(ea)])
                Y.append(t["y"])
    X, Y = np.concatenate(X), np.concatenate(Y)
    b = np.linalg.lstsq(X, Y, rcond=None)[0]
    # residual AR(1) per trial (no cross-trial pairs)
    num = den = 0.0
    res_all = []
    k = 0
    for ds in dss:
        for trs in data[ds].values():
            for t in trs:
                n = len(t["y"])
                r = Y[k:k + n] - X[k:k + n] @ b
                k += n
                num += float(r[1:] @ r[:-1])
                den += float(r[:-1] @ r[:-1])
                res_all.append(r)
    phi = num / den
    eps = np.concatenate([r[1:] - phi * r[:-1] for r in res_all])
    return dict(beta=b[0], gamma=b[1], c=b[2], phi=phi, sigma=float(eps.std()))


@torch.no_grad()
def run_gpu(subj, par, grid):
    subs = list(subj)
    cat = lambda s, k: np.concatenate([t[k] for t in subj[s]])
    lens = [sum(len(t["y"]) for t in subj[s]) for s in subs]
    Sn, Tm, K = len(subs), max(lens), len(grid)
    z = lambda: torch.zeros(Sn, Tm, device=DEV, dtype=torch.float64)
    Y, E0, E1 = z(), z(), z()
    M = torch.zeros(Sn, Tm, device=DEV, dtype=torch.bool)
    B = torch.zeros(Sn, Tm, device=DEV, dtype=torch.bool)
    starts = []
    for i, s in enumerate(subs):
        n = lens[i]
        Y[i, :n] = torch.from_numpy(cat(s, "y"))
        es = cat(s, "e_syn")
        E0[i, :n], E1[i, :n] = torch.from_numpy(es[:, 0]), torch.from_numpy(es[:, 1])
        M[i, :n] = True
        st = np.cumsum([0] + [len(t["y"]) for t in subj[s]][:-1])
        B[i, torch.from_numpy(st.astype(np.int64))] = True
        starts.append(st)
    ps = torch.tensor([g["p_stay"] for g in grid], device=DEV, dtype=torch.float64)[:, None]
    q = (1 - ps) / 8.0                                                   # per 32-Hz sample (8 per 0.25 s)
    lA_stay, lA_sw = torch.log1p(-q), torch.log(q)
    temp = torch.tensor([g["temp"] for g in grid], device=DEV, dtype=torch.float64)[:, None]
    be, ga, c, phi, sg = (par[k] for k in ("beta", "gamma", "c", "phi", "sigma"))
    la = torch.full((K, Sn, 2), np.log(0.5), device=DEV, dtype=torch.float64)
    rprev = torch.zeros(Sn, 2, device=DEV, dtype=torch.float64)          # residual at t-1 under s_{t-1}
    out = torch.zeros(K, Sn, Tm, device=DEV, dtype=torch.bool)
    for t in range(Tm):
        y, e0, e1, m, b = Y[:, t], E0[:, t], E1[:, t], M[:, t], B[:, t]
        mean = torch.stack([be * e0 + ga * e1 + c, be * e1 + ga * e0 + c], -1)        # (S, 2) by s_t
        rp = torch.where(b[:, None], torch.zeros_like(rprev), rprev)
        pred = mean[:, None, :] + phi * rp[:, :, None]                                  # (S, s_prev, s_t)
        ll = -0.5 * ((y[:, None, None] - pred) / sg) ** 2                                # (S, 2, 2)
        lap = torch.where(b[None, :, None], torch.full_like(la, np.log(0.5)), la)       # trial start: reset
        trans = torch.stack([torch.stack([lA_stay, lA_sw], -1), torch.stack([lA_sw, lA_stay], -1)], -2)  # (K,1,2,2)
        joint = lap[..., :, None] + trans + temp[..., None, None] * ll[None]            # (K, S, s_prev, s_t)
        new = torch.logsumexp(joint, 2)
        new = new - torch.logsumexp(new, -1, keepdim=True)
        la = torch.where(m[None, :, None], new, la)
        out[:, :, t] = la[..., 1] > la[..., 0]
        rprev = torch.where(m[:, None], y[:, None] - mean, rprev)
    out = out.cpu().numpy()
    return {s: [out[:, i, st:st + len(tr["y"])] for st, tr in zip(starts[i], subj[s])] for i, s in enumerate(subs)}


def score_config(k, dec, subj, L):
    res = []
    for s, trs in subj.items():
        pred = np.concatenate([expand(dec[s][j][k].astype(float), tr["t_end"], L, tr["T"], tr["guess"])
                               for j, tr in enumerate(trs)])
        gt = np.concatenate([tr["gt"] for tr in trs])
        trial = np.concatenate([np.full(tr["T"], j) for j, tr in enumerate(trs)])
        res.append(fast_score_subject(pred, gt, trial, fs=FS128))
    a = aggregate(res)
    return dict(Acc=a["Acc"], SDT=a["SDT"], L=a["L"], H=a["H"])


def main(exp):
    js = json.load(open(os.path.join(EXP, "results", f"{exp}.json")))
    L = js["causal"]["info"]["AVGC"]["lookahead_s"]
    log(f"=== MSM {exp}: {len(GRID)} configs, lookahead {L} s, device {DEV}")
    t0 = time.time()
    data = load(exp, js["prep"])
    log(f"  loaded in {time.time() - t0:.0f}s")
    rows, pars = [], {}
    for ds in DS_KEYS:
        par = fit(data, [d for d in DS_KEYS if d != ds])
        pars[ds] = par
        t1 = time.time()
        dec = run_gpu(data[ds], par, GRID)
        t2 = time.time()
        sc = Parallel(n_jobs=int(os.environ.get("SCORE_JOBS", 4)))(delayed(score_config)(k, dec, data[ds], L) for k in range(len(GRID)))
        rows += [dict(ds=ds, k=k, **GRID[k], **r) for k, r in enumerate(sc)]
        log(f"  {LAB[ds]}: params " + " ".join(f"{a}={b:.3f}" for a, b in par.items())
            + f" | GPU {t2 - t1:.0f}s scoring {time.time() - t2:.0f}s")
    R = pd.DataFrame(rows)
    R.to_csv(os.path.join(EXP, "results", "research", f"msm_{exp}.csv"), index=False)
    piv = R.pivot_table(index="k", columns="ds", values="H")
    sel = {}
    for ds in DS_KEYS:
        k = int(piv[[d for d in DS_KEYS if d != ds]].mean(1).idxmax())
        r = R[(R.ds == ds) & (R.k == k)].iloc[0]
        sel[ds] = dict(cfg=GRID[k], Acc=r.Acc, SDT=r.SDT, L=r.L, H=r.H)
    mean = {m: float(np.mean([sel[d][m] for d in DS_KEYS])) for m in ("Acc", "SDT", "L", "H")}
    win = js["causal"]["mean"]
    json.dump(dict(experiment=exp, window_hmm_mean={m: win[m] for m in ("Acc", "SDT", "L", "H")}, msm_nested_mean=mean,
                   per_dataset=sel, params=pars), open(os.path.join(EXP, "results", "research", f"msm_{exp}.json"), "w"),
              indent=1, default=float)
    log("  window HMM : " + "  ".join(f"{m} {win[m]:.3f}" for m in ("Acc", "SDT", "L", "H")))
    log("  MSM nested : " + "  ".join(f"{m} {mean[m]:.3f}" for m in mean))
    log(f"=== done MSM {exp} in {time.time() - t0:.0f}s")


if __name__ == "__main__":
    for e in sys.argv[1:]:
        main(e)
