"""Submission step 3: fuse the evidence of the main ensemble (eq3 = 4 decoders + 4 DirectNet sw2 + 4 BMMS3, equal
thirds), causal HMM, label stamping, CSV in the organisers' format.

calibrate  fixed constants from the CORPUS only (the outer 'ALL' calibration of r3_bmms3_combos.py: LDA target weights
           of each decoder seed, DirectNet / decoder / BMMS3 scales, HMM emissions mu / sd of the mixed evidence on the
           five corpus datasets' LODO evidence) -> results/submission/calibration_eq3.json.
           Check: the same constants and code applied to the stored AASD / Carta2026 evidence must reproduce the r3 eq3
           all4 numbers at p 0.998 (Acc 52.7650 / 59.8435, kappa_next_inclusive scorer).
predict    per trial of a converted test set: x = [4 DirectNet logits, 4 x 3 decoder target deltas] (nested_stack_fusion
           .join), BMMS3 mean logit at the same times; mix = (decoder-LDA part + DirectNet part + BMMS3 / sb) / 3; HMM
           forward filter (p_stay 0.998, emissions mu, sd) -> P(stream 2 attended) > 0.5; a decision of the window ending
           at t_end labels the 128-Hz samples from t_end + L - 1 s on (e1_switch.expand, total lookahead 1 s); samples
           before the first decision take the organisers' content-seeded guess (challenge/hook/guess_label.py; it reads
           eight samples spread over the trial) or, with env SUB_GUESS=causal, guess_label_causal (first sample only). Every
           trial is decoded on its own; the HMM starts at (0.5, 0.5) in every trial. Label 0 = envelope_stream1,
           1 = envelope_stream2 (attended-speaker-label). One test_sample_<subject:03d>.csv per subject.
usage: python sub_fuse.py calibrate
       python sub_fuse.py predict --set TEST_DLAAD --out <submission>/dlaad
"""
import argparse
import csv
import json
import os
import pickle
import sys

HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)

import numpy as np  # noqa: E402
import pandas as pd  # noqa: E402

from common_exp import EXP  # noqa: E402

SUB = os.path.join(EXP, "results", "submission")
CAL = os.path.join(SUB, "calibration_eq3.json")
SEEDS = (0, 1, 2, 3)
P_STAY = 0.998
WEIGHTS = (1 / 3, 1 / 3, 1 / 3)                    # decoder, DirectNet, BMMS3
FS128 = 128


def bvec(e, times):
    """BMMS3 logit at the evidence times (bmms3_enhanced_4seed_mix_score.bvec)."""
    te = np.asarray(e["t_end"], float)
    times = np.asarray(times, float)
    idx = np.minimum(np.searchsorted(te, times), len(te) - 1)
    prev = np.maximum(idx - 1, 0)
    idx = np.where(np.abs(te[prev] - times) < np.abs(te[idx] - times), prev, idx)
    if not np.allclose(te[idx], times, atol=1e-3, rtol=0):
        raise ValueError("BMMS timing misalignment")
    return (e["r0"] - e["r1"])[idx]


def mixer(c):
    W, Sc = np.asarray(c["W"]), np.asarray(c["Sc"])

    def mix(x, bm):
        d = np.mean([x[:, 4 + 3 * i:7 + 3 * i] @ W[i] for i in range(4)], axis=0) / c["sd_dec"]
        k = np.mean([x[:, i] / Sc[i] for i in range(4)], axis=0) / c["sd_clf"]
        wd, wc, wb = c["weights"]
        return wd * d + wc * k + wb * bm / c["sb"]
    return mix


def calibrate():
    import bmms3_enhanced_4seed_mix_score as B4
    import eval_external as E
    import nested_schedule_eval as N
    import r1_acc_decision_grid as R
    import r3_bmms3_combos as R3
    import score as S
    from common import DS_KEYS
    R.install_scorer()
    clf, dec, la = R.load_reference()
    bm, _ = zip(*[S.load_folds(R3.BM.format(i=i), "causal") for i in SEEDS])
    tab = R.tables("ALL", clf, dec)
    trials = [t for d in DS_KEYS for t in tab[d].values()]
    # components() of r3, written out so the constants can be stored
    x = np.concatenate([t["x"] * np.where(t["y"] == 0, 1., -1.)[:, None] for t in trials])
    W, Sc = [], []
    for i in SEEDS:
        v = x[:, 4 + 3 * i:7 + 3 * i].astype(float)
        w = np.linalg.solve(np.cov(v.T) + np.eye(3) * 1e-8, v.mean(0))
        W.append(w / max((v @ w).std(), 1e-8))
        Sc.append(max(x[:, i].std(), 1e-8))
    zd = np.mean([x[:, 4 + 3 * i:7 + 3 * i] @ W[i] for i in SEEDS], axis=0)
    zc = np.mean([x[:, i] / Sc[i] for i in SEEDS], axis=0)
    c = dict(W=[w.tolist() for w in W], Sc=Sc, sd_dec=max(float(zd.std()), 1e-8), sd_clf=max(float(zc.std()), 1e-8),
             weights=list(WEIGHTS), p_stay=P_STAY, lookahead_s=la, seeds=list(SEEDS))
    bms = {d: {k: np.mean([bvec(bm[i][d][k], t["ev"]["t_end"]) for i in SEEDS], axis=0) for k, t in tab[d].items()} for d in DS_KEYS}
    c["sb"] = float(np.concatenate([bms[d][k] for d in DS_KEYS for k in bms[d]]).std())
    mix = mixer(c)
    signed = np.concatenate([mix(t["x"], bms[d][k]) * np.where(t["y"] == 0, 1., -1.) for d in DS_KEYS for k, t in tab[d].items()])
    c["mu"], c["sd"] = float(signed.mean()), float(signed.std())
    os.makedirs(SUB, exist_ok=True)
    json.dump(c, open(CAL, "w"), indent=1)
    print("calibration ->", CAL, json.dumps({k: v for k, v in c.items() if k not in ("W",)}), flush=True)
    # reproduction check on the stored real-set evidence (the r3 eq3 all4 row at p 0.998)
    import e1_switch
    def integ(d, prm):
        return e1_switch.integ_hmm(d, dict(p_stay=P_STAY, mu=prm["mu"], sd=prm["sd"]))
    E.integ_hmm = integ
    want = {"AASD": 52.7650, "Carta2026": 59.8435}
    ok = True
    for ds, acc in want.items():
        real = R.real_inputs(ds)
        rbm = [N.load(os.path.join(EXP, "results", "research", f"ext_clf_{ds}_w10bmms3enhP_evidence_s{i}.pkl")) for i in SEEDS]
        ev = {k: dict(e, r0=mix(x, np.mean([B4.bvec(rbm[i][k], e["t_end"])[0] for i in SEEDS], axis=0)), r1=np.zeros(len(e["att"])))
              for k, (x, e) in real.items()}
        out = E.score(ev, c["mu"], c["sd"], P_STAY, la)
        print(f"check {ds}: Acc {out['Acc']:.4f} (r3 eq3 all4: {acc:.4f}), H {out['H']:.4f}", flush=True)
        ok &= abs(out["Acc"] - acc) < 1e-3
    if not ok:
        raise SystemExit("calibration check FAILED")
    print("calibration check passed", flush=True)


def guess_label(neural):
    """challenge/hook/guess_label.py (organisers' fixed helper): neural (channels, samples)."""
    def step(state, value):
        key = int(np.floor(float(value))) if np.isfinite(value) else 0
        return (1664525 * state + 1013904223 + key % (1 << 32)) % (1 << 32)
    n_samples, n_channels = neural.shape[1], neural.shape[0]
    state = 0
    for i in range(8):
        state = step(state, float(neural[i % n_channels, int(np.floor(i * (n_samples - 1) / 7))]))
    state = step(state, float(neural.size))
    state = step(state, 0.0)
    return int((state // 65536) % 2)


def guess_label_causal(neural):
    """Optional causal variant (env SUB_GUESS=causal): the same generator keyed on the FIRST converted EEG sample only
    (trial time t0 = 1 s, i.e. within the 1-s budget of the earliest guessed 128-Hz sample at t = 0) instead of eight
    samples spread over the whole trial and the trial length. Only the samples before the first decision change."""
    def step(state, value):
        key = int(np.floor(float(value))) if np.isfinite(value) else 0
        return (1664525 * state + 1013904223 + key % (1 << 32)) % (1 << 32)
    state = 0
    for c in range(neural.shape[0]):
        state = step(state, float(neural[c, 0]) * 1000.0)
    state = step(state, 0.0)
    return int((state // 65536) % 2)


def guess(neural):
    mode = os.environ.get("SUB_GUESS", "organisers")
    assert mode in ("organisers", "causal"), mode
    return guess_label_causal(neural) if mode == "causal" else guess_label(neural)


def load_evidence(name, model):
    return [pickle.load(open(os.path.join(SUB, name, f"{model}_s{i}.pkl"), "rb")) for i in SEEDS]


def write_predictions(name, out_dir, decide, tag="eq3"):
    """decide(key) -> (per-step decisions 0/1, t_end of the steps, lookahead_s); one CSV per subject."""
    from e1_switch import expand
    idx = pd.read_csv(os.path.join(EXP, "..", "external_data", name, "cache", "index.csv"))
    os.makedirs(out_dir, exist_ok=True)
    stats = []
    for sid, g in idx.groupby("subject"):
        rows = []
        for _, r in g.sort_values("trial").iterrows():
            k = r["key"]
            dec, t_end, la = decide(k)
            z = np.load(os.path.join(EXP, "..", "external_data", name, "cache", "eaP_n5_t60", k + ".npz"))
            lab = expand(dec, np.asarray(t_end, float), la, int(r["n"]), guess(z["eeg"].T))
            assert len(lab) == int(r["n"]) and np.isin(lab, (0, 1)).all()
            rows += [(t, int(v), int(r["trial"])) for t, v in enumerate(lab)]
            stats.append(dict(subject=sid, trial=int(r["trial"]), share_stream2=float(lab.mean()),
                              n_switches=int((np.diff(lab) != 0).sum())))
        with open(os.path.join(out_dir, f"test_sample_{int(sid):03d}.csv"), "w", newline="") as fh:
            w = csv.writer(fh, lineterminator="\n")
            w.writerow(("time_index", "label", "trial"))
            w.writerows(rows)
    s = pd.DataFrame(stats)
    s.to_csv(os.path.join(SUB, f"{name}_{tag}_prediction_stats.csv"), index=False)
    print(f"{name} [{tag}]: {s.subject.nunique()} subjects, {len(s)} trials -> {out_dir}; share of label 1 (stream 2) "
          f"{s.share_stream2.mean():.3f}, decided switches per trial median {s.n_switches.median():.0f}", flush=True)


def predict(name, out_dir):
    import nested_stack_fusion as F
    from e1_switch import integ_hmm
    c = json.load(open(CAL))
    mix = mixer(c)
    ed, ec, eb = load_evidence(name, "dec"), load_evidence(name, "clf"), load_evidence(name, "bmms")
    idx = pd.read_csv(os.path.join(EXP, "..", "external_data", name, "cache", "index.csv"))
    assert set(idx.key) == set(ed[0]) == set(ec[0]) == set(eb[0]), "evidence does not cover every trial"

    def decide(k):
        x, e = F.join(ed, ec, k)
        d = mix(x, np.mean([bvec(eb[i][k], e["t_end"]) for i in SEEDS], axis=0))
        return integ_hmm(d, dict(p_stay=c["p_stay"], mu=c["mu"], sd=c["sd"])), e["t_end"], c["lookahead_s"]
    write_predictions(name, out_dir, decide, "eq3")


if __name__ == "__main__":
    ap = argparse.ArgumentParser()
    ap.add_argument("cmd", choices=("calibrate", "predict"))
    ap.add_argument("--set")
    ap.add_argument("--out")
    a = ap.parse_args()
    calibrate() if a.cmd == "calibrate" else predict(a.set, a.out)
