"""Fixed foundation-model embeddings for D1: every 0.25 s, the 1-s EEG patch that ends there, alone (no later or earlier patch), through the frozen encoder.
Patch k ends at trial time t0 + (k + 1) / 4 s (32-Hz sample 8k + 7 of the cached trial). Input: cache/fm_input (64 Hz, 0.5-20 Hz, running RMS = 1) -> 200 Hz by linear
interpolation inside the patch -> x 0.15 (the model expects uV / 100; EEG sd about 15 uV). Output per step: encoder tokens averaged over the left, midline and right
electrodes (600 values), then a PCA to 32 values fitted on Fuglsang2020 only, which is no LODO test set and no real-switch set.
usage: python fm_embed.py cbramod -> cache/fm_cbramod/<key>.npy (n_hop, 32) float32, pca.npz"""
import glob
import os
import re
import sys

import numpy as np
import torch
import torch.nn.functional as F

from common_exp import EXP, rows_of                                        # first: it puts common.py on sys.path
from common import DS_KEYS, MONTAGES

XT = os.path.join(EXP, "..", "external_data")
DEV = "cuda"
SCALE, DIM = 0.15, 32
MONT = MONTAGES["b64ustc58"]


def groups():
    import re
    side = [0 if re.search(r"Z$", c) else (1 if int(re.search(r"(\d+)$", c).group(1)) % 2 else 2) for c in MONT]
    return [np.flatnonzero(np.array(side) == g) for g in (1, 0, 2)]


def trials():
    out = []
    for ds in DS_KEYS:
        out += [(r["key"], os.path.join(EXP, "cache", "eaP_n5_t60", r["key"] + ".npz")) for _, r in rows_of(ds).iterrows()]
    for nm in ("Fuglsang2020", "AASD", "Carta2026"):
        out += [(os.path.basename(f)[:-4], f) for f in sorted(glob.glob(os.path.join(XT, nm, "cache", "eaP_n5_t60", "*.npz")))]
    return out


def model():
    from fm_models.cbramod import CBraMod
    m = CBraMod()
    m.load_state_dict(torch.load(os.path.join(XT, "foundation_models", "cbramod", "pretrained_weights.pth"), map_location="cpu", weights_only=True))
    m.proj_out = torch.nn.Identity()
    return m.to(DEV).eval()


@torch.no_grad()
def embed(net, key, c32, G, bs=256, width=600):
    z = np.load(c32)
    n32, t0 = len(z["eeg"]), float(z["t0"])
    x = np.load(os.path.join(EXP, "cache", "fm_input", key + ".npy")).astype(np.float32)
    ends = np.round((t0 + (np.arange(n32 // 8) + 1) / 4) * 64).astype(int)
    ends = np.minimum(ends, len(x))
    out = np.empty((len(ends), width), np.float32)
    for i in range(0, len(ends), bs):
        P = np.stack([x[max(e - 64, 0):e] if e >= 64 else np.pad(x[:e], ((64 - e, 0), (0, 0))) for e in ends[i:i + bs]])       # (b, 64, 58)
        P = torch.from_numpy(P).to(DEV).permute(0, 2, 1) * SCALE                                                             # (b, 58, 64)
        P = F.interpolate(P, size=200, mode="linear", align_corners=True)[:, :, None, :]                                     # (b, 58, 1, 200)
        tok = net(P)[:, :, 0, :]                                                                                             # (b, 58, 200)
        out[i:i + bs] = torch.cat([tok[:, g].mean(1) for g in G], 1).cpu().numpy()
    return out


def brainomni_model(name="tiny"):
    """Official code, with stubs for what is unused in inference: deepspeed.comm (distributed training), einx.get_at (plain indexing here),
    vector_quantize_pytorch.rotate_to (training only)."""
    import json
    import types
    root = os.path.join(XT, "foundation_models", "brainomni")
    sys.path.append(os.path.join(root, "code", "BrainOmni-main"))
    ds, cm, ex = types.ModuleType("deepspeed"), types.ModuleType("deepspeed.comm"), types.ModuleType("einx")
    cm.is_initialized = lambda: False
    ds.comm = cm
    ex.get_at = lambda pattern, table, idx: table[idx]
    vq, vq2 = types.ModuleType("vector_quantize_pytorch"), types.ModuleType("vector_quantize_pytorch.vector_quantize_pytorch")
    vq2.rotate_to = lambda src, tgt: tgt                                        # gradient trick of training; its value is tgt
    vq.vector_quantize_pytorch = vq2
    sys.modules.update({"deepspeed": ds, "deepspeed.comm": cm, "einx": ex, "vector_quantize_pytorch": vq, "vector_quantize_pytorch.vector_quantize_pytorch": vq2})
    from brainomni.model import BrainOmni
    m = BrainOmni(**json.load(open(os.path.join(root, f"{name}_model_cfg.json"))))
    miss = m.load_state_dict(torch.load(os.path.join(root, f"{name}_BrainOmni.pt"), map_location="cpu", weights_only=True), strict=False)
    print("BrainOmni loaded; missing", len(miss.missing_keys), "unexpected", len(miss.unexpected_keys), flush=True)
    return m.to(DEV).eval()


def electrode_pos():
    """(58, 3) from MNE's standard_1005.elc (BSD), centred and scaled as factory.utils.normalize_pos."""
    t = open(os.path.join(XT, "foundation_models", "brainomni", "standard_1005.elc")).read().split("\n")
    i, j = t.index("Positions"), t.index("Labels")
    xyz = [[float(v) for v in re.split(r"[:\s]+", ln.strip())[-3:]] for ln in t[i + 1:j] if ln.strip()]
    lab = [ln.strip().upper() for ln in t[j + 1:] if ln.strip()]
    d = dict(zip(lab, xyz))
    return np.array([d[c] for c in MONT], np.float32)


@torch.no_grad()
def embed_bo(net, key, c32, P, bs=128):
    """SCREENING ONLY, not causal inside the patch: consecutive 2-s patches, 256 Hz, reference and scale inside the patch, all 8 tokens (0.25 s each) of the 16 neural
    sources, so a token may use up to 2 s of later EEG. An optimistic bound, 16 times cheaper than one patch per hop."""
    z = np.load(c32)
    n32, t0 = len(z["eeg"]), float(z["t0"])
    x = np.load(os.path.join(EXP, "cache", "fm_input", key + ".npy")).astype(np.float32)
    pres = x.std(0) > 0
    hops = n32 // 8
    ends = np.minimum(np.round((t0 + (np.arange(7, hops + 7, 8) + 1) / 4) * 64).astype(int), len(x))
    pos = P[pres] - P[pres].mean(0, keepdims=True)
    pos = pos / np.sqrt(3 * (pos ** 2).sum(1).mean())
    pos = torch.from_numpy(np.c_[pos, np.zeros((len(pos), 3), np.float32)]).to(DEV)
    out = []
    for i in range(0, len(ends), bs):
        Q = np.stack([np.pad(x[max(e - 128, 0):e][:, pres], ((128 - min(e, 128), 0), (0, 0))) for e in ends[i:i + bs]])       # (b, 128, C)
        Q = torch.from_numpy(Q).to(DEV).permute(0, 2, 1)
        Q = Q - Q.mean(1, keepdim=True)
        Q = Q / (Q.std((1, 2), keepdim=True) + 1e-5)
        Q = F.interpolate(Q, size=512, mode="linear", align_corners=True)
        f = net.encode(Q, pos[None].expand(len(Q), -1, -1), torch.zeros(Q.shape[:2], dtype=torch.long, device=DEV))        # (b, 16, 8, D)
        out.append(f.permute(0, 2, 1, 3).reshape(len(Q) * 8, -1).cpu().numpy().astype(np.float32))
    return np.concatenate(out)[:hops]


def save_pca(T, fn, name, dim):
    OUT = os.path.join(EXP, "cache", "fm_" + name)
    os.makedirs(OUT, exist_ok=True)
    fit = [t for t in T if t[0].startswith("FUG_")]
    A = np.concatenate([fn(k, f)[::8] for k, f in fit])
    mu = A.mean(0)
    C = np.cov((A - mu)[:: max(1, len(A) // 40000)].T)
    ev, U = np.linalg.eigh(C)
    W = U[:, ::-1][:, :dim] / np.sqrt(np.maximum(ev[::-1][:dim], 1e-12))
    print(name, "PCA on", len(fit), "trials,", len(A), "patches; variance kept", round(float(ev[::-1][:dim].sum() / ev.sum()), 3), flush=True)
    for i, (k, f) in enumerate(T):
        np.save(os.path.join(OUT, k + ".npy"), ((fn(k, f) - mu) @ W).astype(np.float32))
        if i % 500 == 0:
            print(i, "/", len(T), k, flush=True)
    open(os.path.join(OUT, "DONE"), "w").write("ok\n")


if __name__ == "__main__":
    name = sys.argv[1]
    T = trials()
    if name == "cbramod":                                                       # D1: 32 dims
        net, G = model(), groups()
        save_pca(T, lambda k, f: embed(net, k, f, G), name, DIM)
    elif name == "cbramod_nopca":                                               # the same 600 values, 200 principal directions keep nearly everything
        net, G = model(), groups()
        save_pca(T, lambda k, f: embed(net, k, f, G), name, 200)
    elif name == "cbramod_ch":                                                  # no regions: every electrode kept, 8 dims per electrode
        net = model()
        save_pca(T, lambda k, f: embed(net, k, f, [np.array([c]) for c in range(58)], width=58 * 200).reshape(-1, 58, 200)[:, :, :8].reshape(-1, 58 * 8), name, 200)
    elif name == "brainomni":
        net, P = brainomni_model(), electrode_pos()
        save_pca(T, lambda k, f: embed_bo(net, k, f, P), name, 64)
