"""Shared helpers for the full-corpus EDA (config, paths, index, loaders, filters, stats)."""
from __future__ import annotations

import logging
import os
import pickle
import sys
import time
from functools import lru_cache

import numpy as np
import pandas as pd
import yaml
from scipy import signal

HERE = os.path.dirname(os.path.abspath(__file__))
OUT = os.path.abspath(os.path.join(HERE, ".."))
with open(os.path.join(OUT, "config.yaml")) as fh:
    CFG = yaml.safe_load(fh)
ROOT = CFG["paths"]["root"]
SH_RAW = os.path.join(ROOT, CFG["paths"]["superhuge_raw"])
SH_PRE = os.path.join(ROOT, CFG["paths"]["superhuge_pre"])
CND = os.path.join(ROOT, CFG["paths"]["cnd"])
FIG = os.path.join(OUT, "figures")
TAB = os.path.join(OUT, "tables")
CACHE = os.path.join(OUT, "cache")
LOGS = os.path.join(OUT, "logs")
for _d in (FIG, TAB, CACHE, LOGS):
    os.makedirs(_d, exist_ok=True)

DS = CFG["datasets"]
DS_KEYS = [d["key"] for d in DS]
DS_BY_KEY = {d["key"]: d for d in DS}
DS_BY_SH = {d["sh_name"]: d for d in DS}
COLORS = {d["key"]: d["color"] for d in DS}
LABELS = {d["key"]: d["label"] for d in DS}
FS = CFG["fs_raw"]
SEED = CFG["seed"]

B64 = ['Fp1', 'AF7', 'AF3', 'F1', 'F3', 'F5', 'F7', 'FT7', 'FC5', 'FC3', 'FC1', 'C1', 'C3', 'C5',
       'T7', 'TP7', 'CP5', 'CP3', 'CP1', 'P1', 'P3', 'P5', 'P7', 'P9', 'PO7', 'PO3', 'O1', 'Iz',
       'Oz', 'POz', 'Pz', 'CPz', 'Fpz', 'Fp2', 'AF8', 'AF4', 'AFz', 'Fz', 'F2', 'F4', 'F6', 'F8',
       'FT8', 'FC6', 'FC4', 'FC2', 'FCz', 'Cz', 'C2', 'C4', 'C6', 'T8', 'TP8', 'CP6', 'CP4', 'CP2',
       'P2', 'P4', 'P6', 'P8', 'P10', 'PO8', 'PO4', 'O2']
B64U = [c.upper() for c in B64]
_MISSING_USTC = {"AF7", "AF8", "P9", "P10", "IZ", "AFZ"}
_MISSING_UHD = {"AFZ", "FCZ"}
MONTAGES = {
    # names in B64 order, upper-cased
    "common57": [c for c in B64U if c not in _MISSING_USTC | _MISSING_UHD],
    "b64ustc58": [c for c in B64U if c not in _MISSING_USTC],
    "b64uhd62": [c for c in B64U if c not in _MISSING_UHD],
}


def get_logger(name: str) -> logging.Logger:
    lg = logging.getLogger(name)
    if not lg.handlers:
        lg.setLevel(logging.INFO)
        fmt = logging.Formatter("%(asctime)s %(name)s %(levelname)s %(message)s")
        fh = logging.FileHandler(os.path.join(LOGS, f"{name}.log"))
        fh.setFormatter(fmt)
        sh = logging.StreamHandler(sys.stdout)
        sh.setFormatter(fmt)
        lg.addHandler(fh)
        lg.addHandler(sh)
    return lg


# ---------------------------------------------------------------------------
# Index
# ---------------------------------------------------------------------------
@lru_cache(maxsize=1)
def trial_index() -> pd.DataFrame:
    """One row per SuperHuge raw trial with channel names and file paths."""
    p = os.path.join(TAB, "trial_index.parquet")
    if os.path.exists(p):
        return pd.read_parquet(p)
    meta = pickle.load(open(os.path.join(SH_RAW, "meta", "metadata.pkl"), "rb"))
    rows = []
    for key, r in meta.items():
        d = DS_BY_SH.get(r["dataset_name"])
        if d is None:
            continue
        names = [v["name"] for _, v in sorted(r["channel_infos"].items())]
        rows.append(dict(key=key, ds=d["key"], did=r["dataset_id"], subject=int(r["subject_id"]),
                         trial=int(r["trial_id"]), n=int(r["signal_length"]), nch=int(r["num_channel"]),
                         fs=int(r["fs"]), label=str(r.get("label", "")),
                         channels="|".join(names)))
    df = pd.DataFrame(rows)
    df["ds"] = pd.Categorical(df["ds"], DS_KEYS, ordered=True)
    df = df.sort_values(["ds", "subject", "trial"]).reset_index(drop=True)
    df["subj_uid"] = df["ds"].astype(str) + "-" + df["subject"].map("{:02d}".format)
    df["dur_s"] = df["n"] / df["fs"]
    df.to_parquet(p)
    return df


def channel_names(row) -> list[str]:
    return row["channels"].split("|")


def montage_idx(row, montage: str) -> np.ndarray:
    """Column indices (into this trial's raw EEG) of the montage channels, matched by upper-cased name."""
    up = [c.upper() for c in channel_names(row)]
    pos = {c: i for i, c in enumerate(up)}
    return np.array([pos[c] for c in MONTAGES[montage]])


def b64_idx(row) -> tuple[np.ndarray, np.ndarray]:
    """(source column or -1, present mask) for the 64 B64 channels."""
    up = [c.upper() for c in channel_names(row)]
    pos = {c: i for i, c in enumerate(up)}
    src = np.array([pos.get(c, -1) for c in B64U])
    return src, src >= 0


# ---------------------------------------------------------------------------
# Loaders
# ---------------------------------------------------------------------------
def load_eeg(key: str, stage: str = "raw") -> np.ndarray:
    base = SH_RAW if stage == "raw" else SH_PRE
    return np.load(os.path.join(base, "eeg", f"{key}.npy"), mmap_mode="r")


def load_env(key: str, stage: str = "raw") -> np.ndarray:
    """(T, 2): column 0 = majority-attended stream (converter convention)."""
    base = SH_RAW if stage == "raw" else SH_PRE
    e = np.load(os.path.join(base, "stimuli", "env", f"{key}_env.npy"), mmap_mode="r")
    return np.asarray(e[:, 0, :])


def load_labels(key: str) -> dict:
    """Per-sample CND ground truth for one trial (built by b1_cnd_extract.py)."""
    return dict(np.load(os.path.join(CACHE, "cnd", f"{key}.npz"), allow_pickle=True))


# ---------------------------------------------------------------------------
# Filters (causal and zero-phase) and the causal pipeline
# ---------------------------------------------------------------------------
@lru_cache(maxsize=64)
def sos_hp(fc, order, fs):
    return signal.butter(order, fc, "highpass", fs=fs, output="sos")


@lru_cache(maxsize=64)
def sos_lp(fc, order, fs):
    return signal.butter(order, fc, "lowpass", fs=fs, output="sos")


@lru_cache(maxsize=64)
def sos_bp(lo, hi, order, fs):
    return signal.butter(order, [lo, hi], "bandpass", fs=fs, output="sos")


def cfilt(sos, x):
    """Causal sosfilt along axis 0, initialised to the steady state of the first sample."""
    zi = signal.sosfilt_zi(sos)                              # (n_sec, 2)
    zi = zi[:, :, None] * x[0][None, None, :] if x.ndim == 2 else zi * x[0]
    y, _ = signal.sosfilt(sos, x, axis=0, zi=zi)
    return y


def zfilt(sos, x):
    return signal.sosfiltfilt(sos, x, axis=0)


def filter_chain(x, variant: str, fs: int = FS):
    """Apply the per-channel LTI part of a pipeline variant and decimate.

    Returns (y, fs_out). CAR is NOT applied here: it commutes with these per-channel LTI
    filters (including the linear sosfilt_zi initialisation), so it is applied later per montage.
    """
    P = CFG["pipeline"]
    x = np.asarray(x, dtype=np.float64)
    f = cfilt if variant.startswith("c") else zfilt
    if variant in ("c19", "z19"):
        y = f(sos_hp(P["hp_hz"], P["hp_order"], fs), x)
        y = f(sos_bp(P["bp_hz"][0], P["bp_hz"][1], P["bp_order"], fs), y)
        y = f(sos_lp(P["aa_hz"], P["aa_order"], fs), y)
        q = fs // P["fs_out"]
        return y[::q], P["fs_out"]
    if variant in ("cwide", "zwide"):
        y = f(sos_hp(P["wide_bp_hz"][0], P["hp_order"], fs), x)
        y = f(sos_lp(P["aa_hz_wide"], P["aa_order"], fs), y)
        q = fs // P["fs_out_wide"]
        return y[::q], P["fs_out_wide"]
    raise ValueError(variant)


def _runz_py(x, alpha, clip, n_init):
    T, C = x.shape
    out = np.empty_like(x)
    m = x[:n_init].mean(0).copy()
    s = 1.2533 * np.abs(x[:n_init] - m).mean(0) + 1e-12
    for t in range(T):
        z = (x[t] - m) / s
        z = np.clip(z, -clip, clip)
        out[t] = z
        xv = m + z * s
        m = m + alpha * (xv - m)
        s = s + alpha * (1.2533 * np.abs(xv - m) - s)
        s = np.maximum(s, 1e-12)
    return out


try:
    import numba

    @numba.njit(cache=True)
    def _runz_nb(x, alpha, clip, n_init):
        T, C = x.shape
        out = np.empty_like(x)
        m = np.zeros(C)
        s = np.zeros(C)
        for c in range(C):
            acc = 0.0
            for t in range(n_init):
                acc += x[t, c]
            m[c] = acc / n_init
            acc = 0.0
            for t in range(n_init):
                acc += abs(x[t, c] - m[c])
            s[c] = 1.2533 * acc / n_init + 1e-12
        for t in range(T):
            for c in range(C):
                z = (x[t, c] - m[c]) / s[c]
                if z > clip:
                    z = clip
                elif z < -clip:
                    z = -clip
                out[t, c] = z
                xv = m[c] + z * s[c]
                m[c] += alpha * (xv - m[c])
                s[c] += alpha * (1.2533 * abs(xv - m[c]) - s[c])
                if s[c] < 1e-12:
                    s[c] = 1e-12
        return out
except Exception:  # pragma: no cover
    _runz_nb = None


def running_z(x, fs, tau_s=None, clip=None, n_init=None):
    """Causal running robust z-score per column (EW mean + EW mean-absolute-deviation*1.2533).

    Output at t uses statistics from samples < t only (plus an initial estimate from the first
    n_init samples, which lie inside the trimmed warm-up second). Updates use the winsorised
    (clipped) value so single artefacts cannot blow the scale up.
    """
    P = CFG["pipeline"]
    tau_s = P["runz_tau_s"] if tau_s is None else tau_s
    clip = P["runz_clip"] if clip is None else clip
    n_init = int(round(P["edge_trim_s"] * fs)) if n_init is None else n_init
    x = np.ascontiguousarray(np.asarray(x, dtype=np.float64))
    squeeze = x.ndim == 1
    if squeeze:
        x = x[:, None]
    alpha = 1.0 / (tau_s * fs)
    f = _runz_nb if _runz_nb is not None else _runz_py
    y = f(x, alpha, float(clip), int(n_init))
    return y[:, 0] if squeeze else y


def car(x):
    return x - x.mean(1, keepdims=True)


def load_pp(row, variant="c19", montage="common57", do_car=True, do_runz=True, do_trim=True):
    """EEG + envelopes for one trial after a pipeline variant, per montage.

    variant: c19 | z19 | cwide (cache/pp) or 'sup' (supplied 32 Hz preprocessed data).
    Returns dict(eeg (T,C), env (T,2), fs, att_col (T,) int8 per-sample attended column,
    az (T,) attended azimuth, t0_s (time of first returned sample in the trial)).
    Montage channels a dataset lacks are zero columns (excluded from the CAR).
    """
    key = row["key"]
    mont = MONTAGES[montage]
    sel = np.array([B64U.index(c) for c in mont])
    if variant == "sup":
        raw = load_eeg(key, "pre")
        src, present = b64_idx(row)
        x = np.zeros((raw.shape[0], 64))
        x[:, present] = np.asarray(raw[:, src[present]], dtype=np.float64)
        env = load_env(key, "pre").astype(np.float64)
        fs = 32
    else:
        z = np.load(os.path.join(CACHE, "pp", variant, f"{key}.npz"))
        x, env, present = z["eeg"].astype(np.float64), z["env"].astype(np.float64), z["present"]
        fs = CFG["pipeline"]["fs_out_wide"] if variant.endswith("wide") else CFG["pipeline"]["fs_out"]
    x = x[:, sel]
    pres = present[sel]
    if do_car:
        x[:, pres] = x[:, pres] - x[:, pres].mean(1, keepdims=True)
    if do_runz:
        x = running_z(x, fs)
        x[:, ~pres] = 0.0
        env = running_z(env, fs)
    lab = load_labels(key)
    q = FS // fs
    att = lab["att_col"][::q][: len(x)]
    az = lab["att_az"][::q][: len(x)]
    n = min(len(x), len(env), len(att))
    x, env, att, az = x[:n], env[:n], att[:n], az[:n]
    t0 = 0.0
    if do_trim:
        k = int(round(CFG["pipeline"]["edge_trim_s"] * fs))
        x, env, att, az = x[k:n - k], env[k:n - k], att[k:n - k], az[k:n - k]
        t0 = k / fs
    return dict(eeg=x, env=env, fs=fs, att_col=att.astype(np.int8), az=az, t0_s=t0, present=pres)


def trim(x, fs):
    k = int(round(CFG["pipeline"]["edge_trim_s"] * fs))
    return x[k:len(x) - k]


# ---------------------------------------------------------------------------
# Stats helpers
# ---------------------------------------------------------------------------
def boot_ci(v, stat=np.median, n=None, ci=None, seed=SEED):
    v = np.asarray(v, float)
    v = v[np.isfinite(v)]
    if len(v) == 0:
        return (np.nan, np.nan, np.nan)
    n = CFG["stats"]["n_boot"] if n is None else n
    ci = CFG["stats"]["ci"] if ci is None else ci
    rng = np.random.default_rng(seed)
    idx = rng.integers(0, len(v), (n, len(v)))
    b = np.apply_along_axis(stat, 1, v[idx]) if stat not in (np.median, np.mean) else stat(v[idx], axis=1)
    a = (1 - ci) / 2
    return (float(stat(v)), float(np.quantile(b, a)), float(np.quantile(b, 1 - a)))


def fmt_ci(t, nd=3):
    return f"{t[0]:.{nd}f} [{t[1]:.{nd}f}, {t[2]:.{nd}f}]"


def fdr_bh(p):
    p = np.asarray(p, float)
    n = len(p)
    o = np.argsort(p)
    r = p[o] * n / np.arange(1, n + 1)
    r = np.minimum.accumulate(r[::-1])[::-1]
    q = np.empty(n)
    q[o] = np.minimum(r, 1)
    return q


def savefig(fig, name, df: pd.DataFrame | None = None):
    import matplotlib.pyplot as plt
    fig.savefig(os.path.join(FIG, f"{name}.png"), dpi=150, bbox_inches="tight")
    plt.close(fig)
    if df is not None:
        df.to_csv(os.path.join(FIG, f"{name}.csv"), index=False)


def timer():
    t0 = time.time()
    return lambda: time.time() - t0


def topo(ax, x, y, v, cmap="RdBu_r", vmin=None, vmax=None, names=None, cbar=True, res=64):
    """Minimal topographic map (linear interpolation inside the electrode hull + head outline)."""
    import matplotlib.pyplot as plt
    from scipy.interpolate import griddata
    xi = np.linspace(-0.55, 0.55, res)
    X, Y = np.meshgrid(xi, xi)
    Z = griddata(np.c_[x, y], v, (X, Y), method="cubic")
    Z[np.hypot(X, Y) > 0.5] = np.nan
    im = ax.contourf(X, Y, Z, 20, cmap=cmap, vmin=vmin, vmax=vmax,
                     levels=np.linspace(vmin if vmin is not None else np.nanmin(v),
                                        vmax if vmax is not None else np.nanmax(v), 21))
    ax.scatter(x, y, s=3, c="k")
    t = np.linspace(0, 2 * np.pi, 200)
    ax.plot(0.5 * np.cos(t), 0.5 * np.sin(t), "k", lw=1)
    ax.plot([-0.05, 0, 0.05], [0.495, 0.56, 0.495], "k", lw=1)
    if names is not None:
        for xx, yy, nn in zip(x, y, names):
            ax.text(xx, yy, nn, fontsize=4)
    ax.set_aspect("equal")
    ax.axis("off")
    if cbar:
        plt.colorbar(im, ax=ax, fraction=0.04)
    return im
