"""Submission step 2: evidence of ONE saved all-five model on every trial of a converted set (sub_convert.py), with the
inference code paths that produced the AASD / Carta evidence of the main ensemble (eq3 = 4 + 4 + 4):

  dec   vlaai_ws decoder, targets env/onset/dom, models/all5_vlaai_ws_bestP_s<i>.pt   (eval_external.evidence)
  clf   DirectNet 10 s, 3 blocks, sw2,            models/all5_direct_clf_w10b3sw2P_s<i>.pt  (eval_external_clf)
  bmms  BMMS3 (BMMS3Enhanced),                    models/all5_direct_clf_w10bmms3enhP_s<i>.pt (eval_external_clf
        with DirectNet replaced, as bmms_3feature_runner.py --phase external)
All with GLIMPSE_THR=norm (as the external runs). Each trial is decoded on its own and causally.
Weights are loaded with weights_only=True. One model family per process (the families set different global options).

--check: run on AASD instead and compare with the stored evidence of the same model
(aasd_c1_bestP / ext_clf_AASD_w10b3sw2P / ext_clf_AASD_w10bmms3enhP _evidence_s<i>.pkl); exit 1 if they differ.
Output results/submission/<SET>/<model>_s<i>.pkl (same layout as the stored external evidence).
usage: GLIMPSE_THR=norm python -I sub_evidence.py --model dec|clf|bmms --seed I --set TEST_DLAAD [--check]
"""
import argparse
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 torch  # noqa: E402

import decoders  # noqa: E402
import eval_external as E  # noqa: E402
from common_exp import CFG, EXP, FS  # noqa: E402
from decoders import D, _targets  # noqa: E402

PREP = "eaP_n5_t60"
TARGETS = ["env", "onset", "dom"]
WEIGHTS = {"dec": "all5_vlaai_ws_bestP_s{i}.pt", "clf": "all5_direct_clf_w10b3sw2P_s{i}.pt",
           "bmms": "all5_direct_clf_w10bmms3enhP_s{i}.pt"}
STORED = {"dec": "aasd_c1_bestP_evidence_s{i}.pkl", "clf": "ext_clf_AASD_w10b3sw2P_evidence_s{i}.pkl",
          "bmms": "ext_clf_AASD_w10bmms3enhP_evidence_s{i}.pkl"}


def load(path):
    return torch.load(path, map_location=decoders.DEV, weights_only=True)


def dec_evidence(seed, ds):
    decoders.apply_overrides([])
    D["fm_held"] = "ALL"
    D.update(train_seed=seed, targets=TARGETS, augment={**CFG["dnn_augment"], **CFG["aug_variants"]["cluster"]})
    E.CONF.update(targets=TARGETS, prep=PREP, decoder="vlaai_ws")
    dec = decoders.make("vlaai_ws", 58, E.log)
    dec.net.load_state_dict(load(os.path.join(EXP, "models", WEIGHTS["dec"].format(i=seed))))
    dec.net.eval()
    with torch.no_grad():
        return E.evidence(dec, ds)


def clf_evidence(model, seed, ds):
    import direct_classifier as DC
    DC.W = int(10 * FS)
    DC.POS0, DC.LAB = DC.W - DC.FUT - DC.HOP, DC.W - FS
    D.update(targets=TARGETS, train_seed=seed, augment={**CFG["dnn_augment"], **CFG["aug_variants"]["cluster"]})
    decoders.apply_overrides(["model_blocks=3"] + (["infer_bs=8"] if model == "bmms" else []), ["clf_p_switch2=0.5"])
    D["fm_held"] = "ALL"
    E.CONF.update(prep=PREP, targets=TARGETS)
    if model == "bmms":
        from bmms_3feature_model import BMMS3Enhanced
        net = BMMS3Enhanced(58, len(TARGETS), "vlaai_ws")
    else:
        net = DC.DirectNet(58, len(TARGETS), "vlaai_ws")
    net = net.to(DC.DEV)
    net.load_state_dict(load(os.path.join(EXP, "models", WEIGHTS[model].format(i=seed))))
    net.eval()
    ev = {}
    with torch.no_grad():
        for tr in E.ext_trials(ds):
            DC.fm_attach(tr, net)
            lg, starts = DC.logits(net, DC.wide(tr), _targets(tr))
            truth = tr["att"].astype(int)[np.maximum(starts - DC.HOP, 0)]
            ev[tr["key"]] = dict(r0=lg / 2, r1=-lg / 2, att=truth.astype(np.int8), t_end=(starts / FS + tr["t0"]).astype(np.float32),
                                 att128=tr["att128"], n128=tr["n128"], subject=tr["subject"])
    return ev


def compare(model, seed, ev):
    ref = pickle.load(open(os.path.join(EXP, "results", "research", STORED[model].format(i=seed)), "rb"))
    assert set(ref) == set(ev), "trial keys differ"
    f = "r0f" if model == "dec" else "r0"
    diff = np.concatenate([np.abs(ev[k][f].astype(float) - ref[k][f].astype(float)).ravel() for k in ref])
    t = max(float(np.abs(ev[k]["t_end"].astype(float) - ref[k]["t_end"].astype(float)).max()) for k in ref)
    print(f"check {model} s{seed} on AASD vs {STORED[model].format(i=seed)}: max |{f} diff| {diff.max():.2e}, "
          f"mean {diff.mean():.2e}, max |t_end diff| {t:.1e}", flush=True)
    return diff.max() < 1e-2 and diff.mean() < 1e-4 and t < 1e-4


if __name__ == "__main__":
    ap = argparse.ArgumentParser()
    ap.add_argument("--model", required=True, choices=sorted(WEIGHTS))
    ap.add_argument("--seed", type=int, required=True)
    ap.add_argument("--set", default="AASD")
    ap.add_argument("--check", action="store_true")
    a = ap.parse_args()
    assert os.environ.get("GLIMPSE_THR") == "norm", "the external evidence runs use GLIMPSE_THR=norm"
    torch.set_num_threads(2)
    ds = "AASD" if a.check else a.set
    ev = dec_evidence(a.seed, ds) if a.model == "dec" else clf_evidence(a.model, a.seed, ds)
    if a.check:
        sys.exit(0 if compare(a.model, a.seed, ev) else 1)
    out = os.path.join(EXP, "results", "submission", ds)
    os.makedirs(out, exist_ok=True)
    pickle.dump(ev, open(os.path.join(out, f"{a.model}_s{a.seed}.pkl"), "wb"))
    print(f"{ds}: {a.model} s{a.seed} evidence for {len(ev)} trials -> {out}", flush=True)
