"""Phase E: switch detection and the accuracy-latency trade-off on LODO evidence.

Evidence: 1-s windows at 0.25-s hop from the LODO decoder (cache/evidence/<cfg>/<scheme>/<lag>), i.e.
the decoder never saw the held-out dataset. d = r(col0) - r(col1).

Label timing (the 1-s lookahead budget): a window ending at t_end used EEG up to t_end + L (decoder lag).
Its decision labels samples tau >= t_end + L - budget, budget = 1 s. Itemised per configuration in
tables/switch_lookahead_budget.csv. Samples with no decision yet (trial start) get one seeded random guess
per trial (as in the MATLAB baseline), never an informed default.

Switch sets
  SYNTHETIC  generateSyntheticSwitches.m schedule (gaps U(30, 90) s; minSeg = min(30, T/3)); the per-sample
             label AND the two evidence columns are swapped together (window swapped by majority), so the
             plain decoder's accuracy is unchanged by construction (asserted).
  GENUINE    KUL-AV-GC direction switch at 300 s (the only genuine within-trial switches). The speaker decoder
             is mapped to a direction via the per-stream azimuth (ASSUMES stream positions are known at test
             time); spatial-decoder evidence for the same switches is scored in d3.
Integrators (causal): argmax; moving average (1-40 s); 2-state HMM (p_stay grid, Gaussian emissions fitted on
the 4 training datasets' own out-of-fold evidence); HMM on running-SD-normalised evidence with a temperature;
N-state MESD-style ladder (N = 3..9).
Null: every configuration also on sign-randomised evidence (|d| x iid random sign per window).
"""
import os
import sys
import zlib

import matplotlib

matplotlib.use("Agg")
import matplotlib.pyplot as plt  # noqa: E402
import numpy as np  # noqa: E402
import pandas as pd  # noqa: E402
from joblib import Parallel, delayed  # noqa: E402

sys.path.insert(0, os.path.dirname(__file__))
from common import CACHE, CFG, COLORS, DS_KEYS, FS, LABELS, SEED, TAB, boot_ci, get_logger, savefig, trial_index  # noqa: E402
from scorer import aggregate, fast_score_subject  # noqa: E402

S = CFG["switch"]
HOP = CFG["decoder"]["evidence_hop_s"]
BUDGET = 1.0
log = get_logger("e1_switch")


# ---------------------------------------------------------------------------
# synthetic schedule (generateSyntheticSwitches.m: drawSwitchTimes)
# ---------------------------------------------------------------------------
def draw_switch_times(rng, trial_sec, mean_int=60.0, min_seg=30.0):
    min_seg = min(min_seg, trial_sec / 3)
    mean_int = max(mean_int, min_seg)
    hi = 2 * mean_int - min_seg
    times, t = [], 0.0
    while True:
        t = t + min_seg + rng.random() * (hi - min_seg)
        if t > trial_sec - min_seg:
            break
        times.append(t)
    return np.array(times)


# ---------------------------------------------------------------------------
# integrators: input d (n,) evidence at 4 Hz -> decision (n,) in {0 (col0), 1 (col1)}
# ---------------------------------------------------------------------------
def integ_argmax(d, p):
    return (d < 0).astype(int)


def integ_ma(d, p):
    k = max(1, int(round(p["mem_s"] / HOP)))
    c = np.cumsum(np.r_[0, d])
    i = np.arange(1, len(d) + 1)
    m = (c[i] - c[np.maximum(i - k, 0)])
    return (m < 0).astype(int)


def hmm_filter(ll0, ll1, p_stay):
    """Forward filter in log domain; ll* = log-likelihood per step. Returns P(col1)."""
    n = len(ll0)
    out = np.empty(n)
    lp = np.log(np.array([0.5, 0.5]))
    ls, lsw = np.log(p_stay), np.log(1 - p_stay)
    for t in range(n):
        if t > 0:
            a = np.logaddexp(lp[0] + ls, lp[1] + lsw)
            b = np.logaddexp(lp[1] + ls, lp[0] + lsw)
            lp = np.array([a, b])
        lp = lp + np.array([ll0[t], ll1[t]])
        lp = lp - np.logaddexp(lp[0], lp[1])
        out[t] = np.exp(lp[1])
    return out


def integ_hmm(d, p):
    mu, sd = p["mu"], p["sd"]
    ll0 = -0.5 * ((d - mu) / sd) ** 2
    ll1 = -0.5 * ((d + mu) / sd) ** 2
    return (hmm_filter(ll0, ll1, p["p_stay"]) > 0.5).astype(int)


def running_sd(d, tau_steps):
    a = 1.0 / tau_steps
    v = np.empty(len(d))
    m2 = np.mean(d[: min(len(d), 4)] ** 2) if len(d) else 1.0
    for t in range(len(d)):
        v[t] = np.sqrt(max(m2, 1e-12))             # uses samples < t only
        m2 = m2 + a * (d[t] ** 2 - m2)
    return v


def integ_hmm_z(d, p):
    z = d / running_sd(d, 60.0 / HOP)
    mu = p["mu_z"]
    T_ = p["temp"]
    ll0 = -0.5 * (z - mu) ** 2 * T_
    ll1 = -0.5 * (z + mu) ** 2 * T_
    return (hmm_filter(ll0, ll1, p["p_stay"]) > 0.5).astype(int)


def integ_ladder(d, p):
    N = p["N"]
    top = (N - 1) // 2
    s, prev = 0, 0
    out = np.empty(len(d), int)
    for t, x in enumerate(d):
        s = min(top, s + 1) if x > 0 else max(-top, s - 1)
        prev = 0 if s > 0 else (1 if s < 0 else prev)
        out[t] = prev
    return out


def configs(emis):
    cf = [("argmax", integ_argmax, {})]
    cf += [(f"ma_{m}s", integ_ma, dict(mem_s=m)) for m in S["ma_memory_s"]]
    cf += [(f"hmm_p{ps}", integ_hmm, dict(p_stay=ps, mu=emis["mu"], sd=emis["sd"])) for ps in S["hmm_pstay"]]
    cf += [(f"hmmz_p{ps}_T{T_}", integ_hmm_z, dict(p_stay=ps, mu_z=emis["mu_z"], temp=T_))
           for ps in S["hmm_pstay"] for T_ in S["hmm_temps"]]
    cf += [(f"ladder_N{N}", integ_ladder, dict(N=N)) for N in S["mesd_states"]]
    return cf


# ---------------------------------------------------------------------------
def load_trial(cfg, scheme, lag, key, T128):
    z = np.load(os.path.join(CACHE, "evidence", cfg, scheme, lag, f"{key}.npz"))
    return dict(d=(z["r0"] - z["r1"]).astype(float), t_end=z["t_end"].astype(float), L=float(z["L_s"]), T=T128)


def expand(dec, t_end, L, T, rng_guess):
    """Window decisions -> per-sample labels at 128 Hz using the lookahead budget."""
    lab = np.full(T, rng_guess, float)
    t_lab = t_end + L - BUDGET                        # earliest sample time each decision may label
    s0 = np.clip(np.ceil(t_lab * FS).astype(int), 0, T)
    for j in range(len(dec)):
        e = s0[j + 1] if j + 1 < len(dec) else T
        lab[s0[j]:e] = dec[j]
    return lab


def synth_trial(key, T, d, t_end, rng):
    times = draw_switch_times(rng, T / FS)
    st = np.zeros(T, bool)
    cur, prev = False, 0
    for tt in times:
        s0 = min(T - 1, int(round(tt * FS)))
        st[prev:s0] = cur
        cur, prev = not cur, s0
    st[prev:] = cur
    # evidence windows: [t_end - 1, t_end) -> swapped if majority of its samples are swapped
    c = np.r_[0, np.cumsum(st)]
    a = np.clip(np.round((t_end - 1.0) * FS).astype(int), 0, T)
    b = np.clip(np.round(t_end * FS).astype(int), 0, T)
    wsw = (c[b] - c[a]) > (b - a) / 2
    return st, np.where(wsw, -d, d), wsw


def build_sets(cfg, scheme, lag, ds, seed):
    ti = trial_index()
    rows = ti[ti.ds == ds]
    out = []
    rng = np.random.default_rng(seed)
    for _, r in rows.iterrows():
        lab = np.load(os.path.join(CACHE, "cnd", f"{r['key']}.npz"))
        T = int(r["n"])
        ev = load_trial(cfg, scheme, lag, r["key"], T)
        att = lab["att_col"][:T].astype(int)
        st, d_syn, wsw = synth_trial(r["key"], T, ev["d"], ev["t_end"], rng)
        az = lab["att_az"][:T]
        out.append(dict(key=r["key"], subj=r["subj_uid"], T=T, d=ev["d"], t_end=ev["t_end"], L=ev["L"], att=att,
                        gt_syn=np.where(st, 1 - att, att), d_syn=d_syn, wsw=wsw, az=az, n_syn=int(np.sum(np.diff(st) != 0)),
                        guess=int(rng.integers(0, 2))))
    return out


def fit_emissions(cfg, scheme, lag, train_ds):
    ds_all = []
    for ds in train_ds:
        ti = trial_index()
        for _, r in ti[ti.ds == ds].iterrows():
            z = np.load(os.path.join(CACHE, "evidence", cfg, scheme, lag, f"{r['key']}.npz"))
            d = (z["r0"] - z["r1"]).astype(float)
            sgn = np.where(z["att"] == 0, 1.0, -1.0)
            zz = d / running_sd(d, 60.0 / HOP)
            ds_all.append(np.c_[d * sgn, zz * sgn])
    A = np.concatenate(ds_all)
    return dict(mu=float(A[:, 0].mean()), sd=float(A[:, 0].std()), mu_z=float(A[:, 1].mean()))


def score_set(trials, fn, p, kind, null_seed=None):
    per = {}
    for tr in trials:
        d = tr["d_syn"] if kind == "syn" else tr["d"]
        if null_seed is not None:
            rng = np.random.default_rng(null_seed + zlib.crc32(tr["key"].encode()))
            d = np.abs(d) * rng.choice([-1.0, 1.0], len(d))
        dec = fn(d, p)
        lab = expand(dec, tr["t_end"], tr["L"], tr["T"], tr["guess"])
        if kind == "syn":
            gt = tr["gt_syn"]
        elif kind == "dir":                                  # genuine direction switches (KUL-AV-GC)
            s = np.sign(tr["az"])
            gt = (s > 0).astype(int)
            # decoded stream -> direction: attended stream sits at att_az, the other at -att_az
            dir_att = (s > 0).astype(int)
            lab = np.where(lab == tr["att"], dir_att, 1 - dir_att)
        else:
            gt = tr["att"]
        per.setdefault(tr["subj"], []).append((lab, gt))
    res = []
    for subj, lst in per.items():
        pred = np.concatenate([a for a, _ in lst])
        gt = np.concatenate([b for _, b in lst])
        trial = np.concatenate([np.full(len(a), i) for i, (a, _) in enumerate(lst)])
        r = fast_score_subject(pred, gt, trial, fs=FS, searchWindow=S["search_window_s"], tSustain=S["t_sustain_s"],
                               tau0=S["tau0_s"])
        r["subject"] = subj
        res.append(r)
    return res


def run_dataset(cfg, scheme, lag, ds):
    train = [d for d in DS_KEYS if d != ds]
    emis = fit_emissions(cfg, scheme, lag, train)
    trials = build_sets(cfg, scheme, lag, ds, SEED + DS_KEYS.index(ds))
    rows, subj_rows = [], []
    kinds = ["syn"] + (["dir"] if ds == "AVGC" else [])
    for name, fn, p in configs(emis):
        for kind in kinds:
            for null in (False, True):
                res = score_set(trials, fn, p, kind, null_seed=(SEED if null else None))
                agg = aggregate(res)
                rows.append(dict(cfg=cfg, scheme=scheme, lag=lag, test_ds=ds, integrator=name, switches=kind,
                                 null=null, **{k: agg[k] for k in ("Acc", "L", "SDT", "H", "nSwitches", "nUndetected")},
                                 miss_rate=agg["nUndetected"] / max(agg["nSwitches"], 1)))
                if not null:
                    for r in res:
                        subj_rows.append(dict(cfg=cfg, scheme=scheme, lag=lag, test_ds=ds, integrator=name,
                                              switches=kind, subject=r["subject"], acc=r["acc"], L=r["L"],
                                              sdt=r["sdt"], n_switches=r["nSwitches"], n_undetected=r["nUndetected"],
                                              H=(2 * r["acc"] / 100 * r["L"] / (r["acc"] / 100 + r["L"])
                                                 if np.isfinite(r["L"]) and (r["acc"] / 100 + r["L"]) > 0 else np.nan)))
    # oracle ceiling under the same synthetic schedule (perfect labels)
    per = {}
    for tr in trials:
        per.setdefault(tr["subj"], []).append((tr["gt_syn"], tr["gt_syn"]))
    ores = []
    for subj, lst in per.items():
        pred = np.concatenate([a for a, _ in lst])
        trial = np.concatenate([np.full(len(a), i) for i, (a, _) in enumerate(lst)])
        ores.append(fast_score_subject(pred, pred, trial, fs=FS))
    oa = aggregate(ores)
    rows.append(dict(cfg=cfg, scheme=scheme, lag=lag, test_ds=ds, integrator="ORACLE", switches="syn", null=False,
                     Acc=oa["Acc"], L=oa["L"], SDT=oa["SDT"], H=oa["H"], nSwitches=oa["nSwitches"],
                     nUndetected=oa["nUndetected"], miss_rate=oa["nUndetected"] / max(oa["nSwitches"], 1)))
    # plain-decoder accuracy unchanged by the synthetic swap (MATLAB assertion analogue)
    for t in trials:
        w_att = np.interp(t["t_end"] - 0.5, np.arange(t["T"]) / FS, t["att"]).round().astype(int)
        w_syn = np.where(t["wsw"], 1 - w_att, w_att)
        acc_before = np.mean((t["d"] < 0).astype(int) == w_att)
        acc_after = np.mean((t["d_syn"] < 0).astype(int) == w_syn)
        assert abs(acc_before - acc_after) < 1e-12, "synthetic swap changed plain accuracy"
    log.info(f"{cfg} {scheme} {lag} {ds}: {sum(t['n_syn'] for t in trials)} SYNTHETIC switches over {len(trials)} trials "
             f"({sum(t['n_syn'] == 0 for t in trials)} trials without one); emissions {emis}")
    return rows, subj_rows


if __name__ == "__main__":
    cfg = sys.argv[1] if len(sys.argv) > 1 else "c19-common57-car"
    scheme = sys.argv[2] if len(sys.argv) > 2 else "lodo_pool"
    lag = sys.argv[3] if len(sys.argv) > 3 else "0-500ms"
    res = Parallel(n_jobs=5)(delayed(run_dataset)(cfg, scheme, lag, ds) for ds in DS_KEYS)
    df = pd.DataFrame([r for rs, _ in res for r in rs])
    sj = pd.DataFrame([r for _, rs in res for r in rs])
    tag = f"{cfg}_{scheme}_{lag}"
    df.to_csv(os.path.join(TAB, f"switch_results_{tag}.csv"), index=False)
    sj.to_csv(os.path.join(TAB, f"switch_per_subject_{tag}.csv"), index=False)
    # choose the best configuration on the MEDIAN held-out dataset (SYNTHETIC switches, real evidence)
    real = df[(~df.null) & (df.switches == "syn") & (df.integrator != "ORACLE")]
    med = real.groupby("integrator").H.median().sort_values(ascending=False)
    best = med.index[0]
    log.info(f"best integrator by median-over-datasets H (SYNTHETIC): {best} (median H {med.iloc[0]:.4f})\n"
             + med.head(10).to_string())
    pd.DataFrame(dict(integrator=med.index, median_H_over_datasets=med.values)).to_csv(
        os.path.join(TAB, f"switch_ranking_{tag}.csv"), index=False)
    # lookahead budget itemisation
    L = float(lag.split("-")[1][:-2]) / 1000
    bud = [dict(component="causal filters (HP, BP, anti-alias, running z)", lookahead_ms=0),
           dict(component=f"backward decoder lags 0-{int(L * 1000)} ms (EEG after the envelope sample)",
                lookahead_ms=L * 1000),
           dict(component="1-s correlation window (stamped at its end)", lookahead_ms=0),
           dict(component="decision stamping advance (labels samples from t_end + L - 1 s)",
                lookahead_ms=(BUDGET - L) * 1000),
           dict(component="integrator (causal forward filter / moving average / ladder)", lookahead_ms=0),
           dict(component="TOTAL", lookahead_ms=BUDGET * 1000)]
    pd.DataFrame(bud).to_csv(os.path.join(TAB, f"switch_lookahead_budget_{lag}.csv"), index=False)
    # trade-off plot with iso-H contours
    fig, axs = plt.subplots(1, 5, figsize=(22, 4.4), sharex=True, sharey=True)
    A_, L_ = np.meshgrid(np.linspace(0.4, 1, 100), np.linspace(0, 1, 100))
    Hh = 2 * A_ * L_ / (A_ + L_)
    for ax, ds in zip(axs, DS_KEYS):
        cs = ax.contour(A_ * 100, L_, Hh, levels=[0.3, 0.4, 0.5, 0.6, 0.7, 0.8], colors="gray", linewidths=0.6)
        ax.clabel(cs, fontsize=6)
        for fam, mk in (("argmax", "o"), ("ma_", "s"), ("hmm_p", "^"), ("hmmz", "v"), ("ladder", "D")):
            g = real[(real.test_ds == ds) & real.integrator.str.startswith(fam)]
            ax.scatter(g.Acc, g.L, marker=mk, s=18, color=COLORS[ds], alpha=0.7, label=fam.rstrip("_"))
            gn = df[df.null & (df.switches == "syn") & (df.test_ds == ds) & df.integrator.str.startswith(fam)]
            ax.scatter(gn.Acc, gn.L, marker=mk, s=10, color="lightgray")
        o = df[(df.integrator == "ORACLE") & (df.test_ds == ds)]
        ax.axhline(min(float(o.L.iloc[0]), 0.995), color="k", ls=":", lw=0.8, label=f"oracle L={float(o.L.iloc[0]):.2f} (misses {int(o.nUndetected.iloc[0])}/{int(o.nSwitches.iloc[0])})")
        ax.set_title(f"{LABELS[ds]} held out (n={real[real.test_ds == ds].nSwitches.iloc[0]} SYNTHETIC switches)", fontsize=8)
        ax.set_xlabel("Acc (%)")
    axs[0].set_ylabel("L")
    axs[0].legend(fontsize=6)
    fig.suptitle(f"SYNTHETIC switches, LODO evidence ({tag}); grey = sign-randomised null; lines = iso-H")
    savefig(fig, f"e1_tradeoff_{tag}", df)
    log.info("\n" + real[real.integrator == best][["test_ds", "Acc", "SDT", "L", "H", "miss_rate", "nSwitches"]].to_string())
