"""H1: pretrain a foundation model on EEG-stimulus matching with the organisers' data only, one model per LODO fold (the held-out dataset is never seen).
EEG encoder = decoders.CBraModBackbone (TUEG weights + column readout, 64 values per 32-Hz sample); stimulus encoder = small conv net on (env, onset, dom).
Contrastive loss (InfoNCE, temperature 0.1) over 5-s windows: positive = the attended stream of the same window; negatives = the other stream of the same
window and the attended streams of the other windows of the batch. Similarity = mean over the 64 dimensions of the Pearson correlation over time.
LP-FT: new layers only for the first 500 steps, then the encoder too (lr 2e-5, dropout off). Best checkpoint by validation 2-way accuracy (attended vs other).
usage: python fm_pretrain_stim.py HELD|ALL [--steps 4000] -> models/stimpt_cbramod__<HELD>.pt (the CBraModBackbone state, for --set fm_front_ckpt)"""
import argparse
import os
import time

import numpy as np
import torch

import decoders
from common import DS_KEYS
from common_exp import EXP, load_trial, rows_of
from decoders import D, _targets

ap = argparse.ArgumentParser()
ap.add_argument("held")
ap.add_argument("--steps", type=int, default=4000)
ap.add_argument("--batch", type=int, default=32)
ap.add_argument("--seed", type=int, default=0)
a = ap.parse_args()
D.update(targets=["env", "onset", "dom"])
DEV = decoders.DEV
torch.manual_seed(a.seed)
rng = np.random.default_rng(a.seed)
W = 160
tr, va = [], []
for ds in [d for d in DS_KEYS if d != a.held]:
    rows = rows_of(ds)
    vs = rng.choice(sorted(rows.subject.unique()))                           # one validation subject per dataset
    for _, r in rows.iterrows():
        t = load_trial("eaP_n5_t60", r["key"])
        t["_Y"] = _targets(t)
        (va if r["subject"] == vs else tr).append(t)
print(f"held {a.held}: {len(tr)} train / {len(va)} val trials", flush=True)
p = np.array([len(t["eeg"]) - W for t in tr], float)


def batch(T, n, rs, pw=None):
    X = np.empty((n, W, 58), np.float32)
    A = np.empty((n, W, 3), np.float32)
    U = np.empty((n, W, 3), np.float32)
    for j in range(n):
        t = T[rs.choice(len(T), p=pw)] if pw is not None else T[rs.integers(len(T))]
        s = rs.integers(0, len(t["eeg"]) - W)
        c = int(t["att"][s + W // 2])
        X[j], A[j], U[j] = t["eeg"][s:s + W], t["_Y"][s:s + W, c], t["_Y"][s:s + W, 1 - c]
    return [torch.from_numpy(z).to(DEV) for z in (X, A, U)]


class Stim(torch.nn.Module):
    def __init__(self, d=64):
        super().__init__()
        self.net = torch.nn.Sequential(torch.nn.Conv1d(3, d, 9, padding=4), torch.nn.GELU(), torch.nn.Conv1d(d, d, 9, padding=4))

    def forward(self, y):
        return self.net(y.transpose(1, 2)).transpose(1, 2)


def unit(z):
    z = z - z.mean(1, keepdim=True)
    return z / (z.norm(dim=1, keepdim=True) + 1e-6)


enc = decoders.CBraModBackbone(58, W).to(DEV)
for m in enc.enc.modules():
    if isinstance(m, torch.nn.Dropout):
        m.p = 0.0
stim = Stim().to(DEV)
fm = list(enc.enc.parameters())
ids = {id(q) for q in fm}
new = [q for q in enc.parameters() if id(q) not in ids] + list(stim.parameters())
opt = torch.optim.AdamW([dict(params=new, lr=1e-3), dict(params=fm, lr=2e-5)], weight_decay=0.05)
sch = torch.optim.lr_scheduler.LambdaLR(opt, lambda s: min(1.0, (s + 1) / 200) * (0.01 + 0.99 * 0.5 * (1 + np.cos(np.pi * s / a.steps))))
Xv, Av, Uv = batch(va, 512, np.random.default_rng(1))


def evaluate():
    enc.eval(); stim.eval()
    with torch.no_grad():
        acc = []
        for k in range(0, 512, 64):
            z = unit(enc(Xv[k:k + 64]))
            sa, su = (unit(z) * unit(stim(Av[k:k + 64]))).sum(1).mean(1), (unit(z) * unit(stim(Uv[k:k + 64]))).sum(1).mean(1)
            acc.append((sa > su).float())
        return float(torch.cat(acc).mean())


best, t0 = -1.0, time.time()
out = os.path.join(EXP, "models", f"stimpt_cbramod__{a.held}.pt")
for step in range(a.steps):
    frozen = step < 500
    for q in fm:
        q.requires_grad_(not frozen)
    enc.train(); stim.train()
    if frozen:
        enc.enc.eval()
    X, A, U = batch(tr, a.batch, rng, p / p.sum())
    z, ua, uu = unit(enc(X)), unit(stim(A)), unit(stim(U))
    sim = torch.einsum("bwd,cwd->bc", z, ua) / z.shape[-1]                   # (B, B): window b vs attended stream of window c
    hard = (z * uu).sum(1).mean(1, keepdim=True)                             # the other stream of the same window
    loss = torch.nn.functional.cross_entropy(torch.cat([sim, hard], 1) / 0.1, torch.arange(len(z), device=DEV))
    opt.zero_grad()
    loss.backward()
    torch.nn.utils.clip_grad_norm_(new + fm, 1.0)
    opt.step()
    sch.step()
    if (step + 1) % 250 == 0:
        acc = evaluate()
        tag = ""
        if acc > best:
            best, tag = acc, " *saved"
            torch.save(enc.state_dict(), out)
        print(f"step {step + 1} loss {loss.item():.4f} val 2-way acc {acc:.3f}{tag} ({time.time() - t0:.0f}s)", flush=True)
print(f"best val 2-way acc {best:.3f} -> {out}", flush=True)
open(out[:-3] + ".done", "w").write(f"{best:.4f}\n")
