"""Submission with the B3 V2 learned decision rule (hmm_learned.py; LODO H 0.641 under the kappa scorer, user 2026-11-11).

Evidence = the 4 + 4 reference (4 decoders + 4 DirectNet sw2, a = 0.5, fuse_external.build_external weights from the
corpus). Decision = the antisymmetric learned rule (hmm_learned.Rule: bias-free read-out of causal EMAs of the evidence,
no position, dataset or column information) instead of the HMM.

check    retrain V2:DTU and V2:Carta2026 (training seed 0) exactly as hmm_learned.py and score them with the kappa scorer:
         must reproduce learned_rule_kappa_next_inclusive.csv (all4, seed 0): DTU Acc 74.658, Carta2026 Acc 53.514.
train    V2:ALL = the V2 recipe with every available training set: real AASD + Carta2026 (weight 0.5) and the synthetic
         switches of all five corpus datasets on their own out-of-fold evidence (weight 0.5); training seeds 0 and 1
         -> results/submission/b3v2_rule_s<seed>.npz. This exact rule was never scored (each scored V2 rule left one set out).
predict  test evidence (results/submission/<SET>/dec_s*.pkl, clf_s*.pkl) through build_external; decision = sign of the
         mean logit of the two rules; labels and CSV as sub_fuse.write_predictions (stamping budget 1 s).
usage: python sub_b3v2.py check | train | predict --set TEST_DLAAD --out DIR
"""
import argparse
import os
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

import hmm_learned as HL  # noqa: E402
import sub_fuse as SF  # noqa: E402
from common import DS_KEYS, SEED  # noqa: E402
from common_exp import EXP, rows_of  # noqa: E402

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")]
DECS, CLFS = [[DEC[i], str(i)] for i in range(4)], [[CLF[i], "_w10b3sw2P", str(i)] for i in range(4)]
REAL = ("AASD", "Carta2026")
RULE = lambda s: os.path.join(SF.SUB, f"b3v2_rule_s{s}.npz")


def training_sets():
    """hmm_learned.main, rep all4: corpus fused evidence + synthetic truth, real-set evidence."""
    import fuse_external as FX
    import fuse_general as FG
    import score as S
    import e1_switch
    from fuse_evidence import LA
    z, meta = FG.build(DEC, CLF)
    ev_real, _ = FX.build_external(DECS, CLFS, [0.5], list(REAL), "_bestP")
    real = {ds: [dict(F=HL.ema_features(e["r0"].astype(float)), t=HL.soft_target(e["t_end"], e["att128"], LA),
                      g=f"{ds}:{e['subject']}", w=None, ds=ds) for e in ev_real[ds][0.5].values()] for ds in REAL}
    truth = {}
    for ds in DS_KEYS:
        rng = np.random.default_rng(SEED + S.SCHED + DS_KEYS.index(ds))
        zd, zc = z[ds][0][ds], z[ds][1][ds]
        truth[ds] = {}
        for _, r in rows_of(ds).iterrows():
            T = int(r["n"])
            att = np.load(os.path.join(S.EDA_CACHE, "cnd", f"{r['key']}.npz"))["att_col"][:T].astype(int)
            d = 0.5 * zd[r["key"]] + 0.5 * zc[r["key"]]
            st, _, _ = e1_switch.synth_trial(r["key"], T, d, meta[ds][r["key"]][1].astype(float), rng)
            rng.integers(0, 2)
            truth[ds][r["key"]] = (np.where(st, 1 - att, att).astype(float), r["subj_uid"])

    def synth(fold, ds):
        zd, zc = z[fold]
        return [dict(F=HL.ema_features(0.5 * zd[ds][k] + 0.5 * zc[ds][k]), t=HL.soft_target(meta[ds][k][1].astype(float), gt, LA),
                     g=f"{ds}:{subj}", w=None, ds=ds) for k, (gt, subj) in truth[ds].items()]
    return z, meta, ev_real, real, synth


def weight(sets, share):
    for grp, sh in zip(sets, share):
        n = sum(len(tr["t"]) for tr in grp)
        for tr in grp:
            tr["w"] = np.full(len(tr["t"]), sh / n)
    return [tr for grp in sets for tr in grp]


def fit(sets, share, seed):
    rule, vl, ne = HL.fit_rule(weight([[dict(tr) for tr in g] for g in sets], share), seed)
    print(f"  rule seed {seed}: val BCE {vl:.4f} after {ne} epochs", flush=True)
    return rule


def check():
    import eval_external as E
    import r1_acc_decision_grid as R
    import score as S
    from fuse_evidence import LA
    R.install_scorer()
    z, meta, ev_real, real, synth = training_sets()
    want = pd.read_csv(os.path.join(EXP, "results", "research", "learned_rule_kappa_next_inclusive.csv"))
    want = want[(want.rep == "all4") & (want.rule == "V2") & (want.seed == 0)].set_index("ds")
    ok = True
    # V2:DTU = real AASD + Carta and the synthetic of the other four datasets (fold DTU evidence)
    rule = fit([real["AASD"] + real["Carta2026"], [tr for ds in DS_KEYS if ds != "DTU" for tr in synth("DTU", ds)]], [.5, .5], 0)
    S.integ_hmm = lambda d, p: rule.decide(d)
    zd, zc = z["DTU"]
    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("DTU", ev, LA, p_stay=0.998)
    print(f"check V2:DTU seed 0: Acc {r['Acc']:.3f} H {r['H']:.4f} (stored {want.loc['DTU', 'Acc']:.3f} / {want.loc['DTU', 'H']:.4f})", flush=True)
    ok &= abs(r["Acc"] - want.loc["DTU", "Acc"]) < 1e-2
    # V2:Carta2026 = real AASD and the synthetic of all five datasets
    rule = fit([real["AASD"], [tr for ds in DS_KEYS for tr in synth(ds, ds)]], [.5, .5], 0)
    E.integ_hmm = lambda d, p: rule.decide(d)
    r = E.score(ev_real["Carta2026"][0.5], 0.0, 1.0, 0.998, LA)
    print(f"check V2:Carta2026 seed 0: Acc {r['Acc']:.3f} H {r['H']:.4f} (stored {want.loc['Carta2026', 'Acc']:.3f} / "
          f"{want.loc['Carta2026', 'H']:.4f})", flush=True)
    ok &= abs(r["Acc"] - want.loc["Carta2026", "Acc"]) < 1e-2
    if not ok:
        raise SystemExit("B3 V2 reproduction check FAILED")
    print("B3 V2 reproduction check passed", flush=True)


def train():
    _, _, _, real, synth = training_sets()
    allsyn = [tr for ds in DS_KEYS for tr in synth(ds, ds)]
    os.makedirs(SF.SUB, exist_ok=True)
    for s in (0, 1):
        r = fit([real["AASD"] + real["Carta2026"], allsyn], [.5, .5], s)
        np.savez(RULE(s), lin=r.lin, W=r.W, v=r.v, scale=r.scale)
        print("saved", RULE(s), flush=True)


def predict(name, out_dir):
    import fuse_external as FX
    from fuse_evidence import LA
    link = os.path.join(SF.SUB, "fx_links", name)
    os.makedirs(link, exist_ok=True)
    for i in range(4):                                       # build_external reads the external-set file names
        for src, dst in ((f"dec_s{i}.pkl", f"ext_c1_{name}_bestP_evidence_s{i}.pkl"),
                         (f"clf_s{i}.pkl", f"ext_clf_{name}_w10b3sw2P_evidence_s{i}.pkl")):
            p = os.path.join(link, dst)
            if not os.path.lexists(p):
                os.symlink(os.path.join(SF.SUB, name, src), p)
    FX.RES = link
    ev, _ = FX.build_external(DECS, CLFS, [0.5], [name], "_bestP")
    ev = ev[name][0.5]
    rules = [HL.Rule(*(np.load(RULE(s))[k] for k in ("lin", "W", "v", "scale"))) for s in (0, 1)]

    def decide(k):
        d = ev[k]["r0"].astype(float)
        return (np.mean([r.logit(d) for r in rules], axis=0) < 0).astype(int), ev[k]["t_end"], LA
    SF.write_predictions(name, out_dir, decide, "b3v2")


if __name__ == "__main__":
    ap = argparse.ArgumentParser()
    ap.add_argument("cmd", choices=("check", "train", "predict"))
    ap.add_argument("--set")
    ap.add_argument("--out")
    a = ap.parse_args()
    {"check": check, "train": train}.get(a.cmd, lambda: predict(a.set, a.out))()
