"""B2 of REAL_SWITCH_STRATEGY.md: Bayesian online change-point detection (BOCPD, Adams & MacKay 2007) as the decision rule.

Evidence d_t (0.25-s steps) is Gaussian around an unknown segment mean theta (sign = attended column, size = how strong the EEG is
in that segment): d ~ N(theta, sigma^2), theta ~ N(0, tau^2) per segment, a new segment starts at every step with hazard h (constant,
so the prior does not know where the trial starts). The filter keeps P(run length r | d_1..t) for r = 0..R-1 (the last bin absorbs
longer runs; its statistics use the last R-1 samples) and returns P(theta_t < 0) = sum_r P(r) Phi(-m_r / sqrt(v_r)), with the
conjugate posterior of theta given the last r samples. Differences to the 2-state HMM: no fixed emission (mu, +-mu) is needed per
segment (the segment mean is learned inside the run, the corpus only sets sigma and the prior width tau), and the prior is symmetric
and the filter exactly antisymmetric (d -> -d gives P -> 1 - P: no bias, no column preference).
Remaining knob: the hazard. 'bma' removes the single choice: it runs several hazards (mean segment 5..125 s) in parallel and mixes them
by their running predictive likelihood (exponential forgetting 0.99 per step, ~25 s), causal and within the trial only.
sigma^2 = max(sd^2 - mu^2, 0.05 sd^2) and tau = tau_mult * |mu| come from the corpus emissions (mu, sd) as for the HMM.
Same fixed pipeline, replicates, real/synthetic reporting as hmm_semimarkov.py (B1).
usage: python hmm_bocpd.py [--selftest | --summarize] -> results/research/bocpd.csv, bocpd.txt"""
import argparse
import math
import multiprocessing as mp
import os

import numpy as np
import pandas as pd
from numba import njit

DT = 0.25
R = 480                                                     # run-length bins (120 s)
LAMS = (5.0, 10.0, 20.0, 40.0, 80.0, 125.0)                 # mean segment length of the hazard (s)
TAUS = (0.5, 1.0, 2.0)
GEO = (0.998, 0.95, 0.9)
FORGET = 0.99


@njit(cache=True)
def _phi(z):
    return 0.5 * (1.0 + math.erf(z / math.sqrt(2.0)))


@njit(cache=True)
def _bocpd(x, sig2, tau2, haz, lw, w, p1):
    """One BOCPD run (hazard haz). lw: running log predictive of this model for the mixture. Returns lw increments in `lw`
    (array of length n: log predictive of x_t) and fills p1 (length n)."""
    n = len(x)
    cs = np.zeros(n + 1)
    for t in range(n):
        cs[t + 1] = cs[t] + x[t]
    w[:] = 0.0
    w[0] = 1.0                                              # before the first sample: fresh segment prior (stats are only the data)
    nw = np.empty(R)
    for t in range(n):
        if t > 0:                                           # transition: grow the run or start a new one
            tot0 = 0.0
            for r in range(R):
                tot0 += w[r] * haz
            nw[0] = tot0
            for r in range(1, R):
                nw[r] = w[r - 1] * (1.0 - haz)
            nw[R - 1] += w[R - 1] * (1.0 - haz)
            for r in range(R):
                w[r] = nw[r]
        # emission: predictive of x_t under run r (stats of the last r samples before t)
        c = 0.0
        for r in range(R):
            m_ = min(r, t)
            if m_ == 0 and r > 0 and w[r] == 0.0:
                continue
            s = cs[t] - cs[t - m_]
            v = 1.0 / (1.0 / tau2 + m_ / sig2)
            mu = v * s / sig2
            var = sig2 + v
            pr = math.exp(-0.5 * (x[t] - mu) ** 2 / var) / math.sqrt(2.0 * math.pi * var)
            w[r] *= pr
            c += w[r]
        lw[t] = math.log(max(c, 1e-300))
        acc = 0.0
        for r in range(R):
            w[r] /= c
            m_ = min(r + 1, t + 1)
            s = cs[t + 1] - cs[t + 1 - m_]
            v = 1.0 / (1.0 / tau2 + m_ / sig2)
            acc += w[r] * _phi(-(v * s / sig2) / math.sqrt(v))
        p1[t] = acc


def bocpd_filter(x, sig, tau, lams):
    """P(col 1 attended) per step. lams: one hazard -> plain BOCPD; several -> mixture by running predictive likelihood."""
    x = np.ascontiguousarray(x, dtype=np.float64)
    n = len(x)
    K = len(lams)
    P = np.empty((K, n))
    L = np.empty((K, n))
    w = np.empty(R)
    for k, lam in enumerate(lams):
        _bocpd(x, sig * sig, tau * tau, 1.0 - math.exp(-DT / lam), L[k], w, P[k])
    if K == 1:
        return P[0]
    out = np.empty(n)
    lwk = np.zeros(K)
    for t in range(n):
        wk = np.exp(lwk - lwk.max())
        out[t] = (wk / wk.sum()) @ P[:, t]                   # weights from the past steps only; every P[k, t] is itself causal
        lwk = FORGET * lwk + L[:, t]
    return out


def selftest():
    import itertools
    import common_exp  # noqa: F401
    from scipy.stats import norm
    rng = np.random.default_rng(1)
    sig, tau, lam = 1.0, 0.8, 1.0
    haz = 1.0 - math.exp(-DT / lam)
    # exact enumeration of all change-point configurations on a short sequence
    T = 9
    x = rng.normal(size=T) * 1.0 + np.r_[np.full(4, 0.7), np.full(5, -0.6)]
    post, tot = 0.0, 0.0
    for cp in itertools.product([0, 1], repeat=T - 1):
        segs, cur = [], [0]
        for t in range(1, T):
            if cp[t - 1]:
                segs.append(cur)
                cur = []
            cur.append(t)
        segs.append(cur)
        lik = np.prod([haz if c else 1 - haz for c in cp])
        for sg in segs:
            xs = x[sg]
            C = sig ** 2 * np.eye(len(sg)) + tau ** 2 * np.ones((len(sg), len(sg)))
            lik *= np.exp(-0.5 * xs @ np.linalg.solve(C, xs)) / np.sqrt((2 * np.pi) ** len(sg) * np.linalg.det(C))
        sg = segs[-1]
        v = 1 / (1 / tau ** 2 + len(sg) / sig ** 2)
        m = v * x[sg].sum() / sig ** 2
        post += lik * norm.cdf(-m / np.sqrt(v))
        tot += lik
    exact = post / tot
    got = bocpd_filter(x, sig, tau, (lam,))[-1]
    print(f"exact enumeration {exact:.10f} | filter {got:.10f}")
    assert abs(exact - got) < 1e-9
    z = rng.normal(size=500)
    a, b = bocpd_filter(z, sig, tau, (5.0,)), bocpd_filter(-z, sig, tau, (5.0,))
    print("antisymmetry max |P(-x) - (1 - P(x))| =", np.abs(a - (1 - b)).max())
    assert np.abs(a - (1 - b)).max() < 1e-9
    a, b = bocpd_filter(z, sig, tau, LAMS), bocpd_filter(-z, sig, tau, LAMS)
    print("antisymmetry (mixture) max =", np.abs(a - (1 - b)).max())
    assert np.abs(a - (1 - b)).max() < 1e-9
    p = bocpd_filter(np.zeros(1000), sig, tau, (20.0,))
    print("no evidence: P min/max", p.min(), p.max())
    step = np.r_[np.full(200, 0.7), np.full(200, -0.7)] + rng.normal(size=400)
    p = bocpd_filter(step, 1.0, 0.7, (40.0,))
    print("step 0.7/-0.7: mean P(col1) first half %.3f, second half %.3f, 20 steps after the switch %.3f" % (p[:200].mean(), p[200:].mean(), p[220]))
    print("selftest ok")


# ---------------------------------------------------------------------------------------------------------------------
_TASK = None


def _run(args):
    return _TASK(args)


def main():
    import fuse_external as FX
    import fuse_general as FG
    import eval_external as E
    import score as S
    import e1_switch
    from common import DS_KEYS
    from common_exp import EXP
    from fuse_evidence import LA

    DEC = ["vlaai_ws__eaP_n5_t60_aug_cluster_onset_dom_xFUG_ptSPK" + t for t in ("", "_s1", "_s2", "_s3")]
    CLF = ["direct_clf__eaP_n5_t60_w10b3_ptSPK" + t + "_aug-sw2" for t in ("", "_s1", "_s2", "_s3")]
    REPS = {"all4": [0, 1, 2, 3], "s01": [0, 1], "s23": [2, 3]}
    CUR = {}

    def integ(d, p):
        if CUR["kind"] == "geo":
            return e1_switch.integ_hmm(d, dict(p_stay=CUR["p"], mu=p["mu"], sd=p["sd"]))
        mu, sd = abs(p["mu"]), p["sd"]
        sig = math.sqrt(max(sd * sd - mu * mu, 0.05 * sd * sd))
        return (bocpd_filter(d, sig, CUR["tau"] * mu, CUR["lams"]) > 0.5).astype(int)

    S.integ_hmm = integ
    E.integ_hmm = integ

    data = {}
    for rep, idx in REPS.items():
        print("building evidence", rep, flush=True)
        z, meta = FG.build([DEC[i] for i in idx], [CLF[i] for i in idx])
        decs, clfs = [[DEC[i], str(i)] for i in idx], [[CLF[i], "_w10b3sw2P", str(i)] for i in idx]
        ev_real, emis = FX.build_external(decs, clfs, [0.5], ["AASD", "Carta2026"], "_bestP")
        data[rep] = (z, meta, ev_real, emis[0.5])

    def task(args):
        rep, ds, kind, par = args
        CUR.clear()
        CUR["kind"] = kind
        if kind == "geo":
            CUR["p"] = par
        elif kind == "bocpd":
            CUR["lams"], CUR["tau"] = (par[0],), par[1]
        else:
            CUR["lams"], CUR["tau"] = LAMS, par
        z, meta, ev_real, emis = data[rep]
        if ds in DS_KEYS:
            zd, zc = z[ds]
            ev = {d: {k: dict(r0=0.5 * zd[d][k] + 0.5 * zc[d][k], r1=np.zeros(len(zd[d][k])), att=meta[d][k][0], t_end=meta[d][k][1],
                              pre_switched=True) for k in zd[d]} for d in DS_KEYS}
            r = S.score_dataset(ds, ev, LA, p_stay=0.998)
        else:
            r = E.score(ev_real[ds][0.5], *emis, 0.998, LA)
        return dict(rep=rep, ds=ds, pt=label(kind, par), H=r["H"], Acc=r["Acc"], SDT=r["SDT"])

    points = [("geo", p) for p in GEO] + [("bocpd", (lam, t)) for lam in LAMS for t in TAUS] + [("bma", t) for t in TAUS]
    dss = ["AASD", "Carta2026"] + list(DS_KEYS)
    tasks = [(rep, ds, kind, par) for rep in REPS for (kind, par) in points for ds in dss]
    out = os.path.join(EXP, "results", "research", "bocpd.csv")
    if os.path.exists(out):
        os.remove(out)
    global _TASK
    _TASK = task
    rows = []
    with mp.get_context("fork").Pool(int(os.environ.get("SCORE_JOBS", 3))) as pool:
        for i, r in enumerate(pool.imap_unordered(_run, tasks, chunksize=1)):
            rows.append(r)
            pd.DataFrame([r]).to_csv(out, mode="a", header=not os.path.exists(out), index=False)
            if i % 20 == 0:
                print(f"{i + 1}/{len(tasks)} {r}", flush=True)
    summarize(pd.DataFrame(rows), os.path.join(EXP, "results", "research", "bocpd.txt"))


def label(kind, par):
    return f"geo p{par}" if kind == "geo" else (f"bocpd l{par[0]:g} t{par[1]:g}" if kind == "bocpd" else f"bma t{par:g}")


def summarize(df, path):
    lines = []

    def say(s=""):
        print(s, flush=True)
        lines.append(s)

    df = df.copy()
    df["fam"] = np.where(df.ds.isin(["AASD", "Carta2026"]), df.ds, "synthetic")
    pv = df.groupby(["rep", "pt", "fam"]).H.mean().reset_index().pivot_table(index=["rep", "pt"], columns="fam", values="H")
    pv["real"] = pv[["AASD", "Carta2026"]].mean(1)
    ref = "geo p0.998"
    t = pv.xs("all4", level="rep")
    t = t.assign(dAASD=t.AASD - t.loc[ref, "AASD"], dCarta=t.Carta2026 - t.loc[ref, "Carta2026"], dSyn=t.synthetic - t.loc[ref, "synthetic"])
    say("H, 4-seed ensemble (all4); synthetic = mean of the 5 LODO datasets; reference = HMM geometric p_stay 0.998")
    say(t.round(4).sort_values("real", ascending=False).to_string())
    names = list(t.index)
    bo = [n for n in names if n.startswith("bocpd")]
    bm = [n for n in names if n.startswith("bma")]
    choices = {"best BOCPD on AASD (-> Carta)": t.loc[bo, "AASD"].idxmax(), "best BOCPD on Carta (-> AASD)": t.loc[bo, "Carta2026"].idxmax(),
               "best BOCPD on real mean (-> synthetic)": t.loc[bo, "real"].idxmax(), "best BOCPD on synthetic (-> real)": t.loc[bo, "synthetic"].idxmax()}
    choices.update({f"BMA, no hazard choice: {n}": n for n in bm})
    rows = [dict(rule=why, point=pt, rep=rep, fam=fam, base=pv.loc[(rep, ref), fam], new=pv.loc[(rep, pt), fam])
            for why, pt in choices.items() for rep in ("s01", "s23", "all4") for fam in ("AASD", "Carta2026", "synthetic")]
    c = pd.DataFrame(rows)
    c["delta"] = c.new - c.base
    say("\nSelection (cross-fitted); replicates = seeds {0,1}, {2,3}; headline all4")
    say(c.round(4).to_string(index=False))
    best_bma = t.loc[bm, "real"].idxmax()
    say(f"\nPer dataset (all4): HMM 0.998 vs {best_bma}: H / Acc / SDT")
    q = df[(df.rep == "all4") & (df.pt.isin([ref, best_bma]))].pivot_table(index="ds", columns="pt", values=["H", "Acc", "SDT"])
    say(q.round(3).to_string())
    open(path, "w").write("\n".join(lines) + "\n")


if __name__ == "__main__":
    ap = argparse.ArgumentParser()
    ap.add_argument("--selftest", action="store_true")
    ap.add_argument("--summarize", action="store_true")
    a = ap.parse_args()
    if a.selftest:
        selftest()
    elif a.summarize:
        from common_exp import EXP
        summarize(pd.read_csv(os.path.join(EXP, "results", "research", "bocpd.csv")), os.path.join(EXP, "results", "research", "bocpd.txt"))
    else:
        main()
