"""Convert Carta et al. 2026 (PLOS Biology; Zenodo 20569817, CC-BY-4.0) to the decoder_lodo cache format.

Paradigm: two competing TED-talk streams (left / right loudspeaker) + 16-talker babble; listeners switch attention
on a visual cue every ~15-30 s (REAL cued switches). 24 subjects x 20 trials x 175 s, BioSemi 64 ch.
Facts from the authors' code (Code/Step0_MakeStimMatrix_Stim2CND.m):
  * audio Data/STIM/Audio/Audio_{trial:03d}.wav, stereo (left / right stream), first 5 s are zero padding
  * attention at trial start: startAtt = [1,1,-1,-1,...] (LEFT = 1), flipping at every Data/MAIN/attentionSwitches.mat time
  * EEG CND Data/MAIN/dataCND/ica_pre_05-20Hz_64fs_dataSubXXX.mat (ICA-cleaned, 0.5-20 Hz, 64 Hz), aligned to speech onset

Pipeline (= prep 'mat_reac' / 'mat'):
  EEG  : 58 montage channels by name -> filtfilt Butterworth order 2, 1-9 Hz -> 32 Hz -> CAR -> full-trial z-score
         -> [mat_reac: causal running EA carried across the subject's trials] -> 1-s edge trim
  ENV  : gammatone^0.6 per channel (organisers' method) -> filtfilt order 2, 1-9 Hz -> 32 Hz -> z-score -> trim
  LABEL: column 0 = LEFT stream, column 1 = RIGHT; att(t) from startAtt and the switch times (real cued switches)
usage: python convert_carta.py [mat_reac|mat]  -> cache/<prep>/CAR_S{s:02d}_T{t:02d}.npz, cache/index.csv"""
import glob
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

D = os.path.join(HERE, "extract", "Carta_et_al_2026", "Data")
PREP = sys.argv[1] if len(sys.argv) > 1 else "mat_reac"
OUT = os.path.join(HERE, "cache", PREP)
START_ATT = [1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1]
MONT = [c.upper() for c in MONTAGES["b64ustc58"]]
TRIM = 32


def loadmat_any(f):
    """CND files may be MATLAB v7.3 (HDF5)."""
    try:
        return sio.loadmat(f, squeeze_me=True, struct_as_record=False)
    except NotImplementedError:
        import mat73
        return mat73.loadmat(f)


def envelope(trial):
    cp = os.path.join(HERE, "cache", "env", f"Audio_{trial:03d}.npy")
    if os.path.exists(cp):
        return np.load(cp)
    fs, a = wavfile.read(os.path.join(D, "STIM", "Audio", f"Audio_{trial:03d}.wav"))
    a = a[5 * fs:].astype(np.float64)                                # drop the 5-s zero padding
    e = np.stack([candidates(a[:, c], fs)["gt_pow06"] for c in range(2)], 1)
    e = sosfiltfilt(butter(2, [1, 9], "bandpass", fs=128, output="sos"), 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 labels(trial, n128, switches):
    sw = [t for t in np.atleast_1d(switches[trial - 1]) if np.isfinite(t) and t > 0]
    att = np.zeros(n128, np.int8)
    cur = 0 if START_ATT[trial - 1] == 1 else 1
    prev = 0
    for t in sw:
        s = min(n128, int(round(t * 128)))
        att[prev:s] = cur
        cur, prev = 1 - cur, s
    att[prev:] = cur
    return att


def subject(f, switches):
    s = int(os.path.basename(f)[-7:-4])
    d = loadmat_any(f)["eeg"]
    get = (lambda k: getattr(d, k)) if hasattr(d, "_fieldnames") else (lambda k: d[k])
    fs = int(np.ravel(get("fs"))[0])
    labs = [str(getattr(c, "labels", c)).strip().upper() for c in np.atleast_1d(get("chanlocs"))] if np.size(get("chanlocs")) else []
    sel = [labs.index(c) if c in labs else -1 for c in MONT]
    pres = np.array([i >= 0 for i in sel])
    bp = butter(2, [1, 9], "bandpass", fs=fs, output="sos")
    R, rows = None, []
    os.makedirs(OUT, exist_ok=True)
    for t, x in enumerate(np.atleast_1d(get("data")), start=1):
        x = np.asarray(x, np.float64)
        if x.shape[0] < x.shape[1]:
            x = x.T
        x = sosfiltfilt(bp, x, axis=0)[:: fs // 32]
        eeg = np.zeros((len(x), len(MONT)))
        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, 32, tau_s=60.0, R0=R, return_R=True)
        env128 = envelope(t)
        n128 = min(len(env128), len(x) * 4)
        att128 = labels(t, n128, switches)
        env = env128[:n128][::4]
        n = min(len(eeg), len(env))
        att = att128[::4][:n]
        key = f"CAR_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 / 32,
                 att128=att128, n128=n128)
        rows.append(dict(key=key, subject=s, trial=t, n=n128, n_switches=int(np.sum(np.diff(att128) != 0)), channels=int(pres.sum())))
    return rows


if __name__ == "__main__":
    switches = sio.loadmat(os.path.join(D, "MAIN", "attentionSwitches.mat"))["attentionSwitches"]
    Parallel(n_jobs=4)(delayed(envelope)(t) for t in range(1, 21))
    files = sorted(glob.glob(os.path.join(D, "MAIN", "dataCND", "ica_pre_05-20Hz_64fs_dataSub*.mat")))
    rows = sum(Parallel(n_jobs=3)(delayed(subject)(f, switches) for f in files), [])
    I = pd.DataFrame(rows)
    if PREP == "mat_reac":
        I.to_csv(os.path.join(HERE, "cache", "index.csv"), index=False)
    print(f"Carta2026 [{PREP}]: {I.subject.nunique()} subjects, {len(I)} trials, switches/trial median {I.n_switches.median():.0f}, "
          f"channels present {I.channels.min()}-{I.channels.max()}")
