"""Phase D4 (H12): physiology of spatial attention.

Alpha lateralisation index ALI = (R - L) / (R + L) on parieto-occipital 8-13 Hz power (1-s power from
cache/qcpsd, zero-phase BP, CAR over common57), in 10-s bins. L = P7 P5 P3 PO7 PO3 O1, R = P8 P6 P4 PO8 PO4 O2.
Horizontal EOG proxy = mean(AF7, F7, FT7) - mean(AF8, F8, FT8) (USTC lacks AF7/AF8 -> F7, FT7 vs F8, FT8),
LP 1 Hz (zero-phase), NOT detrended (the level difference between left and right trials is the effect),
10-s bin means. Where the stored data are mean-free per trial (KUL-AV-GC, KUL2016: HP / DC removed) a
between-trial level effect cannot exist, so there only within-trial (switch-locked) changes are informative. True EOG channels (KUL-AV-GC 4 ch, DTU EXG3-8, USTC
HEO/VEO/EKG/EMG) are analysed per channel (FDR over channels).
Per subject: effect = mean(bins attending right) - mean(bins attending left), standardised by the pooled bin SD
(Cohen's d). Across subjects: median d with bootstrap CI, Wilcoxon signed-rank vs 0.
KUL-AV-GC: event-related average +-60 s around every genuine direction switch (sign-aligned so that a
positive deflection = the change expected for left->right), per subject then across subjects.
Writes tables/physiology_*.csv, figures/d4_*.png
"""
import os
import sys

import matplotlib

matplotlib.use("Agg")
import matplotlib.pyplot as plt  # noqa: E402
import numpy as np  # noqa: E402
import pandas as pd  # noqa: E402
from scipy import stats  # noqa: E402

sys.path.insert(0, os.path.dirname(__file__))
from common import CACHE, COLORS, DS_KEYS, FS, LABELS, TAB, boot_ci, fdr_bh, get_logger, load_labels, savefig, trial_index  # noqa: E402

log = get_logger("d4_physiology")
LCH = ["P7", "P5", "P3", "PO7", "PO3", "O1"]
RCH = ["P8", "P6", "P4", "PO8", "PO4", "O2"]
BIN = 10


def bins(x, fs_x, T_s, width=BIN):
    n = int(T_s // width)
    k = int(width * fs_x)
    return np.array([x[i * k:(i + 1) * k].mean(0) for i in range(n)]) if n else np.zeros((0,) + x.shape[1:])


def cohen(v, lab):
    a, b = v[lab == 1], v[lab == 0]
    if len(a) < 2 or len(b) < 2:
        return np.nan
    sp = np.sqrt(((len(a) - 1) * a.var(ddof=1) + (len(b) - 1) * b.var(ddof=1)) / (len(a) + len(b) - 2))
    return (a.mean() - b.mean()) / (sp + 1e-12)


if __name__ == "__main__":
    ti = trial_index()
    per_bin = []
    era_ali, era_heog, era_eog = [], [], []
    for _, r in ti.iterrows():
        z = np.load(os.path.join(CACHE, "qcpsd", f"{r['key']}.npz"))
        lab = load_labels(r["key"])
        chs = list(z["alpha_ch"])
        a = z["alpha1s"]
        L = a[:, [chs.index(c.upper()) for c in LCH]].mean(1)
        R = a[:, [chs.index(c.upper()) for c in RCH]].mean(1)
        ali1 = (R - L) / (R + L)
        T_s = len(ali1)
        side = (lab["att_az"] > 0).astype(float)
        side1 = side[: T_s * FS].reshape(T_s, FS).mean(1)
        # no detrending: for single-direction trials the between-trial level difference IS the effect
        heog = z["heog"].astype(float)
        eog = z["eog4"].astype(float) if z["eog4"].size else z["eog4"]
        b_ali = bins(ali1, 1, T_s)
        b_side = bins(side1, 1, T_s)
        b_heog = bins(heog, 4, len(heog) / 4)
        n = min(len(b_ali), len(b_side), len(b_heog))
        d = dict(key=r["key"], ds=r["ds"], subj=r["subj_uid"], cond=str(lab["cond"]))
        for i in range(n):
            row = dict(d, bin=i, ali=b_ali[i], heog=b_heog[i], right=int(b_side[i] > 0.5), pure=abs(b_side[i] - 0.5) == 0.5)
            if eog.size:
                be = bins(eog, 4, len(eog) / 4)
                for c in range(eog.shape[1]):
                    row[f"eog{c}"] = be[i, c] if i < len(be) else np.nan
            per_bin.append(row)
        if r["ds"] == "AVGC":
            sw = int(np.flatnonzero(np.diff(np.sign(lab["att_az"])) != 0)[0] + 1)
            sgn = 1.0 if lab["att_az"][sw] > 0 else -1.0          # +1 = left->right
            ts = sw // FS
            if ts - 60 >= 0 and ts + 60 <= T_s:
                era_ali.append(dict(subj=r["subj_uid"], cond=str(lab["cond"]), sgn=sgn, x=sgn * ali1[ts - 60:ts + 60]))
            t4 = sw // 32
            if t4 - 240 >= 0 and t4 + 240 <= len(heog):
                seg = z["heog"].astype(float)[t4 - 240:t4 + 240]
                era_heog.append(dict(subj=r["subj_uid"], cond=str(lab["cond"]), sgn=sgn, x=sgn * (seg - seg[:240].mean())))
                if eog.size:
                    se = z["eog4"].astype(float)[t4 - 240:t4 + 240]
                    era_eog.append(dict(subj=r["subj_uid"], sgn=sgn, x=sgn * (se - se[:240].mean(0))))
    pb = pd.DataFrame(per_bin)
    pb.to_parquet(os.path.join(CACHE, "physiology_bins.parquet"))
    pb = pb[pb.pure]                                       # bins entirely on one side
    out = []
    feats = ["ali", "heog"] + [c for c in pb.columns if c.startswith("eog")]
    for ds in DS_KEYS:
        g = pb[pb.ds == ds]
        for f in feats:
            if g[f].isna().all():
                continue
            ds_ = g.dropna(subset=[f]).groupby("subj")[[f, "right"]].apply(lambda s: cohen(s[f].values, s.right.values)).dropna()
            if len(ds_) < 3:
                continue
            m, lo, hi = boot_ci(ds_.values)
            out.append(dict(ds=ds, feature=f, n_subjects=len(ds_), n_bins=int(g[f].notna().sum()), median_d=m, ci_lo=lo,
                            ci_hi=hi, share_positive=float((ds_ > 0).mean()),
                            wilcoxon_p=stats.wilcoxon(ds_.values).pvalue if len(ds_) >= 6 else np.nan))
    out = pd.DataFrame(out)
    out["fdr_q"] = np.nan
    ok = out.wilcoxon_p.notna()
    out.loc[ok, "fdr_q"] = fdr_bh(out.loc[ok, "wilcoxon_p"].values)
    out.to_csv(os.path.join(TAB, "physiology_direction_effects.csv"), index=False)
    log.info("\n" + out.to_string())

    # KUL-AV-GC by condition (ALI, hEOG)
    cond = []
    g = pb[pb.ds == "AVGC"]
    for c, gc in g.groupby("cond"):
        for f in ("ali", "heog"):
            dd = gc.groupby("subj")[[f, "right"]].apply(lambda s: cohen(s[f].values, s.right.values)).dropna()
            if len(dd) >= 3:
                m, lo, hi = boot_ci(dd.values)
                cond.append(dict(cond=c, feature=f, n_subjects=len(dd), median_d=m, ci_lo=lo, ci_hi=hi,
                                 wilcoxon_p=stats.wilcoxon(dd.values).pvalue if len(dd) >= 6 else np.nan))
    pd.DataFrame(cond).to_csv(os.path.join(TAB, "physiology_avgc_by_condition.csv"), index=False)
    log.info("\n" + pd.DataFrame(cond).to_string())

    # switch-locked ERAs
    fig, axs = plt.subplots(1, 3, figsize=(16, 3.8))
    era_rows = []
    for ax, (name, lst, fs_x) in zip(axs, (("ALI (1-s)", era_ali, 1), ("hEOG proxy (4 Hz)", era_heog, 4),
                                           ("EOG ch0-ch3 (4 Hz)", era_eog, 4))):
        if not lst:
            continue
        df = pd.DataFrame(lst)
        subj = np.array([np.mean(np.stack(g.x.values), 0) for _, g in df.groupby("subj")])
        t = (np.arange(subj.shape[1]) / fs_x) - 60
        if subj.ndim == 3:
            for c in range(subj.shape[2]):
                ax.plot(t, np.median(subj[:, :, c], 0), label=f"EOG ch{c}")
            post = subj[:, t >= 0].mean(1) - subj[:, t < 0].mean(1)
            for c in range(subj.shape[2]):
                m_, lo_, hi_ = boot_ci(post[:, c])
                era_rows.append(dict(signal=f"EOG ch{c}", n_subjects=len(subj), n_switches=len(df),
                                     post_minus_pre=m_, ci_lo=lo_, ci_hi=hi_, wilcoxon_p=stats.wilcoxon(post[:, c]).pvalue))
            ax.legend(fontsize=7)
        else:
            lo_, hi_ = np.quantile(np.array([np.median(subj[np.random.default_rng(i).integers(0, len(subj), len(subj))], 0)
                                             for i in range(500)]), [0.025, 0.975], axis=0)
            ax.plot(t, np.median(subj, 0), color=COLORS["AVGC"])
            ax.fill_between(t, lo_, hi_, color=COLORS["AVGC"], alpha=0.2)
            post = subj[:, t >= 0].mean(1) - subj[:, t < 0].mean(1)
            m_, lo_, hi_ = boot_ci(post)
            era_rows.append(dict(signal=name, n_subjects=len(subj), n_switches=len(df), post_minus_pre=m_, ci_lo=lo_, ci_hi=hi_,
                                 wilcoxon_p=stats.wilcoxon(post).pvalue))
        ax.axvline(0, color="k", lw=0.8)
        ax.set_title(f"KUL-AV-GC {name}, switch-locked (n={len(df)} switches, {len(subj)} subjects)", fontsize=9)
        ax.set_xlabel("time from direction switch (s); sign-aligned (+ = L->R)")
    savefig(fig, "d4_switch_locked", pd.DataFrame(era_rows))
    er = pd.DataFrame(era_rows)
    er.to_csv(os.path.join(TAB, "physiology_avgc_switch_locked.csv"), index=False)
    log.info("\n" + er.to_string())
