"""Common input of the foundation-model experiments (D1-D3 of REAL_SWITCH_STRATEGY.md): EEG at 64 Hz, 58 montage channels, band 0.5-20 Hz.

Carta2026 is only available at 64 Hz, 0.5-20 Hz (authors' ICA-cleaned data), so this is the widest band all sets share. Every step is CAUSAL from the raw data of
the trial: Butterworth band-pass order 2 (sosfilt) -> CAR over the present channels -> 64 Hz -> division by the bias-corrected running RMS per channel (60 s).
Absent channels stay zero. Only Carta's input has the authors' zero-phase filter before this (their published data; the blind sets are not affected).
usage: python fm_input.py -> cache/fm_input/<key>.npy (n64, 58) float16"""
import glob
import importlib.util
import os
import sys

import numpy as np
import scipy.io as sio
from joblib import Parallel, delayed
from scipy.signal import butter, lfilter, sosfilt

from common_exp import EXP, rows_of
from common import B64U, DS_KEYS, MONTAGES, b64_idx, load_eeg

OUT = os.path.join(EXP, "cache", "fm_input")
XT = os.path.join(EXP, "..", "external_data")
MONT = MONTAGES["b64ustc58"]
TAU = 60.0


def _conv(rel):
    """The dataset's converter as a module (constants and loaders only; its work is under __main__)."""
    spec = importlib.util.spec_from_file_location("conv_" + os.path.basename(rel)[:-3], os.path.join(XT, rel))
    m = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(m)
    return m


def finish(x, fs, pres, key):
    """x (n, 58) at fs; band-pass, CAR, 64 Hz, running RMS."""
    sos = butter(2, [0.5, 20.0], "bandpass", fs=fs, output="sos")
    x = sosfilt(sos, x - x[0], axis=0)                                      # first sample removed: no start-up transient from the DC offset
    x[:, pres] -= x[:, pres].mean(1, keepdims=True)
    x[:, ~pres] = 0.0
    x = x[:: fs // 64]
    a = 1.0 - np.exp(-1.0 / (TAU * 64))
    den = lfilter([a], [1.0, -(1.0 - a)], np.ones(len(x)))[:, None]
    rms = np.sqrt(lfilter([a], [1.0, -(1.0 - a)], x ** 2, axis=0) / den)
    z = np.clip(x / np.maximum(rms, 1e-6 * (rms.max() + 1e-12)), -8, 8)
    z[:, ~pres] = 0.0
    np.save(os.path.join(OUT, key + ".npy"), z.astype(np.float16))


def corpus(r):
    raw = load_eeg(r["key"], "raw")
    src, present = b64_idx(r)
    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 MONT])
    finish(x[:, sel], 128, present[sel], r["key"])


def fuglsang(f):
    z = np.load(f)
    finish(z["eeg"].astype(np.float64), 128, z["present"].astype(bool), "FUG_" + os.path.basename(f)[:-4])


def aasd(s):
    X = os.path.join(XT, "AASD", "extract")
    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"]
    CH62 = _conv("AASD/convert_aasd.py").CH62
    sel = [CH62.index(c) if c in CH62 else -1 for c in MONT]
    pres = np.array([i >= 0 for i in sel])
    for t in range(1, 51):
        y = d.data[:, :, t - 1].T.astype(np.float64)
        x = np.zeros((len(y), 58))
        x[:, pres] = y[:, [i for i in sel if i >= 0]]
        finish(x, 128, pres, f"AASD_S{s:02d}_T{t:02d}")


def carta(s):
    D = os.path.join(XT, "Carta2026", "extract", "Carta_et_al_2026", "Data", "MAIN", "dataCND")
    d = _conv("Carta2026/convert_carta.py").loadmat_any(os.path.join(D, f"ica_pre_05-20Hz_64fs_dataSub{s:03d}.mat"))["eeg"]
    get = (lambda k: getattr(d, k)) if hasattr(d, "_fieldnames") else (lambda k: d[k])
    labs = [str(getattr(c, "labels", c)).strip().upper() for c in np.atleast_1d(get("chanlocs"))]
    sel = [labs.index(c) if c in labs else -1 for c in MONT]
    pres = np.array([i >= 0 for i in sel])
    for t, y in enumerate(np.atleast_1d(get("data")), start=1):
        y = np.asarray(y, np.float64)
        y = y.T if y.shape[0] < y.shape[1] else y
        x = np.zeros((len(y), 58))
        x[:, pres] = y[:, [i for i in sel if i >= 0]]
        finish(x, 64, pres, f"CAR_S{s:02d}_T{t:02d}")


if __name__ == "__main__":
    os.makedirs(OUT, exist_ok=True)
    n = int(os.environ.get("SCORE_JOBS", 3))
    Parallel(n_jobs=n)(delayed(corpus)(r) for ds in DS_KEYS for _, r in rows_of(ds).iterrows())
    print("corpus done", flush=True)
    Parallel(n_jobs=n)(delayed(fuglsang)(f) for f in sorted(glob.glob(os.path.join(XT, "Fuglsang2020", "stage_a", "*.npz"))))
    print("Fuglsang done", flush=True)
    Parallel(n_jobs=2)(delayed(aasd)(s) for s in range(1, 19))
    print("AASD done", flush=True)
    Parallel(n_jobs=2)(delayed(carta)(s) for s in range(1, 25))
    print("Carta done", len(os.listdir(OUT)), "files", flush=True)
