"""LaBraM, EEGPT, BrainOmni as drop-in backbones of decoders.build_backbone: (B, W, C) at 32 Hz -> (B, W, 64), like CBraModBackbone.
Each keeps its pretrained weights and is fine-tuned end to end. Only the ends are new: a linear map of the input columns onto the channels the model knows
(identity for the electrodes, the stimulus columns start at 0 where a channel is free) and a linear output per token that gives the 32-Hz samples of that token.
The input is linearly interpolated from 32 Hz to the model's rate (the content stays below 9 Hz). Whole-window models: causality comes from the read-out, as for vlaai_ws."""
import argparse
import json
import os
import re
import sys
import types

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

from common import MONTAGES
from common_exp import EXP, FS

FM = os.path.join(EXP, "..", "external_data", "foundation_models")
MONT = MONTAGES["b64ustc58"]
SCALE = 0.15                                                 # the models take uV / 100; our z-scored EEG has sd 1, EEG sd is about 15 uV


def interp(x, n):
    """(B, C, T) -> (B, C, n)."""
    return F.interpolate(x, size=n, mode="linear", align_corners=True)


class LaBraMBackbone(torch.nn.Module):
    """1-s patches of every column; attention over all patches of all columns."""

    def __init__(self, C, W, d=64):
        super().__init__()
        from fm_models.labram_model import labram_base_patch200_200
        import zipfile
        names = eval(re.search(r"standard_1020 = (\[.*?\])", zipfile.ZipFile(os.path.join(FM, "labram", "code_main.zip")).read("LaBraM-main/utils.py").decode(), re.S).group(1))
        self.net = labram_base_patch200_200(EEG_size=1600, num_classes=0, use_rel_pos_bias=False, use_abs_pos_emb=True, init_values=0.1, qkv_bias=False)
        with torch.serialization.safe_globals([argparse.Namespace, np.core.multiarray.scalar, np.dtype, np.dtypes.Float64DType, np.dtypes.Float32DType]):   # its training arguments
            sd = torch.load(os.path.join(FM, "labram", "labram-base.pth"), map_location="cpu", weights_only=True)
        sd = sd.get("model", sd)
        sd = {k[len("student."):]: v for k, v in sd.items() if k.startswith("student.")} or sd
        r = self.net.load_state_dict({k: v for k, v in sd.items() if not k.startswith("head")}, strict=False)
        print("LaBraM weights: missing", len(r.missing_keys), r.missing_keys[:4], "unexpected", len(r.unexpected_keys), r.unexpected_keys[:4], flush=True)
        el = [names.index(c) + 1 for c in MONT[:C]]
        free = [i + 1 for i in range(len(names)) if i + 1 not in el][: max(C - 58, 0)]
        self.register_buffer("chans", torch.tensor([0] + el[:58] + free))
        from decoders import ColumnReadout
        self.out = ColumnReadout(C, 200, FS, d)

    def forward(self, x):
        B, W, C = x.shape
        N = W // FS
        P = interp(x.transpose(1, 2), N * 200).reshape(B, C, N, 200) * SCALE
        tok = self.net.forward_features(P, input_chans=self.chans, return_patch_tokens=True)         # (B, C N, 200)
        return self.out(tok.reshape(B, C, N, 200))


class EEGPTBackbone(torch.nn.Module):
    """0.25-s patches at 256 Hz, attention over the channels of a patch; no attention over time inside the encoder (the head of the network does the rest)."""

    def __init__(self, C, W, d=64):
        super().__init__()
        from safetensors.torch import load_file
        from fm_models.eegpt_model import CHANNEL_DICT, EEGTransformer
        sd = load_file(os.path.join(FM, "eegpt", "braindecode_model.safetensors"))
        sd = {k[len("target_encoder."):]: v for k, v in sd.items() if k.startswith("target_encoder.")}
        depth = 1 + max(int(k.split(".")[1]) for k in sd if k.startswith("blocks."))
        K = sd["chan_embed.weight"].shape[0]
        self.enc = EEGTransformer(img_size=(K, W * 8), patch_size=64, embed_dim=512, embed_num=4, depth=depth, num_heads=8, mlp_ratio=4.0, drop_path_rate=0.0, qkv_bias=True)
        r = self.enc.load_state_dict(sd, strict=False)
        print("EEGPT weights: depth", depth, "missing", len(r.missing_keys), r.missing_keys[:4], "unexpected", len(r.unexpected_keys), r.unexpected_keys[:4], flush=True)
        self.map = torch.nn.Linear(C, K, bias=False)
        with torch.no_grad():
            self.map.weight.normal_(0.0, 0.02)                       # small, not 0: with 0 the two stimulus streams enter identically
            self.map.weight[:, :min(C, 58)] = 0.0
            for i, c in enumerate(MONT[:C]):
                if c in CHANNEL_DICT and CHANNEL_DICT[c] < K:
                    self.map.weight[CHANNEL_DICT[c], i] = 1.0
        self.out = torch.nn.Linear(4 * 512, 8 * d)
        self.d = d

    def forward(self, x):
        B, W, C = x.shape
        z = interp(self.map(x).transpose(1, 2), W * 8) * SCALE / 10                                   # scale of their training data: mV-like, set by the map when trained
        tok = self.enc(z)                                                                             # (B, N, 4 * 512)
        return self.out(tok.reshape(B, W // 8, -1)).reshape(B, W, self.d)


def _brainomni_code():
    root = os.path.join(FM, "brainomni", "code", "BrainOmni-main")
    if root not in sys.path:
        sys.path.append(root)
    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
    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})


class BrainOmniBackbone(torch.nn.Module):
    """2-s segments at 256 Hz, 8 tokens per segment for each of 16 latent sources; the quantiser of its pretraining is skipped so that gradients reach the encoder."""

    def __init__(self, C, W, d=64):
        super().__init__()
        _brainomni_code()
        from brainomni.model import BrainOmni
        from fm_embed import electrode_pos
        cfg = json.load(open(os.path.join(FM, "brainomni", "tiny_model_cfg.json")))
        cfg["lm_dropout"] = 0.0                                      # its attention applies dropout even in eval mode, which would break the exact antisymmetry
        self.net = BrainOmni(**cfg)
        r = self.net.load_state_dict(torch.load(os.path.join(FM, "brainomni", "tiny_BrainOmni.pt"), map_location="cpu", weights_only=True), strict=False)
        print("BrainOmni weights: missing", len(r.missing_keys), "unexpected", len(r.unexpected_keys), flush=True)
        del self.net.tokenizer.quantizer, self.net.tokenizer.decoder, self.net.predict_head
        P = torch.from_numpy(electrode_pos())
        P = P - P.mean(0, keepdim=True)
        P = P / torch.sqrt(3 * (P ** 2).sum(1).mean())
        self.register_buffer("pos", torch.cat([P, torch.zeros(58, 3)], 1))
        self.map = torch.nn.Linear(C, 58, bias=False)
        with torch.no_grad():
            self.map.weight.normal_(0.0, 0.02)
            self.map.weight[:, :58] = 0.0
            self.map.weight[torch.arange(58), torch.arange(58)] = 1.0
        self.seg = (W + 2 * FS - 1) // (2 * FS)                      # 2-s segments; a 5-s window is padded on the left to 6 s
        self.keep = W // 8
        from decoders import ColumnReadout
        self.out = ColumnReadout(16, 256, 8, d)                      # one readout per latent source

    def forward(self, x):
        B, W, C = x.shape
        z = self.map(x).transpose(1, 2)
        z = z - z.mean(1, keepdim=True)
        z = z / (z.std((1, 2), keepdim=True) + 1e-5)
        z = F.pad(z, (self.seg * 2 * FS - W, 0))
        z = interp(z, self.seg * 512)                                # (B, 58, T)
        tk = self.net.tokenizer
        se = tk.sensor_embed(self.pos[None].expand(B, -1, -1), torch.zeros(B, 58, dtype=torch.long, device=x.device))
        with torch.backends.cudnn.flags(enabled=False):             # its LSTM: cuDNN cannot backpropagate through an RNN in eval mode (frozen phase)
            f = tk.encoder(tk.unfold(z, overlap_ratio=0.0), se)      # (B, 16, S, 8, D)
        f = f.reshape(B, f.shape[1], -1, f.shape[-1])                # (B, 16, S 8, D)
        f = self.net.projection(f + tk.encoder.neuros.type_as(f).detach().view(1, 16, 1, -1))
        for blk in self.net.blocks[:-1]:
            f = blk(f)
        return self.out(f[:, :, -self.keep:])


class BIOTBackbone(torch.nn.Module):
    """Spectrogram tokens (1-s FFT, 0.5-s hop) of 18 channels; the columns are mapped onto the 18 channels by a linear layer. Pretrained on MGH + SHHS."""

    def __init__(self, C, W, d=64):
        super().__init__()
        sys.path.append(os.path.join(FM, "pydeps"))
        from fm_models.biot_model import BIOTEncoder
        self.enc = BIOTEncoder(emb_size=256, heads=8, depth=4, n_channels=18, n_fft=200, hop_length=100)
        sd = torch.load(os.path.join(FM, "biot", "EEG-SHHS+PREST-18-channels.ckpt"), map_location="cpu", weights_only=True)
        r = self.enc.load_state_dict(sd, strict=False)
        print("BIOT weights: missing", len(r.missing_keys), r.missing_keys[:3], "unexpected", len(r.unexpected_keys), r.unexpected_keys[:3], flush=True)
        self.map = torch.nn.Linear(C, 18, bias=False)
        from decoders import ColumnReadout
        self.out = ColumnReadout(18, 256, 16, d)

    def forward(self, x):
        B, W, C = x.shape
        z = interp(self.map(x).transpose(1, 2), W // FS * 200) * SCALE                                # (B, 18, 200 N)
        z = F.pad(z, (0, 100))                                                                        # 2 N tokens per column
        seq = []
        for i in range(18):
            e = self.enc.patch_embedding(self.enc.stft(z[:, i:i + 1]))                                # (B, L, 256)
            seq.append(self.enc.positional_encoding(e + self.enc.channel_tokens(self.enc.index[i])[None, None]))
        e = self.enc.transformer(torch.cat(seq, 1))
        return self.out(e.reshape(B, 18, -1, 256))
