"""Build per-trial caches for both preprocessing pipelines (58-ch baseline montage, 32 Hz).
Normalisation per prep: norm=trial -> full-trial z-score (loadSubject.m), norm=running -> causal running z.
cache/<prep>/<key>.npz: eeg (T,58) float32, env (T,2) float32 (column 0 = majority-attended), att (T,) int8, t0."""
import os
import sys

import numpy as np
from joblib import Parallel, delayed

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from common_exp import CFG, cache_path, resolve_read  # noqa: E402
from common import CFG as ECFG, FS as FS_RAW, B64U, MONTAGES, b64_idx, load_eeg, load_env, load_labels, load_pp, trial_index  # noqa: E402
from scipy.signal import detrend  # noqa: E402


def fft_bandpass(x, fs, lo, hi, taper=0.25, pad_s=4.0):
    """Zero-phase FFT band-pass of a whole trial (NON-causal): linear detrend, reflect-pad pad_s on both sides,
    rfft, keep lo..hi Hz with raised-cosine edges of width `taper` Hz (0.25 Hz: EEG 1/f power below 1 Hz is large), irfft, crop."""
    x = detrend(np.asarray(x, np.float64), axis=0, type="linear")
    p = int(pad_s * fs)
    xp = np.pad(x, ((p, p), (0, 0)), mode="reflect")
    f = np.fft.rfftfreq(len(xp), 1 / fs)
    g = np.zeros_like(f)
    g[(f >= lo) & (f <= hi)] = 1.0
    a = (f > lo - taper) & (f < lo)
    g[a] = 0.5 - 0.5 * np.cos(np.pi * (f[a] - (lo - taper)) / taper)
    b = (f > hi) & (f < hi + taper)
    g[b] = 0.5 + 0.5 * np.cos(np.pi * (f[b] - hi) / taper)
    y = np.fft.irfft(np.fft.rfft(xp, axis=0) * g[:, None], n=len(xp), axis=0)
    return y[p:p + len(x)]


def load_fft(row, montage, band=(1.0, 9.0)):
    """'fft' pipeline: raw 128 Hz -> FFT band-pass 1-9 Hz (0.25-Hz cosine edges) on EEG and envelopes -> 32 Hz
    (every 4th sample; band-limited to < 9.5 Hz so no aliasing) -> montage -> CAR over present channels -> 1-s edge trim.
    Normalisation is applied by the caller (same as the other preps)."""
    key = row["key"]
    src, present = b64_idx(row)
    raw = load_eeg(key)
    x = np.zeros((raw.shape[0], 64))
    x[:, present] = np.asarray(raw[:, src[present]], dtype=np.float64)
    e = load_env(key).astype(np.float64)
    q = FS_RAW // CFG["fs"]
    x = fft_bandpass(x, FS_RAW, band[0], band[1])[::q]
    e = fft_bandpass(e, FS_RAW, band[0], band[1])[::q]
    x[:, ~present] = 0.0
    sel = np.array([B64U.index(c) for c in MONTAGES[montage]])
    x, pres = x[:, sel], present[sel]
    x[:, pres] -= x[:, pres].mean(1, keepdims=True)
    lab = load_labels(key)
    att = lab["att_col"][::q][: len(x)]
    n = min(len(x), len(e), len(att))
    k = int(round(ECFG["pipeline"]["edge_trim_s"] * CFG["fs"]))
    return dict(eeg=x[k:n - k], env=e[k:n - k], fs=CFG["fs"], att_col=att[k:n - k], t0_s=k / CFG["fs"])


def zscore_trial(x):
    """MATLAB zscore along time: N-1 std, a zero-variance column (zero-filled channel) is divided by 1."""
    x = np.asarray(x, np.float64)
    sd = x.std(0, ddof=1)
    sd[sd == 0] = 1.0
    return (x - x.mean(0)) / sd


def causal_ea(x, fs, tau_s=60.0, block_s=1.0, shrink=0.1, R0=None, return_R=False, n0=None):
    """Causal running Euclidean alignment (whitening by R^-1/2). R = exponentially weighted spatial covariance of
    PAST 1-s blocks only (time constant tau_s), shrunk towards the identity; block k is whitened with R estimated
    from blocks < k (R starts at the identity: data are z-scored). Zero (absent) channels are left untouched.
    Not an exempted whole-trial method, hence causal (1-s lookahead rule).
    n0 (s): weight of the start value R0 / identity, as seconds of data. None = tau_s (the original recursion). A small n0
    gives a fast warm-up: a plain cumulative mean of the trial's own blocks at first, exponential forgetting (tau_s) later."""
    x = np.asarray(x, np.float64)
    pres = x.std(0) > 0
    y = x.copy()
    xp = x[:, pres]
    C = xp.shape[1]
    R = np.eye(C) if R0 is None or R0.shape != (C, C) else R0.copy()   # R0: carried over from earlier trials
    a = block_s / tau_s
    N = None if n0 is None else n0 / block_s
    B = int(block_s * fs)
    for s0 in range(0, len(xp), B):
        blk = xp[s0:s0 + B]
        Rs = (1 - shrink) * R + shrink * np.trace(R) / C * np.eye(C)
        w, V = np.linalg.eigh(Rs)
        y[s0:s0 + B, pres] = blk @ (V * (1.0 / np.sqrt(np.maximum(w, 1e-8)))) @ V.T
        Cb = (blk.T @ blk) / max(len(blk), 1)
        if n0 is None:
            R = (1 - a) * R + a * Cb
        else:                                             # weighted mean with effective count N (in blocks), N -> tau
            N1 = (1 - a) * N + 1.0
            R, N = ((1 - a) * N * R + Cb) / N1, N1
    return (y, R) if return_R else y


def causal_tw(x, fs, order=6, tau_s=60.0, block_s=1.0, shrink=0.05, strength=1.0, state=None, return_state=False,
              recolor_acf=None):
    """Causal TEMPORAL whitening per channel: adaptive linear-prediction error filter.
    y(t) = (x(t) - strength * sum_k a_k x(t-k)) / sd_err, with the AR(order) coefficients a of every channel solved
    (Yule-Walker, diagonal loading shrink * r0) from the exponentially weighted autocovariances r_0..r_order of PAST
    1-s blocks only (time constant tau_s; `state` = r carried across a subject's trials). Block k is filtered with
    coefficients estimated from blocks < k; the first block of a subject is unfiltered (r = [1, 0, ...]: data are
    z-scored). sd_err = sqrt of the predicted error variance (from the same past statistics), so the output has about
    unit variance. Flattens each channel's spectrum inside the pass band (1/f slope, alpha peak), which differs between
    subjects and datasets. Filter memory restarts at a trial start (zeros). Absent (zero) channels stay zero.
    Not an exempted whole-trial method, hence strictly causal.
    recolor_acf (autocorrelation at lags 1..order of a CANONICAL spectrum): spectral STANDARDISATION instead of
    whitening: the whitened signal is re-coloured by the FIXED all-pole filter of that canonical AR model (same
    shrinkage), so every channel of every recording gets the same canonical spectral shape. Recordings whose spectrum is
    already canonical come out nearly unchanged, atypical ones (e.g. KUL-AV-GC) are pulled to the canonical shape."""
    x = np.asarray(x, np.float64)
    T, C = x.shape
    p, B, al = int(order), int(block_s * fs), block_s / tau_s
    r = np.zeros((C, p + 1)) if state is None else state.copy()
    if state is None:
        r[:, 0] = 1.0
    xp = np.vstack([np.zeros((p, C)), x])                       # p zeros of filter memory before the trial
    y = np.empty_like(x)
    ii = np.abs(np.arange(p)[:, None] - np.arange(p)[None, :])
    for s0 in range(0, T, B):
        n = min(B, T - s0)
        seg = xp[s0:s0 + n + p]                                 # block with its p preceding samples
        ok = r[:, 0] > 1e-12
        a = np.zeros((C, p))
        if ok.any():
            R = r[ok][:, ii] + shrink * r[ok, :1, None] * np.eye(p)
            a[ok] = np.linalg.solve(R, r[ok, 1:, None])[..., 0]
        a *= strength
        pred = sum(seg[p - k:p - k + n] * a[:, k - 1] for k in range(1, p + 1))
        ev = r[:, 0] - 2 * (a * r[:, 1:]).sum(1) + np.einsum("ci,cij,cj->c", a, r[:, ii], a)   # error variance (past)
        y[s0:s0 + n] = (seg[p:] - pred) / np.sqrt(np.where(ok, np.maximum(ev, 1e-6 * r[:, 0]), 1.0))
        rb = np.stack([(seg[p:] * seg[p - k:p - k + n]).mean(0) for k in range(p + 1)], 1)
        r = (1 - al) * r + al * rb
    if recolor_acf is not None:
        from scipy.signal import lfilter
        rc = np.concatenate([[1.0], np.asarray(recolor_acf, float)[:p]])
        ac = np.linalg.solve(rc[ii] + shrink * np.eye(p), rc[1:])
        evc = rc[0] - 2 * ac @ rc[1:] + ac @ rc[ii] @ ac
        y = lfilter([np.sqrt(evc)], np.concatenate([[1.0], -ac]), y, axis=0)     # causal, zero state at trial start
    y[:, x.std(0) == 0] = 0.0
    return (y, r) if return_state else y


BEQ_BANDS = ((1.0, 2.5), (2.5, 5.0), (5.0, 9.0))


def causal_band_eq(x, fs, state=None, tau_s=60.0, block_s=1.0):
    """F1, causal spectral equalisation: split each channel into BEQ_BANDS with CAUSAL Butterworth band-passes,
    scale every band of every channel by 1/sqrt(its running power) estimated from PAST 1-s blocks only (EW, tau_s;
    state carried across a subject's trials), then sum the bands. Equalises the within-band spectral shape
    (1/f slope, alpha leakage near 9 Hz) that differs between datasets. Absent (zero) channels stay zero."""
    from scipy.signal import butter, sosfilt
    x = np.asarray(x, np.float64)
    pres = x.std(0) > 0
    C, B = x.shape[1], int(block_s * fs)
    bands = [sosfilt(butter(2, [lo, hi], "bandpass", fs=fs, output="sos"), x, axis=0) for lo, hi in BEQ_BANDS]
    Pw = np.full((len(bands), C), 1.0 / len(bands)) if state is None else state.copy()
    a = block_s / tau_s
    y = np.zeros_like(x)
    for s0 in range(0, len(x), B):
        for i, b in enumerate(bands):
            blk = b[s0:s0 + B]
            y[s0:s0 + B] += blk / np.sqrt(np.maximum(Pw[i], 1e-6))
            Pw[i] = (1 - a) * Pw[i] + a * (blk ** 2).mean(0)
    y[:, ~pres] = 0.0
    return y / np.sqrt(len(bands)), Pw


def gamma_envelope(row, n_out, k_trim):
    """E2: causal 30-40 Hz envelope per montage channel from the RAW 128 Hz EEG: CAR -> causal band-pass 30-40 Hz
    (Butterworth 4) -> |.| -> causal low-pass 8 Hz -> every 4th sample (32 Hz) -> same 1-s edge trim as the other
    preps -> causal running robust z (EDA running_z; no second full-trial z-score)."""
    from scipy.signal import butter, sosfilt
    from common import B64U, MONTAGES, b64_idx, load_eeg, running_z
    src, present = b64_idx(row)
    raw = load_eeg(row["key"])
    x = np.zeros((raw.shape[0], 64))
    x[:, present] = np.asarray(raw[:, src[present]], dtype=np.float64)
    sel = np.array([B64U.index(c) for c in MONTAGES[CFG["montage"]]])
    x, pres = x[:, sel], present[sel]
    x[:, pres] -= x[:, pres].mean(1, keepdims=True)
    fs_raw = 128
    g = np.abs(sosfilt(butter(4, [30, 40], "bandpass", fs=fs_raw, output="sos"), x, axis=0))
    g = sosfilt(butter(2, 8, "lowpass", fs=fs_raw, output="sos"), g, axis=0)[::fs_raw // CFG["fs"]]
    g = running_z(g, CFG["fs"])
    g[:, ~pres] = 0.0
    g = g[k_trim:k_trim + n_out]
    if len(g) < n_out:
        g = np.vstack([g, np.zeros((n_out - len(g), g.shape[1]))])
    return g


def ea_prior(P, x):
    """Start value of the EA covariance: None (identity) or the fixed population prior of build_ea_prior.py, cut to
    the channels present in this recording (prep key ea_prior)."""
    if not P.get("ea_prior"):
        return None
    R = np.load(resolve_read(os.path.join("cache", f"ea_prior_{P['ea_prior']}.npy")))
    pres = np.asarray(x).std(0) > 0
    return R[np.ix_(pres, pres)]


def trial_order(rows):
    """Recording order of a subject's trials: orig_pos when the dataset stores it (>= 0), else the trial number."""
    from common import load_labels
    def pos(r):
        op = load_labels(r["key"]).get("orig_pos")
        v = int(np.ravel(op)[0]) if op is not None and np.size(op) else -1
        return (v if v >= 0 else 10 ** 6, int(r["trial"]))
    return sorted(rows, key=pos)


def build_subject_carry(rows, prep):
    """Causal EA with the covariance estimate CARRIED across the subject's trials in recording order (a device
    keeps adapting to its user). Still causal: each trial only uses earlier samples of itself and earlier trials."""
    P = CFG["preps"][prep]
    R, BQ, TW, n = None, None, None, 0
    for row in trial_order(rows):
        p = cache_path(prep, row["key"])
        d = load_pp(row, P["variant"], CFG["montage"], do_car=True, do_runz=False, do_trim=True)
        d["eeg"], d["env"] = zscore_trial(d["eeg"]), zscore_trial(d["env"])
        if P.get("band_eq"):
            d["eeg"], BQ = causal_band_eq(d["eeg"], CFG["fs"], state=BQ)
        if P.get("tw_order"):                             # causal temporal whitening per channel, before the spatial EA
            d["eeg"], TW = causal_tw(d["eeg"], CFG["fs"], order=P["tw_order"], strength=P.get("tw_strength", 1.0),
                                     state=TW, return_state=True, recolor_acf=P.get("tw_recolor_acf"))
        d["eeg"], R = causal_ea(d["eeg"], CFG["fs"], tau_s=P.get("ea_tau", 60.0), R0=R, return_R=True)
        if not os.path.exists(p):
            os.makedirs(os.path.dirname(p), exist_ok=True)
            np.savez(p, eeg=d["eeg"].astype(np.float32), env=d["env"].astype(np.float32),
                     att=d["att_col"].astype(np.int8), t0=d["t0_s"])
            n += 1
    return n


def causal_phase(x, fs, bands=((1.0, 4.0), (4.0, 8.0)), taps=31):
    """E1: CAUSAL instantaneous phase per band: causal Butterworth band-pass, then an analytic signal from a causal
    FIR Hilbert transformer (taps = 2D+1, equiripple) paired with the band signal delayed by D samples:
    z(t) = x_b(t - D) + i H{x_b}(t), i.e. the phase of time t - D computed from samples <= t (a fixed delay, no
    lookahead). Returns [cos(phase), sin(phase)] per band and channel -> (T, 4 C) for two bands."""
    from scipy.signal import butter, remez, sosfilt, lfilter
    D = taps // 2
    h = remez(taps, [0.6, fs / 2 - 0.6], [1.0], type="hilbert", fs=fs)
    out = []
    for lo, hi in bands:
        xb = sosfilt(butter(2, [lo, hi], "bandpass", fs=fs, output="sos"), x, axis=0)
        im = -lfilter(h, [1.0], xb, axis=0)               # remez "hilbert" gives +90 deg; analytic signal needs -90
        re = np.vstack([np.zeros((D, x.shape[1])), xb[:-D]])
        mag = np.sqrt(re ** 2 + im ** 2) + 1e-9
        c, s_ = re / mag, im / mag
        c[:, x.std(0) == 0] = 0.0
        s_[:, x.std(0) == 0] = 0.0
        out += [c, s_]
    return np.hstack(out)


def build_phase(row, prep):
    """prep with 'phase_of': base prep cache + causal cos/sin phase channels (delta, theta) of its EEG."""
    P = CFG["preps"][prep]
    p = cache_path(prep, row["key"])
    if os.path.exists(p):
        return 0
    z = np.load(cache_path(P["phase_of"], row["key"]))
    ph = causal_phase(z["eeg"].astype(np.float64), CFG["fs"])
    os.makedirs(os.path.dirname(p), exist_ok=True)
    np.savez(p, eeg=np.hstack([z["eeg"], ph.astype(np.float32)]), env=z["env"], att=z["att"], t0=z["t0"])
    return 1


def build_gamma(row, prep):
    """prep with 'gamma_of': copy the base prep's cache and append the 58 causal gamma-envelope channels."""
    P = CFG["preps"][prep]
    p = cache_path(prep, row["key"])
    if os.path.exists(p):
        return 0
    z = np.load(cache_path(P["gamma_of"], row["key"]))
    k = int(round(float(z["t0"]) * CFG["fs"]))
    g = gamma_envelope(row, len(z["eeg"]), k)
    os.makedirs(os.path.dirname(p), exist_ok=True)
    np.savez(p, eeg=np.hstack([z["eeg"], g.astype(np.float32)]), env=z["env"], att=z["att"], t0=z["t0"])
    return 1


def build(row, prep):
    p = cache_path(prep, row["key"])
    if os.path.exists(p):
        return 0
    P = CFG["preps"][prep]
    if P["variant"] == "fft":
        assert P["norm"] == "trial"
        d = load_fft(row, CFG["montage"], tuple(P.get("band", (1.0, 9.0))))
    else:
        d = load_pp(row, P["variant"], CFG["montage"], do_car=True, do_runz=P["norm"] == "running", do_trim=True)
    if P["norm"] == "trial":
        # statistics over the trimmed trial, so the 1-s filter warm-up of the causal pipeline cannot inflate them
        d["eeg"], d["env"] = zscore_trial(d["eeg"]), zscore_trial(d["env"])
    if P.get("ea") == "causal":
        d["eeg"] = causal_ea(d["eeg"], CFG["fs"], tau_s=P.get("ea_tau", 60.0), R0=ea_prior(P, d["eeg"]), n0=P.get("ea_n0"),
                             shrink=P.get("ea_shrink", 0.1))
    assert d["fs"] == CFG["fs"]
    os.makedirs(os.path.dirname(p), exist_ok=True)
    np.savez(p, eeg=d["eeg"].astype(np.float32), env=d["env"].astype(np.float32), att=d["att_col"].astype(np.int8),
             t0=d["t0_s"])
    return 1


if __name__ == "__main__":
    ti = trial_index()
    for prep in (sys.argv[1:] or CFG["preps"]):
        if CFG["preps"][prep].get("phase_of"):
            n = Parallel(n_jobs=4)(delayed(build_phase)(r, prep) for _, r in ti.iterrows())
            print(prep, "built", sum(n), "of", len(ti))
            continue
        if CFG["preps"][prep].get("gamma_of"):
            n = Parallel(n_jobs=4)(delayed(build_gamma)(r, prep) for _, r in ti.iterrows())
            print(prep, "built", sum(n), "of", len(ti))
            continue
        if CFG["preps"][prep].get("ea_carry"):
            subs = [list(g.to_dict("records")) for _, g in ti.groupby("subj_uid")]
            n = Parallel(n_jobs=4)(delayed(build_subject_carry)(rs, prep) for rs in subs)
            print(prep, "built", sum(n), "of", len(ti))
            continue
        n = Parallel(n_jobs=4)(delayed(build)(r, prep) for _, r in ti.iterrows())
        print(prep, "built", sum(n), "of", len(ti))
