"""Phase D3 (H11): spatial decoding of the attended side and the trial-fingerprint shortcut.

Features (cache/spatial fb1 / fb5; 1-s: every other cached window, <= 15 per trial, to bound memory): filter bank [1-4, 4-8, 8-13, 13-30] Hz, causal, common57 CAR, running z.
Models
  FBCSP-LDA  per band: CSP from the training class-mean covariances (shrinkage 1e-3), 3 filter pairs ->
             normalised log-variance -> LDA on the 24 features
  Riemann-LR per band: log-Euclidean tangent vector of the (57->56 zero-sum-projected) covariance ->
             StandardScaler -> PCA(64, fitted on the training fold) -> logistic regression
Labels: attended side of the window (majority of sign(azimuth)); 0 = left, 1 = right.
Schemes
  within-trial  random 5-fold split of a subject's windows (windows of the same trial on both sides)
  LOTO          within subject; KUL-AV-GC: leave-one-trial-out (every trial holds both sides);
                other datasets: balanced leave-pair-out (one left + one right trial, <= 10 random pairs)
  LOSO          within dataset
  LODO          across the five datasets
  trial-ID      within subject, random 5-fold, predict the trial (Riemann-LR, multi-class)
Also: LODO models applied to continuous KUL-AV-GC 1-s windows (0.25-s hop) -> direction evidence scored on
the 99 GENUINE direction switches with the Phase E integrators (cache/evidence_dir, tables/spatial_switch_*).
"""
import os
import sys

import numpy as np
import pandas as pd
from joblib import Parallel, delayed
from scipy import linalg, signal
from sklearn.decomposition import PCA
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import KFold
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler

sys.path.insert(0, os.path.dirname(__file__))
from common import CACHE, CFG, DS_KEYS, SEED, TAB, boot_ci, get_logger, load_labels, load_pp, sos_bp, trial_index  # noqa: E402

log = get_logger("d3_spatial")
C = 57
IU = np.triu_indices(C)
OFF = np.where(IU[0] == IU[1], 1.0, 2.0)          # w'Cw = sum_ij w_i w_j C_ij over the upper triangle
_Q, _ = np.linalg.qr(np.eye(C) - 1.0 / C)
Q = _Q[:, : C - 1]
IU56 = np.triu_indices(C - 1)
W56 = np.where(IU56[0] == IU56[1], 1.0, np.sqrt(2.0))
BANDS = [(1, 4), (4, 8), (8, 13), (13, 30)]


def full(v):
    M = np.zeros((C, C))
    M[IU] = v
    return M + M.T - np.diag(np.diag(M))


def tangent_block(V):
    """V (n, 1653) upper triangles -> (n, 1596) log-Euclidean tangent vectors (trace-normalised)."""
    out = np.empty((len(V), len(IU56[0])), np.float32)
    for i in range(0, len(V), 2000):
        b = V[i:i + 2000].astype(np.float64)
        M = np.zeros((len(b), C, C))
        M[:, IU[0], IU[1]] = b
        M = M + np.transpose(M, (0, 2, 1)) - M * np.eye(C)[None]
        M = np.einsum("ci,ncd,dj->nij", Q, M, Q, optimize=True)
        M = M / np.trace(M, axis1=1, axis2=2)[:, None, None]
        e, U = np.linalg.eigh(M)
        e = np.maximum(e, 1e-10)
        L = np.einsum("nij,nj,nkj->nik", U, np.log(e), U, optimize=True)
        out[i:i + 2000] = L[:, IU56[0], IU56[1]] * W56
    return out


class FBCSP:
    def __init__(self, n_pairs=3):
        self.n_pairs = n_pairs

    def fit(self, V, y):
        """V (n, nb, 1653)."""
        self.W = []
        for b in range(V.shape[1]):
            A = full(V[y == 0, b].mean(0))
            B = full(V[y == 1, b].mean(0))
            A, B = A / np.trace(A), B / np.trace(B)
            S = A + B + 1e-3 * np.eye(C) * np.trace(A + B) / C
            e, W = linalg.eigh(A, S)
            W = np.c_[W[:, : self.n_pairs], W[:, -self.n_pairs:]]
            # weights on the upper-triangle vector for each filter's variance
            self.W.append(np.array([(np.outer(w, w)[IU] * OFF) for w in W.T]).T)       # (1653, 6)
        F = self.transform(V)
        self.lda = LinearDiscriminantAnalysis(solver="lsqr", shrinkage="auto").fit(F, y)
        return self

    def transform(self, V):
        F = []
        for b in range(V.shape[1]):
            var = np.maximum((V[:, b] @ self.W[b].astype(np.float32)).astype(np.float64), 1e-12)
            F.append(np.log(var / var.sum(1, keepdims=True)))
        return np.concatenate(F, 1)

    def decision_function(self, V):
        return self.lda.decision_function(self.transform(V))

    def predict(self, V):
        return (self.decision_function(V) > 0).astype(int)


class RiemannLR:
    def fit(self, T, y):
        self.m = make_pipeline(StandardScaler(), PCA(min(64, T.shape[0] - 1, T.shape[1]), random_state=SEED),
                               LogisticRegression(max_iter=1000, C=1.0)).fit(T, y)
        return self

    def decision_function(self, T):
        return self.m.decision_function(T)

    def predict(self, T):
        return self.m.predict(T)


def load(kind):
    ti = trial_index()
    V, meta = [], []
    for _, r in ti.iterrows():
        z = np.load(os.path.join(CACHE, "spatial", f"{r['key']}.npz"))
        sel = slice(None) if kind == "5" else slice(0, None, 2)      # 1-s: every other cached window (<= 15/trial)
        V.append(z[f"fb{kind}"][sel])
        lab = load_labels(r["key"])
        meta.append(pd.DataFrame(dict(key=r["key"], ds=r["ds"], subj=r["subj_uid"], trial=r["trial"],
                                      y=z[f"lab{kind}"][sel].astype(int), t=z[f"t{kind}"][sel], cond=str(lab["cond"]))))
    V = np.concatenate(V)
    meta = pd.concat(meta, ignore_index=True)
    T = np.concatenate([tangent_block(V[:, b]) for b in range(len(BANDS))], 1)
    return V, T, meta


def fit_pred(model, Xtr, ytr, Xte):
    if len(np.unique(ytr)) < 2:
        return np.full(len(Xte), np.nan), np.full(len(Xte), np.nan)
    m = (FBCSP() if model == "fbcsp" else RiemannLR()).fit(Xtr, ytr)
    return m.predict(Xte), m.decision_function(Xte)


def folds_for_subject(meta_s, scheme, rng):
    idx = meta_s.index.values
    if scheme == "within_trial":
        return [(idx[a], idx[b]) for a, b in KFold(5, shuffle=True, random_state=SEED).split(idx)]
    trials = meta_s.key.unique()
    if meta_s.ds.iloc[0] == "AVGC":
        return [(idx[(meta_s.key != k).values], idx[(meta_s.key == k).values]) for k in trials]
    tl = meta_s.groupby("key").y.agg(lambda s: int(s.mean() > 0.5))
    L_, R_ = tl[tl == 0].index.values, tl[tl == 1].index.values
    pairs = [(a, b) for a in L_ for b in R_]
    if len(pairs) > 10:
        pairs = [pairs[i] for i in rng.choice(len(pairs), 10, replace=False)]
    out = []
    for a, b in pairs:
        te = meta_s.key.isin([a, b]).values
        out.append((idx[~te], idx[te]))
    return out


def _within_one(kind, model, Xs, Ts, ms):
    """One subject; Xs/Ts/ms are that subject's rows only (index reset)."""
    ms = ms.reset_index(drop=True)
    rng = np.random.default_rng(SEED)
    res = []
    subj = ms.subj.iloc[0]
    y = ms.y.values
    for scheme in ("within_trial", "LOTO"):
        correct, n = 0, 0
        per_cond = {}
        for tr, te in folds_for_subject(ms, scheme, rng):
            p, _ = fit_pred(model, Xs[tr], y[tr], Xs[te])
            ok = p == y[te]
            correct += np.nansum(ok)
            n += np.isfinite(p).sum()
            for c, o in zip(ms.cond.values[te], ok):
                per_cond.setdefault(c, []).append(o)
        res.append(dict(kind=kind, model=model, scheme=scheme, subj=subj, ds=ms.ds.iloc[0],
                        acc=correct / max(n, 1), n_windows=int(n),
                        **{f"acc_{c}": float(np.mean(v)) for c, v in per_cond.items() if c}))
    # trial fingerprint: predict the trial ID from the same windows (random split)
    if model == "riemann" and ms.key.nunique() > 1:
        yt = pd.factorize(ms.key)[0]
        pr = np.empty(len(ms), int)
        for a_, b_ in KFold(5, shuffle=True, random_state=SEED).split(Ts):
            m = make_pipeline(StandardScaler(), PCA(min(64, len(a_) - 1), random_state=SEED),
                              LogisticRegression(max_iter=1000)).fit(Ts[a_], yt[a_])
            pr[b_] = m.predict(Ts[b_])
        res.append(dict(kind=kind, model="riemann", scheme="trialID_within", subj=subj, ds=ms.ds.iloc[0],
                        acc=float((pr == yt).mean()), n_windows=len(ms), chance=1.0 / ms.key.nunique()))
    return res


def within_subject(kind, V, T, meta, model):
    X = V if model == "fbcsp" else T
    def jobs():                       # lazy: only the dispatched subjects' slices exist at any time
        for subj, ms in meta.groupby("subj", sort=False):
            i = ms.index.values
            yield X[i], (T[i] if model == "riemann" else None), ms

    out = Parallel(n_jobs=2, max_nbytes=None, pre_dispatch="2*n_jobs")(
        delayed(_within_one)(kind, model, a, b, c) for a, b, c in jobs())
    return [r for o in out for r in o]


def _fit_pred_idx(model, Xtr, ytr, Xte, te):
    p, dv = fit_pred(model, Xtr, ytr, Xte)
    return te, p, dv


def across(kind, V, T, meta, model, scheme):
    X = V if model == "fbcsp" else T
    y = meta.y.values
    P = np.full(len(meta), np.nan)
    D = np.full(len(meta), np.nan)
    if scheme == "LOSO":
        for ds in DS_KEYS:
            di = np.flatnonzero((meta.ds == ds).values)
            Xd, yd, sd = X[di], y[di], meta.subj.values[di]
            jobs = [(np.flatnonzero(sd != s_), np.flatnonzero(sd == s_)) for s_ in np.unique(sd)]
            out = Parallel(n_jobs=2, max_nbytes=None)(delayed(_fit_pred_idx)(model, Xd[tr], yd[tr], Xd[te], di[te])
                                                      for tr, te in jobs)
            for te, p, dv in out:
                P[te], D[te] = p, dv
    else:
        for d in DS_KEYS:
            te = np.flatnonzero((meta.ds == d).values)
            tr = np.flatnonzero((meta.ds != d).values)
            _, P[te], D[te] = _fit_pred_idx(model, X[tr], y[tr], X[te], te)
    m = meta.assign(p=P, dv=D, ok=(P == y))
    rows = []
    for subj, g in m.groupby("subj"):
        rows.append(dict(kind=kind, model=model, scheme=scheme, subj=subj, ds=g.ds.iloc[0], acc=float(g.ok.mean()),
                         n_windows=len(g), **{f"acc_{c}": float(gc.ok.mean()) for c, gc in g.groupby("cond") if c}))
    return rows, m[["key", "ds", "subj", "t", "y", "dv"]].assign(kind=kind, model=model, scheme=scheme)


def avgc_continuous_evidence(models_by_kind):
    """Apply LODO(-AVGC) models to every 1-s window (0.25-s hop) of the KUL-AV-GC trials."""
    ti = trial_index()
    rows = ti[ti.ds == "AVGC"]

    def one(r):
        d = load_pp(r, "cwide", "common57")
        fs = d["fs"]
        W, H = fs, fs // 4
        starts = np.arange(0, len(d["eeg"]) - W + 1, H)
        V = np.empty((len(starts), len(BANDS), len(IU[0])), np.float32)
        for b, (lo, hi) in enumerate(BANDS):
            xb = signal.sosfilt(sos_bp(float(lo), float(hi), 4, fs), d["eeg"], axis=0)
            for i, s in enumerate(starts):
                seg = xb[s:s + W] - xb[s:s + W].mean(0)
                V[i, b] = (seg.T @ seg / W)[IU]
        out = {}
        for model, m in models_by_kind.items():
            X = V if model == "fbcsp" else np.concatenate([tangent_block(V[:, b]) for b in range(len(BANDS))], 1)
            out[model] = m.decision_function(X)
        t_end = d["t0_s"] + (starts + W) / fs
        for model, dv in out.items():
            p = os.path.join(CACHE, "evidence_dir", model, f"{r['key']}.npz")
            os.makedirs(os.path.dirname(p), exist_ok=True)
            # evidence convention of e1: d > 0 -> label 0 (left); decision_function > 0 -> right
            np.savez(p, d=(-dv).astype(np.float32), t_end=t_end.astype(np.float32), L_s=0.0)
        return r["key"]

    Parallel(n_jobs=2)(delayed(one)(r) for _, r in rows.iterrows())


if __name__ == "__main__":
    allrows, dv_all = [], []
    for kind in ("5", "1"):
        ck = os.path.join(CACHE, f"spatial_ckpt_{kind}.pkl")
        if os.path.exists(ck) and kind == "5":
            r_, d_ = pd.read_pickle(ck)
            allrows += r_
            dv_all += d_
            log.info(f"{kind}-s results loaded from checkpoint")
            continue
        n0, m0 = len(allrows), len(dv_all)
        V, T, meta = load(kind)
        log.info(f"{kind}-s windows: {len(meta)} from {meta.key.nunique()} trials; label balance {meta.y.mean():.3f}")
        for model in ("fbcsp", "riemann"):
            allrows += within_subject(kind, V, T, meta, model)
            for scheme in ("LOSO", "LODO"):
                r, m = across(kind, V, T, meta, model, scheme)
                allrows += r
                dv_all.append(m)
            log.info(f"{kind}s {model} done")
        pd.to_pickle((allrows[n0:], dv_all[m0:]), ck)
        if kind == "1":
            tr = (meta.ds != "AVGC").values
            models = {"fbcsp": FBCSP().fit(V[tr], meta.y.values[tr]), "riemann": RiemannLR().fit(T[tr], meta.y.values[tr])}
        del V, T
    df = pd.DataFrame(allrows)
    df.to_csv(os.path.join(TAB, "spatial_per_subject.csv"), index=False)
    pd.concat(dv_all).to_parquet(os.path.join(CACHE, "spatial_decisions.parquet"))
    summ = []
    for (kind, model, scheme, ds), g in df.groupby(["kind", "model", "scheme", "ds"]):
        m, lo, hi = boot_ci(g.acc.values)
        summ.append(dict(window_s=kind, model=model, scheme=scheme, ds=ds, n_subjects=len(g), median_acc=m, ci_lo=lo,
                         ci_hi=hi, chance=float(g.chance.median()) if "chance" in g and g.chance.notna().any() else 0.5))
    summ = pd.DataFrame(summ)
    summ.to_csv(os.path.join(TAB, "spatial_summary.csv"), index=False)
    log.info("\n" + summ.pivot_table(index=["window_s", "model", "ds"], columns="scheme", values="median_acc").round(3).to_string())
    cond = df[df.ds == "AVGC"].groupby(["kind", "model", "scheme"])[[c for c in df.columns if c.startswith("acc_")]].median()
    cond.to_csv(os.path.join(TAB, "spatial_avgc_by_condition.csv"))
    log.info("\n" + cond.round(3).to_string())
    avgc_continuous_evidence(models)
    log.info("continuous KUL-AV-GC direction evidence written")
