"""Convert AASD (Sci. Data 2026, Zenodo 17413336) to the decoder_lodo cache format, with the compliant pipeline
of the best configuration (prep 'mat_reac'):

  EEG  : processed MAT (62 ch = CNT order minus M1/M2, 128 Hz, mastoid reference) -> filtfilt Butterworth order 2,
         1-9 Hz -> 32 Hz -> the 58-channel montage (by name) -> CAR -> full-trial z-score (once) -> causal running
         Euclidean alignment carried across the subject's trials in recording order -> 1-s edge trim
  ENV  : stereo wav (left channel = left-ear stream, right = right-ear) -> gammatone^0.6 envelope (organisers'
         method, see ../../envelope_reverse) -> filtfilt order 2, 1-9 Hz -> 32 Hz -> full-trial z-score -> trim
  LABEL: REAL spontaneous switches from the key presses (179 = attend LEFT -> column 0, 184 = attend RIGHT ->
         column 1); before the first press the first pressed side is assumed (~1 s)
Facts verified in tools/: audio = mixed_{start_code-10:03d}.wav (decoder r 0.10-0.15 vs ~0 for wrong mappings);
key 179 = left (r_L - r_R > 0 after 179, < 0 after 184, 3 subjects). Only trials 1-50 (trials 51-60 = passive control).

Output: cache/mat_reac/AASD_S{s:02d}_T{t:02d}.npz with eeg (T,58), env (T,2), att (T,) at 32 Hz, t0, att128 (full
trial at 128 Hz, for the scorer), n128; index.csv (key, subject, trial, n, n_switches)."""
import os
import sys

import numpy as np
import pandas as pd
import scipy.io as sio
from joblib import Parallel, delayed
from scipy.io import wavfile
from scipy.signal import butter, sosfiltfilt

HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.abspath(os.path.join(HERE, "..", "..", ".."))
sys.path.insert(0, os.path.join(ROOT, "experiments", "decoder_lodo"))
sys.path.insert(0, os.path.join(ROOT, "experiments", "envelope_reverse"))
from prepare_data import causal_ea, zscore_trial  # noqa: E402
from common import MONTAGES  # noqa: E402
from reverse import candidates  # noqa: E402

X = os.path.join(HERE, "extract")
PREP = sys.argv[1] if len(sys.argv) > 1 else "mat_reac"          # "mat" = baseline pipeline without EA
OUT = os.path.join(HERE, "cache", PREP)
CNT = ['FP1', 'FPZ', 'FP2', 'AF3', 'AF4', 'F7', 'F5', 'F3', 'F1', 'FZ', 'F2', 'F4', 'F6', 'F8', 'FT7', 'FC5', 'FC3', 'FC1',
       'FCZ', 'FC2', 'FC4', 'FC6', 'FT8', 'T7', 'C5', 'C3', 'C1', 'CZ', 'C2', 'C4', 'C6', 'T8', 'M1', 'TP7', 'CP5', 'CP3',
       'CP1', 'CPZ', 'CP2', 'CP4', 'CP6', 'TP8', 'M2', 'P7', 'P5', 'P3', 'P1', 'PZ', 'P2', 'P4', 'P6', 'P8', 'PO7', 'PO5',
       'PO3', 'POZ', 'PO4', 'PO6', 'PO8', 'CB1', 'O1', 'OZ', 'O2', 'CB2']
CH62 = [c for c in CNT if c not in ("M1", "M2")]
FS0, FS, TRIM = 128, 32, 32
BP = butter(2, [1, 9], "bandpass", fs=FS0, output="sos")
MONT = [c.upper() for c in MONTAGES["b64ustc58"]]


def envelope(code):
    """gammatone^0.6 envelope of both channels at 128 Hz, band-passed; cached on disk (all subjects share the audio)."""
    cp = os.path.join(HERE, "cache", "env", f"mixed_{code - 10:03d}.npy")
    if os.path.exists(cp):
        return np.load(cp)
    fs, a = wavfile.read(os.path.join(X, "Stimuli Audio", f"mixed_{code - 10:03d}.wav"))
    e = np.stack([candidates(a[:, c].astype(np.float64), fs)["gt_pow06"] for c in range(2)], 1)
    e = sosfiltfilt(BP, e, axis=0)
    os.makedirs(os.path.dirname(cp), exist_ok=True)
    np.save(cp + ".tmp.npy", e)
    os.replace(cp + ".tmp.npy", cp)
    return e


def subject(s):
    d = sio.loadmat(os.path.join(X, "Processed EEG", f"S{s}", f"S{s}.mat"), squeeze_me=True, struct_as_record=False)["EEG_new"]
    start, presses = {}, {}
    for r in d.event:
        c, lat, e = int(float(r[0])), float(r[1]), int(float(r[4]))
        if c in (179, 184):
            presses.setdefault(e, []).append((lat - (e - 1) * d.data.shape[1] - 1, c))
        else:
            start.setdefault(e, c)
    sel = [CH62.index(c) if c in CH62 else -1 for c in MONT]
    R, rows = None, []
    os.makedirs(OUT, exist_ok=True)
    for t in range(1, 51):                                         # recording order; 51-60 = passive control
        x = sosfiltfilt(BP, d.data[:, :, t - 1].T.astype(np.float64), axis=0)   # (7680, 62) @128 Hz
        n128 = len(x)
        x = x[::FS0 // FS]
        eeg = np.zeros((len(x), len(MONT)))
        pres = np.array([i >= 0 for i in sel])
        eeg[:, pres] = x[:, [i for i in sel if i >= 0]]
        eeg[:, pres] -= eeg[:, pres].mean(1, keepdims=True)
        eeg = zscore_trial(eeg)
        if PREP == "mat_reac":
            eeg, R = causal_ea(eeg, FS, tau_s=60.0, R0=R, return_R=True)
        env = envelope(start[t])[: n128][:: FS0 // FS]
        n = min(len(eeg), len(env))
        # labels from the key presses (128 Hz), first pressed side assumed from the trial start
        pr = sorted(presses.get(t, []))
        att128 = np.zeros(n128, np.int8)
        if pr:
            att128[:] = 0 if pr[0][1] == 179 else 1
            for lat, c in pr:
                att128[max(0, int(round(lat))):] = 0 if c == 179 else 1
        att = att128[:: FS0 // FS][:n]
        key = f"AASD_S{s:02d}_T{t:02d}"
        np.savez(os.path.join(OUT, key + ".npz"), eeg=eeg[TRIM:n - TRIM].astype(np.float32),
                 env=zscore_trial(env[:n])[TRIM:n - TRIM].astype(np.float32), att=att[TRIM:n - TRIM], t0=TRIM / FS,
                 att128=att128, n128=n128)
        rows.append(dict(key=key, subject=s, trial=t, n=n128, n_switches=int(np.sum(np.diff(att128) != 0)),
                         share_left=float(np.mean(att128 == 0))))
    return rows


if __name__ == "__main__":
    subs = sorted(int(f[1:]) for f in os.listdir(os.path.join(X, "Processed EEG")) if f.startswith("S"))
    Parallel(n_jobs=4)(delayed(envelope)(code) for code in range(11, 71))          # 60 shared audio files
    rows = sum(Parallel(n_jobs=3)(delayed(subject)(s) for s in subs), [])
    I = pd.DataFrame(rows)
    if PREP == "mat_reac":
        I.to_csv(os.path.join(HERE, "cache", "index.csv"), index=False)
    print(f"AASD: {I.subject.nunique()} subjects, {len(I)} trials, switches per trial median {I.n_switches.median():.0f} "
          f"(mean interval {60 / max(I.n_switches.mean(), 1e-9):.1f} s), left share {I.share_left.mean():.2f}")
