"""E8: bipolar channels and the slow (0.1-1 Hz) frontal band, on top of `cnn_feat_gru` with the low-pass 45 Hz input.

Motivation (EDA, section 6): slow horizontal eye movements, visible as a negative AF7-AF8 correlation in the
0.1-1 Hz band (median -0.14 far from N2, -0.41 at 90-180 s before it), are a classic N1 sign, and the 217
features cannot see them (the per-window mean removal of a 5 s window leaves almost nothing below 0.2 Hz).

New inputs, all causal (filters carry their state across windows):
  bipolar    AF7-AF8 and TP9-TP10 of the low-pass-45 Hz signal: added as two extra raw channels (6 in total)
             and as 20 features (6 log band powers, theta/alpha, sigma/beta, delta/beta, log SD; per channel)
  slow       band-pass 0.1-1 Hz (two second-order Butterworth sections per edge) of the RAW signal, per window:
             correlation AF7-AF8, correlation TP9-TP10, log SD of AF7, AF8, TP9, TP10 and of AF7-AF8 (7 features)

Variants
  e8a   bipolar channels + bipolar features
  e8b   e8a + slow-band features
  e8c   slow-band features only (4 raw channels, no bipolar)
Reference: lp45 `cnn_feat_gru` (experiments/iir_filter), same folds, seeds and inner validation people.
"""
import argparse
import os
import sys
import time

import mne
import numpy as np
import torch
from scipy.signal import butter
from torch import nn

HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(os.path.dirname(HERE))
sys.path.insert(0, os.path.join(ROOT, "experiments"))
sys.path.insert(0, os.path.join(ROOT, "experiments/iir_filter"))
import cvharness as H  # noqa: E402
import run_iir_filter as I  # noqa: E402

C, R, log = H.C, H.R, H.log
CACHE = os.path.join(HERE, "cache")
IIR = os.path.join(ROOT, "experiments/iir_filter")
WIN = R.WIN
SOS_SLOW = butter(2, [0.1, 1.0], "bandpass", fs=128, output="sos")
TP9, AF7, AF8, TP10 = 0, 1, 2, 3


def bip_features(xb):
    """(n, 2, 640) -> (n, 20): per bipolar channel 6 log band powers, 3 ratios, log SD."""
    dm = xb - xb.mean(-1, keepdims=True)
    fr = dm[:, :, R.FRAME_IDX] * R.HANN
    P = np.abs(np.fft.rfft(fr, axis=-1)) ** 2 / (R.FS * R.U)
    P[..., 1:-1] *= 2
    Pm = P.mean(2)
    bp = {b: Pm[..., (R.FREQS >= lo) & (R.FREQS < hi)].sum(-1) * R.DF + 1e-6 for b, (lo, hi) in R.BANDS.items()}
    f = [np.log10(bp[b]) for b in R.BANDS]
    f += [np.log10(bp["theta"] / bp["alpha"]), np.log10(bp["sigma"] / bp["beta"]), np.log10(bp["delta"] / bp["beta"]),
          np.log10(dm.std(-1) + 1e-3)]
    return np.stack(f, 2).reshape(len(xb), -1)                                     # (n, 2, 10) -> (n, 20)


def _corr(a, b):
    a = a - a.mean(-1, keepdims=True); b = b - b.mean(-1, keepdims=True)
    return (a * b).sum(-1) / (np.sqrt((a ** 2).sum(-1) * (b ** 2).sum(-1)) + 1e-9)


def slow_features(sl):
    """(n, 4, 640) slow-band signal -> (n, 7)."""
    ls = lambda x: np.log10(x.std(-1) + 1e-3)
    return np.stack([_corr(sl[:, AF7], sl[:, AF8]), _corr(sl[:, TP9], sl[:, TP10]), ls(sl[:, AF7]), ls(sl[:, AF8]),
                     ls(sl[:, TP9]), ls(sl[:, TP10]), ls(sl[:, AF7] - sl[:, AF8])], 1)


def build(win, rec):
    paths = {k: os.path.join(CACHE, f"{k}.npy") for k in ("bip_raw_f16", "bip_feat", "slow_feat")}
    if all(os.path.exists(p) for p in paths.values()):
        return {k: np.load(p) for k, p in paths.items()}
    os.makedirs(CACHE, exist_ok=True)
    counts = win.groupby("rec").size()
    n_all = len(win)
    out = dict(bip_raw_f16=np.empty((n_all, 2, WIN), np.float16), bip_feat=np.empty((n_all, 20), np.float32),
               slow_feat=np.empty((n_all, 7), np.float32))
    sos_lp = I.design(None)
    pos, t0 = 0, time.time()
    for i, r in rec.iterrows():
        vhdr = f"{R.BIDS}/{r.subject}/{r.session_id}/eeg/{r.rec_id}_task-sleeponset_eeg.vhdr"
        x = mne.io.read_raw_brainvision(vhdr, preload=True, verbose="ERROR").get_data(picks=R.CH) * 1e6
        n = int(counts[i]); x = x[:, : n * WIN]
        xf, _ = I.filter_stream(sos_lp, x)                                          # low-pass 45 Hz, causal
        bip = np.stack([xf[AF7] - xf[AF8], xf[TP9] - xf[TP10]])                     # (2, T)
        slow, _ = I.filter_stream(SOS_SLOW, x)                                      # band-pass 0.1-1 Hz of the raw signal
        bw = bip.reshape(2, n, WIN).transpose(1, 0, 2)
        out["bip_raw_f16"][pos:pos + n] = bw.astype(np.float16)
        out["bip_feat"][pos:pos + n] = bip_features(bw)
        out["slow_feat"][pos:pos + n] = slow_features(slow.reshape(4, n, WIN).transpose(1, 0, 2))
        pos += n
    assert pos == n_all
    for k, p in paths.items():
        np.save(p, out[k])
    log(f"E8 features built in {time.time() - t0:.0f} s: bipolar raw {out['bip_raw_f16'].shape}, "
        f"bipolar features {out['bip_feat'].shape}, slow-band features {out['slow_feat'].shape}")
    return out


class Net(nn.Module):
    """Raw CNN (n_ch channels) + feature MLP -> 96-d -> GRU -> 600 * sigmoid."""

    def __init__(self, n_ch, n_feat, dim=96, hidden=128, dropout=0.1):
        super().__init__()

        def block(cin, cout, k, s):
            return nn.Sequential(nn.Conv1d(cin, cout, k, s, k // 2), nn.GroupNorm(8, cout), nn.GELU())
        self.cnn = nn.Sequential(block(n_ch, 32, 15, 2), block(32, 64, 9, 2), block(64, 96, 9, 2), block(96, 128, 5, 2))
        self.fmlp = nn.Sequential(nn.Linear(n_feat, 128), nn.GELU(), nn.Linear(128, 64), nn.GELU())
        self.out = nn.Sequential(nn.Linear(256 + 64, dim), nn.GELU())
        self.drop = nn.Dropout(dropout)
        self.gru = nn.GRU(dim, hidden, 1, batch_first=True)
        self.head = nn.Sequential(nn.Linear(hidden, 64), nn.GELU(), nn.Linear(64, 1))

    def forward(self, xr, f, h=None):
        b, l = xr.shape[:2]
        x = xr.reshape(b * l, *xr.shape[2:]).float()
        x = x - x.mean(-1, keepdim=True)
        hc = self.cnn(torch.clamp(x / 40.0, -10.0, 10.0))
        z = self.out(torch.cat([hc.mean(-1), hc.amax(-1), self.fmlp(f.reshape(b * l, -1))], 1))
        o, h = self.gru(self.drop(z.reshape(b, l, -1)), h)
        return 600.0 * torch.sigmoid(self.head(o).squeeze(-1)), h


def build_specs(win, X, extra):
    y = win.y.to_numpy(); w = R.metric_weights(y, win.rec.to_numpy())
    raw4 = lambda: np.load(f"{IIR}/cache/lp45_raw_f16.npy")
    variants = {
        "e8a": (6, np.concatenate([X, extra["bip_feat"]], 1), True),
        "e8b": (6, np.concatenate([X, extra["bip_feat"], extra["slow_feat"]], 1), True),
        "e8c": (4, np.concatenate([X, extra["slow_feat"]], 1), False),
    }
    specs = {}
    for name, (n_ch, F, bip) in variants.items():
        def make_store(F=F, bip=bip):
            raw = raw4()
            if bip:
                raw = np.concatenate([raw, extra["bip_raw_f16"]], 1)
            return C.Store(raw, F.astype(np.float64), y, w, win)
        specs[name] = dict(raw=True, feat=True, make_store=make_store, decode=lambda out, model: out,
                           loss=lambda out, y_, w_, model: (w_ * (out - y_).abs()).sum(1).mean(), loss_scale=0.01,
                           make_model=lambda store, tr, n_ch=n_ch, nf=F.shape[1]: Net(n_ch, nf))
    return specs


if __name__ == "__main__":
    ap = argparse.ArgumentParser()
    ap.add_argument("--folds", type=int, default=5)
    ap.add_argument("--seeds", type=int, nargs="+", default=[0, 1, 2])
    ap.add_argument("--variants", nargs="+", default=["e8a", "e8b", "e8c"])
    ap.add_argument("--epochs", type=int, default=25)
    ap.add_argument("--smoke", action="store_true")
    args = ap.parse_args()
    if args.smoke:
        args.folds, args.seeds, args.epochs = 2, [0], 2
    if H.DEV.type != "cuda":
        log("WARNING: CUDA not available, training on CPU")
    res = os.path.join(HERE, "results_smoke" if args.smoke else "results")
    t0 = time.time()
    X0, names, win, rec = R.extract()
    X = np.load(f"{IIR}/cache/lp45_X.npy")
    extra = build(win, rec)
    for k, v in extra.items():
        assert np.isfinite(v).all(), f"non-finite values in {k}"
    specs = {k: v for k, v in build_specs(win, X, extra).items() if k in args.variants}
    fold, per = H.run_cv(specs, X0, win, rec, args, res)
    H.summarize(res, fold, per, args, "E8: kênh lưỡng cực và dải chậm 0.1-1 Hz trên `cnn_feat_gru`",
                "Input nền là EEG lọc low-pass 45 Hz. `e8a`: thêm 2 kênh lưỡng cực (AF7-AF8, TP9-TP10) làm kênh thô "
                "và 20 feature; `e8b`: `e8a` cộng 7 feature dải chậm (tương quan AF7-AF8, TP9-TP10, độ lớn dải 0.1-1 Hz); "
                "`e8c`: chỉ thêm 7 feature dải chậm. Mốc `lp45` là `cnn_feat_gru` cùng input nền, không thêm gì.",
                [("lp45", os.path.join(IIR, "results"), "lp45")])
    log(f"done in {(time.time() - t0) / 60:.1f} min")
