"""Decision stage for MULTI-TARGET evidence (e.g. env, onset, glim, mask): does the +SNR1 reach H?

Needs fold pickles with per-target evidence r0f / r1f (common_exp.evidence_from_features). No retraining: everything
runs on the saved LODO evidence, scored exactly as score.py (same synthetic switches, label stamping, scorer).
Every parameter used for held-out dataset D is fitted on the OTHER four datasets' out-of-fold evidence (no test data).

1. Diagnostics per target: SNR1, correlation between targets, kurtosis, emission mismatch (train-fitted vs held-out).
2. Target weights  d = sum_f w_f (r0f - r1f):
     equal (= score.py) | lda: w = Sigma^-1 mu (optimal for Gaussian evidence = multivariate-Gaussian HMM emissions)
     | lda_shrink (Sigma shrunk 50% to its diagonal) | snr: w_f = mu_f / var_f (diagonal) | drop-one | single target
3. HMM emission variants on the equal and the best-weighted evidence:
     gauss (score.py) | temp T: LLR x T (overlapping windows are not independent) | student-t emissions (nu fitted
     on the training evidence, robust to heavy tails) | zrun: evidence / causal running sd (past only, tau 60 s)
   and p_stay 0.995 / 0.998 / 0.999.
usage: python decision_targets.py EXP [EXP ...]  -> results/research/decision_targets.csv (+ printed tables)"""
import json
import os
import sys

import numpy as np
import pandas as pd
from joblib import Parallel, delayed
from scipy import stats

import score as S
from common_exp import CFG, EXP, rows_of
from common import CACHE as EDA_CACHE, DS_KEYS, FS as FS128, SEED
from e1_switch import expand, hmm_filter, running_sd, synth_trial
from scorer import aggregate, fast_score_subject

LA = CFG["dnn"]["evidence_lookahead_s"]


def signed(ev, ds_list):
    """Per-target evidence with the attended stream as the positive class: (n, F)."""
    return np.concatenate([(e["r0f"].astype(float) - e["r1f"].astype(float)) * np.where(e["att"] == 0, 1.0, -1.0)[:, None]
                           for d in ds_list for e in ev[d].values()])


def weights(kind, X):
    mu, C = X.mean(0), np.cov(X.T)
    F = len(mu)
    if kind == "equal":
        w = np.ones(F)
    elif kind == "lda":
        w = np.linalg.solve(C, mu)
    elif kind == "lda_shrink":
        w = np.linalg.solve(0.5 * C + 0.5 * np.diag(np.diag(C)), mu)
    elif kind == "snr":
        w = mu / np.diag(C)
    elif kind.startswith("drop"):
        w = np.ones(F); w[int(kind[4:])] = 0
    elif kind.startswith("only"):
        w = np.zeros(F); w[int(kind[4:])] = 1
    return w / np.abs(w).sum()


def integ(d, P):
    mu, sd, T_ = P["mu"], P["sd"], P.get("temp", 1.0)
    if P["emis"] == "zrun":
        d = d / running_sd(d, 60.0 / 0.25)
        mu, sd = P["mu_z"], 1.0
    if P["emis"] == "t":
        ll0 = stats.t.logpdf((d - mu) / P["scale"], P["nu"])
        ll1 = stats.t.logpdf((d + mu) / P["scale"], P["nu"])
    else:
        ll0, ll1 = -0.5 * ((d - mu) / sd) ** 2, -0.5 * ((d + mu) / sd) ** 2
    return (hmm_filter(T_ * ll0, T_ * ll1, P["p_stay"]) > 0.5).astype(int)


def score_ds(ds, ev, wkind, emis, p_stay, temp):
    train = [d for d in DS_KEYS if d != ds]
    X = signed(ev, train)
    w = weights(wkind, X)
    A = X @ w
    P = dict(mu=float(A.mean()), sd=float(A.std()), emis=emis, p_stay=p_stay, temp=temp)
    if emis == "t":
        nu, _, sc = stats.t.fit(A[::7] - A.mean(), floc=0)             # thinned: overlapping windows
        P.update(nu=float(nu), scale=float(sc))
    if emis == "zrun":                                                # mean of the running-z evidence on training data
        z = [((e["r0f"].astype(float) - e["r1f"].astype(float)) @ w) for d in train for e in ev[d].values()]
        sg = [np.where(e["att"] == 0, 1.0, -1.0) for d in train for e in ev[d].values()]
        P["mu_z"] = float(np.mean(np.concatenate([x / running_sd(x, 240.0) * s for x, s in zip(z, sg)])))
    rng = np.random.default_rng(SEED + DS_KEYS.index(ds))             # identical switch schedule to score.py
    per, dd = {}, []
    for _, r in rows_of(ds).iterrows():
        T = int(r["n"])
        e = ev[ds][r["key"]]
        att = np.load(os.path.join(EDA_CACHE, "cnd", f"{r['key']}.npz"))["att_col"][:T].astype(int)
        d = (e["r0f"].astype(float) - e["r1f"].astype(float)) @ w
        t_end = e["t_end"].astype(float)
        st, d_syn, _ = synth_trial(r["key"], T, d, t_end, rng)
        guess = int(rng.integers(0, 2))
        labs = expand(integ(d_syn, P), t_end, LA, T, guess)
        per.setdefault(r["subj_uid"], []).append((labs, np.where(st, 1 - att, att)))
        dd.append((r["subj_uid"], (d * np.where(e["att"] == 0, 1.0, -1.0))[::4]))
    res = []
    for lst in per.values():
        pred, gt = np.concatenate([a for a, _ in lst]), np.concatenate([b for _, b in lst])
        trial = np.concatenate([np.full(len(a), i) for i, (a, _) in enumerate(lst)])
        res.append(fast_score_subject(pred, gt, trial, fs=FS128))
    a = aggregate(res)
    snr = np.median([np.mean(x) / np.std(x) for x in
                     [np.concatenate([v for u, v in dd if u == s]) for s in sorted({u for u, _ in dd})]])
    return dict(ds=ds, weights=wkind, emis=emis, p_stay=p_stay, temp=temp, Acc=a["Acc"], SDT=a["SDT"], H=a["H"], snr1=snr,
                w=" ".join(f"{x:+.2f}" for x in w))


def diagnostics(exp, ev, names):
    X = signed(ev, DS_KEYS)
    print(f"\n=== {exp}: per-target diagnostics (all 5 datasets pooled, 0.25-s hop) ===")
    print("target   SNR1(pooled)  kurtosis")
    for f, n in enumerate(names):
        print(f"{n:8s} {X[:, f].mean() / X[:, f].std():8.3f} {stats.kurtosis(X[::4, f]):9.2f}")
    m = X.mean(1)
    print(f"{'mean':8s} {m.mean() / m.std():8.3f} {stats.kurtosis(m[::4]):9.2f}")
    print("correlation between targets' evidence:\n", pd.DataFrame(np.corrcoef(X.T), names, names).round(2).to_string())
    print("emission mismatch (equal weights): fitted on the other 4 vs actual on the held-out dataset")
    for ds in DS_KEYS:
        tr, te = signed(ev, [d for d in DS_KEYS if d != ds]).mean(1), signed(ev, [ds]).mean(1)
        print(f"  {ds:5s} mu {tr.mean():.4f} -> {te.mean():.4f}   sd {tr.std():.3f} -> {te.std():.3f}   "
              f"mu/sd {tr.mean() / tr.std():.3f} -> {te.mean() / te.std():.3f}")


if __name__ == "__main__":
    rows = []
    for exp in sys.argv[1:]:
        ev, _ = S.load_folds(exp, "causal")
        F = next(iter(ev[DS_KEYS[0]].values()))["r0f"].shape[1]
        rj = os.path.join(EXP, "results", f"{exp}.json")
        names = (json.load(open(rj))["targets"] if os.path.exists(rj) else [f"t{i}" for i in range(F)])[:F]
        diagnostics(exp, ev, names)
        grid = [(w, "gauss", 0.998, 1.0) for w in ["equal", "lda", "lda_shrink", "snr"] + [f"drop{f}" for f in range(F)]
                + [f"only{f}" for f in range(F)]]
        for w in ("equal", "lda_shrink"):
            grid += [(w, "gauss", 0.998, t) for t in (0.5, 2.0)] + [(w, "t", 0.998, 1.0), (w, "zrun", 0.998, 1.0)]
            grid += [(w, "gauss", p, 1.0) for p in (0.995, 0.999)]
        out = Parallel(n_jobs=int(os.environ.get("SCORE_JOBS", 3)))(
            delayed(score_ds)(ds, ev, *g) for g in grid for ds in DS_KEYS)
        for r in out:
            r["exp"] = exp
        rows += out
    df = pd.DataFrame(rows)
    ren = {f"drop{f}": f"drop_{n}" for f, n in enumerate(names)} | {f"only{f}": f"only_{n}" for f, n in enumerate(names)}
    df["weights"] = df.weights.replace(ren)
    os.makedirs(os.path.join(EXP, "results", "research"), exist_ok=True)
    df.to_csv(os.path.join(EXP, "results", "research", os.environ.get("DT_OUT", "decision_targets") + ".csv"), index=False)
    g = df.groupby(["weights", "emis", "p_stay", "temp"], sort=False)[["H", "Acc", "SDT", "snr1"]].mean().round(4)
    print("\n=== mean over held-out datasets and experiments ===\n", g.to_string())
    pv = df.groupby(["weights", "emis", "p_stay", "temp", "ds"], sort=False).H.mean().unstack().round(3)
    print("\n=== H per held-out dataset ===\n", pv.to_string())
    print(f"\nLDA weights per held-out dataset ({' '.join(names)}):\n",
          df[(df.weights == "lda") & (df.exp == sys.argv[1])][["ds", "w"]].to_string(index=False))
