"""Within-trial self-adaptation (allowed by Terms 6.3: parameter updates within the trial under test, causal).

On top of the fused evidence of the generic models (decoders + direct classifiers) a small LINEAR backward decoder is
learnt from scratch inside every trial, from the pipeline's own decisions:
  target      s(t) = env_0(t) - env_1(t)  (the two streams as presented, synthetic switches included)
  pseudo label c(t) = 1 - 2 P(t) in [-1, 1], P = filtering posterior of stream 1 at the decision that labels t
  decoder     w = (Rxx + lam tr(Rxx)/p I)^-1 rxy on EEG lags 0..375 ms after the stimulus sample, with Rxx, rxy
              accumulated over all samples labelled so far in this trial (target c(t) s(t)); refitted every second
  evidence    e_j = corr(X w, s) over the last 5 s available at decision j, with the w that was fitted BEFORE that
              window started (no in-sample correlation), added to the generic evidence:
              d_j = d_generic_j + beta * min(1, training time / 120 s) * e_j / 0.1  -> same HMM forward filter.
Everything restarts in every trial and uses data < a + 0.75 s for the decision at hop start a (classifier timing).
--oracle: true labels instead of pseudo labels (upper bound of what a within-trial linear decoder can add).
Reported per held-out dataset (LODO evidence, synthetic switches, official scorer): H / Acc / SDT, plus the accuracy
of sign(e) alone once >= 3 min of the trial have been seen. Long trials only matter (KUL 6 min, UHD 6.5 min, AVGC 10 min).
usage: python adapt_within_trial.py  -> results/research/adapt_within_trial.csv"""
import os

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

import fuse_general as FG
from common_exp import EXP, FS, load_trial, rows_of
from common import CACHE as EDA_CACHE, DS_KEYS, SEED
from e1_switch import expand, synth_trial
from fuse_evidence import LA
from hmm_bank import C, D, W, subjects_score

LAGS = np.arange(0, 13, 2)                       # EEG samples after the stimulus sample (0 .. 375 ms)
MAXLAG, HOP, WAD, SIG, RAMP = int(LAGS.max()), 8, 5 * FS, 0.1, 120.0
CONFIGS = [("generic only", dict(beta=0.0)), ("oracle labels, beta 0.5", dict(beta=0.5, oracle=True)),
           ("pseudo labels, beta 0.3", dict(beta=0.3)), ("pseudo labels, beta 0.6", dict(beta=0.6)),
           ("pseudo labels, beta 0.6, ridge 0.2", dict(beta=0.6, lam=0.2))]


def lagged(x):
    T, Cn = x.shape
    out = np.zeros((T, Cn * len(LAGS)), np.float32)
    for i, l in enumerate(LAGS):
        out[:T - l, i * Cn:(i + 1) * Cn] = x[l:]
    return out


def run_trial(x, S, d_base, starts, mu, sd, p_stay=0.998, beta=0.5, lam=1.0, truth=None, oracle=False):
    """Decisions (n,) and adaptive evidence (n,), trained extent in s (n,). truth (T,): attended column (oracle only)."""
    T = len(x)
    Xl, s = lagged(x), (S[:, 0] - S[:, 1]).astype(np.float64)
    p = Xl.shape[1]
    Rxx, rxy = np.zeros((p, p)), np.zeros(p)
    hist, hi_ptr = [], -1                                     # (targets used: t < extent, w)
    post = np.array([0.5, 0.5])
    n = len(d_base)
    P, e, ext = np.empty(n), np.zeros(n), np.zeros(n)
    for j in range(n):
        a = int(starts[j])
        hi = min(a + 24 - MAXLAG, T - MAXLAG)                 # targets t < hi are reconstructable with EEG < a + 24
        lo = hi - WAD
        if beta > 0 and lo >= 0 and hist:
            while hi_ptr + 1 < len(hist) and hist[hi_ptr + 1][0] <= lo - MAXLAG - 1:
                hi_ptr += 1
            if hi_ptr >= 0:
                y = Xl[lo:hi] @ hist[hi_ptr][1]
                sy, ss = y.std(), s[lo:hi].std()
                if sy > 0 and ss > 0:
                    e[j] = np.mean((y - y.mean()) * (s[lo:hi] - s[lo:hi].mean())) / (sy * ss)
                ext[j] = hist[hi_ptr][0] / FS
        d = d_base[j] + beta * min(1.0, ext[j] / RAMP) * e[j] / SIG
        if j > 0:
            p0 = post[0] * p_stay + post[1] * (1 - p_stay)
            post = np.array([p0, 1 - p0])
        ll = np.array([-0.5 * ((d - mu) / sd) ** 2, -0.5 * ((d + mu) / sd) ** 2])
        post = post * np.exp(ll - ll.max())
        post = post / post.sum()
        P[j] = post[1]
        if beta > 0:
            b0, b1 = max(a - HOP, 0), min(a, T - MAXLAG)       # the samples this decision labels
            if b1 > b0:
                c = (1.0 - 2.0 * truth[b0:b1]) if oracle else np.full(b1 - b0, 1.0 - 2.0 * P[j])
                Xb = Xl[b0:b1].astype(np.float64)
                Rxx += Xb.T @ Xb
                rxy += Xb.T @ (c * s[b0:b1])
            if j % 4 == 3 and b1 > 0:
                hist.append((b1, np.linalg.solve(Rxx + (lam * np.trace(Rxx) / p + 1e-9) * np.eye(p), rxy).astype(np.float32)))
    return (P > 0.5).astype(int), e, ext


def score_fold(held, ev, cfg):
    A = np.concatenate([d * np.where(att == 0, 1.0, -1.0) for ds in DS_KEYS if ds != held for d, att, _ in ev[ds].values()])
    mu, sd = float(A.mean()), float(A.std())
    rng = np.random.default_rng(SEED + DS_KEYS.index(held))
    per, hit = {}, []
    for _, r in rows_of(held).iterrows():
        T128 = int(r["n"])
        d, att_w, t_end = ev[held][r["key"]]
        tr = load_trial("mat_reac", r["key"])
        att128 = np.load(os.path.join(EDA_CACHE, "cnd", f"{r['key']}.npz"))["att_col"][:T128].astype(int)
        st128, _, _ = synth_trial(r["key"], T128, d, t_end, rng)
        guess = int(rng.integers(0, 2))
        x = tr["eeg"]
        st = st128[np.minimum(T128 - 1, np.round((np.arange(len(x)) / FS + tr["t0"]) * 128).astype(int))]
        S = np.where(st[:, None], tr["env"][:, ::-1], tr["env"])[:len(x)]
        truth = (tr["att"].astype(int)[:len(x)] ^ st).astype(float)
        starts = np.round((t_end - tr["t0"]) * FS).astype(int)
        dec, e, ext = run_trial(x, S, d, starts, mu, sd, truth=truth, **cfg)
        per.setdefault(r["subj_uid"], []).append((expand(dec, t_end, LA, T128, guess), np.where(st128, 1 - att128, att128)))
        m = ext >= 180
        if m.any():
            hit.append(((e[m] > 0) == (att_w[m] == 0))[::4])
    a = subjects_score(per)
    return dict(H=a["H"], Acc=a["Acc"], SDT=a["SDT"], adapt_acc_after_3min=float(np.mean(np.concatenate(hit))) if hit else np.nan,
                steps_after_3min=int(sum(len(h) for h in hit)))


if __name__ == "__main__":
    Z, meta = FG.build([D, D + "_s1"], [C, C + "_s1", W, W + "_s1"])
    x = 0.3
    ev = {h: {ds: {k: ((1 - x) * Z[h][0][ds][k] + x * Z[h][1][ds][k], meta[ds][k][0], meta[ds][k][1].astype(float)) for k in meta[ds]}
              for ds in DS_KEYS} for h in DS_KEYS}
    jobs = [(n, c, h) for n, c in CONFIGS for h in DS_KEYS]
    out = Parallel(n_jobs=int(os.environ.get("SCORE_JOBS", 3)))(delayed(score_fold)(h, ev[h], c) for n, c, h in jobs)
    df = pd.DataFrame([dict(config=n, ds=h, **r) for (n, c, h), r in zip(jobs, out)])
    df.to_csv(os.path.join(EXP, "results", "research", "adapt_within_trial.csv"), index=False)
    pd.set_option("display.width", 220)
    order = [n for n, _ in CONFIGS]
    for m in ("H", "Acc", "SDT", "adapt_acc_after_3min"):
        g = df.pivot(index="config", columns="ds", values=m).reindex(order)
        g["mean"] = g.mean(1)
        print(f"\n=== {m} ===\n", g.round(4 if m != "SDT" else 1).to_string())
