"""Extra causal features designed per time-to-N2 regime (Track 03, Muse), 18 in total.

All are computed per recording, in time order, from the per-window features of the low-pass 45 Hz pipeline
(experiments/iir_filter/cache/lp45_X.npy), using only the current and earlier windows.

NEAR  (0-90 s before N2; EDA: "late movers" sigma/beta, 1/f slope, theta/alpha only move in the last 1.5-2.5 min)
  near_slope60_sigma_beta_TP     least-squares slope over the last 60 s (12 windows), per second
  near_slope60_aper_slope_TP
  near_slope60_theta_alpha_TP
  near_z_sigma_beta_TP           EMA 30 s as a z-score against the expanding mean and SD of this recording (own baseline)
  near_z_aper_slope_TP
  near_ema10_minus_ema60_sigma_beta_TP   fast minus slow average: is the ratio accelerating?
MID   (90-300 s; "slow drifters" beta, gamma, alpha, amplitude fall over 2-5 min)
  mid_slope180_log_beta_TP       slope over the last 180 s (36 windows)
  mid_slope180_log_gamma_TP
  mid_slope180_log_alpha_TP
  mid_dropmax_log_beta_TP        EMA 30 s minus its running maximum so far: how far down from the most alert level
  mid_dropmax_log_gamma_TP
  mid_dropmax_log_sd_TP
FAR   (300-600 s; wakefulness indicators: artifacts, movement)
  far_frac_bad_60s               fraction of the last 12 windows with at least one bad channel
  far_frac_bad_300s              ... of the last 60 windows
  far_mean_bad_since_start       expanding mean of the bad-channel count
  far_since_last_bad_s           seconds since the last window with a bad channel (capped at 600)
  far_max_logmaxabs_TP_300s      largest log max-amplitude of the temporal channels over the last 300 s
  far_frac_clip_300s             fraction of the last 60 windows with clipped samples on the temporal pair
"""
import os
import sys

import numpy as np
import pandas as pd
from scipy.signal import lfilter

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/stft_tree"))
import run_stft_tree as R  # noqa: E402

IIR = os.path.join(ROOT, "experiments/iir_filter")
CACHE = os.path.join(HERE, "cache")
NAMES = ["near_slope60_sigma_beta_TP", "near_slope60_aper_slope_TP", "near_slope60_theta_alpha_TP",
         "near_z_sigma_beta_TP", "near_z_aper_slope_TP", "near_ema10_minus_ema60_sigma_beta_TP",
         "mid_slope180_log_beta_TP", "mid_slope180_log_gamma_TP", "mid_slope180_log_alpha_TP",
         "mid_dropmax_log_beta_TP", "mid_dropmax_log_gamma_TP", "mid_dropmax_log_sd_TP",
         "far_frac_bad_60s", "far_frac_bad_300s", "far_mean_bad_since_start", "far_since_last_bad_s",
         "far_max_logmaxabs_TP_300s", "far_frac_clip_300s"]
REGIME = {n: n.split("_")[0] for n in NAMES}


def ema(x, tau):
    a = 1 - np.exp(-5.0 / tau)
    return lfilter([a], [1, -(1 - a)], x, zi=(1 - a) * x[:1])[0]


def roll_slope(x, m):
    """Least-squares slope (per second) over the last min(m, i+1) windows; 0 while fewer than 3 points."""
    n = len(x); t = np.arange(n, dtype=np.float64)
    cs = lambda v: np.concatenate([[0.0], np.cumsum(v)])
    cx, ctx, ct, ct2 = cs(x), cs(t * x), cs(t), cs(t * t)
    i = np.arange(n); lo = np.maximum(i - m + 1, 0); L = i - lo + 1
    sx, stx, st, st2 = cx[i + 1] - cx[lo], ctx[i + 1] - ctx[lo], ct[i + 1] - ct[lo], ct2[i + 1] - ct2[lo]
    den = L * st2 - st ** 2
    return np.where(L >= 3, (L * stx - st * sx) / np.where(den == 0, 1.0, den), 0.0) / 5.0


def expanding_z(x, min_periods=12):
    n = len(x); k = np.arange(1, n + 1)
    mean = np.cumsum(x) / k
    var = np.maximum(np.cumsum(x * x) / k - mean ** 2, 0.0)
    z = (x - mean) / (np.sqrt(var) + 1e-6)
    z[:min_periods] = 0.0
    return z


def roll_mean(x, m):
    return pd.Series(x).rolling(m, min_periods=1).mean().to_numpy()


def roll_max(x, m):
    return pd.Series(x).rolling(m, min_periods=1).max().to_numpy()


def since_last(ind):
    out = np.empty(len(ind)); last = -1
    for i, b in enumerate(ind):
        if b:
            last = i
        out[i] = min(600.0, 5.0 * (i - last)) if last >= 0 else min(600.0, 5.0 * (i + 1))
    return out


def one_recording(c, lm):
    """c: dict name -> (n,) arrays of core features of ONE recording; lm: (n,) log max-abs of the temporal pair."""
    sb, asl, ta = c["sigma_beta_TP"], c["aper_slope_TP"], c["theta_alpha_TP"]
    lb, lg, la, ls = c["log_beta_TP"], c["log_gamma_TP"], c["log_alpha_TP"], c["log_sd_TP"]
    nb, clip = c["n_bad"], c["clip_frac_TP"]
    cm = lambda x: np.maximum.accumulate(ema(x, 30))
    return np.stack([
        roll_slope(sb, 12), roll_slope(asl, 12), roll_slope(ta, 12),
        expanding_z(ema(sb, 30)), expanding_z(ema(asl, 30)), ema(sb, 10) - ema(sb, 60),
        roll_slope(lb, 36), roll_slope(lg, 36), roll_slope(la, 36),
        ema(lb, 30) - cm(lb), ema(lg, 30) - cm(lg), ema(ls, 30) - cm(ls),
        roll_mean((nb > 0).astype(float), 12), roll_mean((nb > 0).astype(float), 60),
        np.cumsum(nb) / np.arange(1, len(nb) + 1), since_last(nb > 0),
        roll_max(lm, 60), roll_mean((clip > 0).astype(float), 60)], 1)


def build(X, names, win):
    """X: lp45 per-window features (N, 217); returns (N, 18) float32 aligned with `win`."""
    C, cn = R.core_matrix(X, names)
    col = {n: i for i, n in enumerate(cn)}
    lm_all = X[:, [names.index("log_maxabs_TP9"), names.index("log_maxabs_TP10")]].mean(1)
    out = np.zeros((len(win), len(NAMES)), np.float32)
    for r, idx in win.groupby("rec").indices.items():
        c = {n: C[idx, j].astype(np.float64) for n, j in col.items()}
        out[idx] = one_recording(c, lm_all[idx].astype(np.float64))
    return out


def load(use_cache=True):
    X0, names, win, rec = R.extract()
    X = np.load(f"{IIR}/cache/lp45_X.npy")
    path = os.path.join(CACHE, "regime_feats.npy")
    if use_cache and os.path.exists(path):
        return X, list(names), win, rec, np.load(path)
    os.makedirs(CACHE, exist_ok=True)
    G = build(X, list(names), win)
    assert np.isfinite(G).all(), "non-finite regime features"
    np.save(path, G)
    return X, list(names), win, rec, G


def causality_check(X, names, win, n_check=6, seed=0):
    rng = np.random.default_rng(seed)
    C, cn = R.core_matrix(X, names)
    col = {n: i for i, n in enumerate(cn)}
    lm_all = X[:, [names.index("log_maxabs_TP9"), names.index("log_maxabs_TP10")]].mean(1)
    groups = win.groupby("rec").indices
    for r in rng.choice(list(groups), n_check, replace=False):
        idx = groups[r]
        c = {n: C[idx, j].astype(np.float64) for n, j in col.items()}
        full = one_recording(c, lm_all[idx].astype(np.float64))
        k = int(rng.integers(5, len(idx)))
        part = one_recording({n: v[:k] for n, v in c.items()}, lm_all[idx][:k].astype(np.float64))
        assert np.allclose(part, full[:k], atol=1e-9), f"regime features are not causal (recording {r})"
    return n_check
