"""Follow-up 2 of test_followups.py: is the feature-drop DirectNet (sw2 + clf_p_feat_drop, tag swfd, 4 seeds already
trained) less tied to stimulus memorisation and to the stream level than DirectNet sw2? All-five models, seeds 0-1;
BMMS3 as the reference that showed neither effect.
M  memorisation: zero EEG / mismatched EEG on the models' OWN training trials (KUL, AVGC, DTU; 16 trials each, usual
   synthetic schedule); 50 % = no stimulus-only knowledge.
L  level imbalance on Carta2026 (real switches, unseen by every model): one random stream per trial L dB louder
   (L = 0, 4, 8) before dom is computed from the raw 128-Hz envelope (build_glimpse_ext convention; L = 0 is checked
   against the cached dom); frame accuracy overall and split by whether the louder stream is attended.
Output results/research/test_followups2_summary.txt (+ .csv).
"""
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 torch  # noqa: E402

import direct_classifier as DC  # noqa: E402
import eval_external as E  # noqa: E402
import test_followups as TF  # noqa: E402
from common import SEED  # noqa: E402
from common_exp import EXP, load_trial, rows_of  # noqa: E402
from decoders import _targets  # noqa: E402

RES = os.path.join(EXP, "results", "research")
ALL5 = {"directnet_sw2": ("all5_direct_clf_w10b3sw2P_s{i}.pt", DC.DirectNet),
        "directnet_swfd": ("all5_direct_clf_w10b3swfdP_s{i}.pt", DC.DirectNet),
        "bmms3": ("all5_direct_clf_w10bmms3enhP_s{i}.pt", TF.BMMS3Enhanced)}
SEEDS = (0, 1)


def net(fam, i):
    path, cls = ALL5[fam]
    m = cls(58, 3, "vlaai_ws").to(DC.DEV)
    m.load_state_dict(torch.load(os.path.join(EXP, "models", path.format(i=i)), map_location=DC.DEV, weights_only=True)); m.eval()
    return m


def part_m(rng):
    rows = []
    for ds in ("KUL", "AVGC", "DTU"):
        r = rows_of(ds); pick = r.iloc[np.sort(rng.choice(len(r), 16, replace=False))]
        trials = [(row, load_trial("eaP_n5_t60", row["key"])) for _, row in pick.iterrows()]
        sch = [TF.schedule(int(row["n"]), len(t["eeg"]), t["t0"], np.random.default_rng(SEED + i)) for i, (row, t) in enumerate(trials)]
        for fam in ALL5:
            for i in SEEDS:
                m = net(fam, i); acc = {}
                for j, (row, t) in enumerate(trials):
                    S, x = _targets(t), t["eeg"]
                    xm = np.resize(trials[(j + 1) % len(trials)][1]["eeg"], x.shape).astype(np.float32)
                    for cond, xx in (("real", x), ("zero", np.zeros_like(x)), ("mismatch", xm)):
                        lg, idx = TF.run(m, xx, S, sch[j])
                        truth = (t["att"].astype(int) ^ sch[j])[idx]
                        acc.setdefault(cond, []).append(lg * np.where(truth == 0, 1., -1.))
                for cond, v in acc.items():
                    rows.append(dict(part="M", ds=ds, family=fam, seed=i, cond=cond, acc=float((np.concatenate(v) > 0).mean() * 100)))
                del m; torch.cuda.empty_cache()
        print("M", ds, flush=True)
    return rows


def part_l(rng):
    c = os.path.join(E.EXT, "Carta2026", "cache")
    tr = E.ext_trials("Carta2026"); tr = [tr[i] for i in np.sort(rng.choice(len(tr), 60, replace=False))]
    boost = rng.integers(0, 2, len(tr))
    doms = {}
    for j, t in enumerate(tr):
        e = np.load(os.path.join(c, "env_raw", f"Audio_{int(t['key'].split('_T')[1]):03d}.npy")).astype(np.float64)
        for lv in (0, 4, 8):
            ee = e.copy(); ee[:, boost[j]] *= 10 ** (lv * 0.6 / 20)
            d = TF.BG.features(ee)["dom"][::4][32:32 + len(t["eeg"])].astype(np.float32)
            if lv == 0:
                assert np.abs(d - t["dom"]).max() < 1e-4, ("dom recomputation differs", t["key"])
            doms[(j, lv)] = d
    rows = []
    for fam in ALL5:
        for i in SEEDS:
            m = net(fam, i)
            for lv in (0, 4, 8):
                a, at, ig = [], [], []
                for j, t in enumerate(tr):
                    st = np.zeros(len(t["eeg"]), bool)
                    lg, idx = TF.run(m, t["eeg"], _targets(dict(t, dom=doms[(j, lv)])), st)
                    truth = t["att"].astype(int)[idx]
                    sg = lg * np.where(truth == 0, 1., -1.)
                    a.append(sg); at.append(sg[truth == boost[j]]); ig.append(sg[truth != boost[j]])
                f = lambda v: float((np.concatenate(v) > 0).mean() * 100)
                rows.append(dict(part="L", ds="Carta2026", family=fam, seed=i, level_db=lv, acc=f(a),
                                 acc_boost_attended=f(at), acc_boost_ignored=f(ig)))
            del m; torch.cuda.empty_cache()
    print("L done", flush=True)
    return rows


def main():
    assert os.environ.get("GLIMPSE_THR") == "norm"
    TF.setup(); rng = np.random.default_rng(1)
    df = pd.DataFrame(part_m(rng) + part_l(rng)); df.to_csv(os.path.join(RES, "test_followups2.csv"), index=False)
    M, Lv = df[df.part == "M"], df[df.part == "L"]
    txt = ["M memorisation (all-five models on their own training trials), frame accuracy %, mean of seeds 0-1:",
           M.pivot_table(index=["family", "ds"], columns="cond", values="acc").round(2).to_string(), "",
           "L level imbalance on Carta2026 (real switches, unseen), frame accuracy %, mean of seeds 0-1:"]
    for col in ("acc", "acc_boost_attended", "acc_boost_ignored"):
        txt += [f"{col}:", Lv.pivot_table(index="family", columns="level_db", values=col).round(2).to_string(), ""]
    txt = "\n".join(txt)
    open(os.path.join(RES, "test_followups2_summary.txt"), "w").write(txt + "\n"); print(txt, flush=True)


if __name__ == "__main__":
    main()
