# model/hftt.py
import torch
import torch.nn as nn
import torch.nn.functional as F
from BEATs.BEATs import BEATs, BEATsConfig


class FrequencyTemporalAttention(nn.Module):
    def __init__(self, dim, num_heads=8, qkv_bias=True, dropout=0.1):
        super().__init__()
        self.num_heads = num_heads
        self.head_dim = dim // num_heads
        self.scale = self.head_dim ** -0.5

        self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)
        self.proj = nn.Linear(dim, dim)
        self.dropout = nn.Dropout(dropout)

    def forward(self, x):
        B, N, D = x.shape
        qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, self.head_dim)
        qkv = qkv.permute(2, 0, 3, 1, 4)
        q, k, v = qkv[0], qkv[1], qkv[2]

        attn = (q @ k.transpose(-2, -1)) * self.scale
        attn = attn.softmax(dim=-1)
        attn = self.dropout(attn)

        x = (attn @ v).transpose(1, 2).reshape(B, N, D)
        x = self.proj(x)
        return x


class MultiScaleTemporalPooling(nn.Module):
    def __init__(self, dim, scales=(1, 2, 4)):
        super().__init__()
        self.scales = scales
        self.attention_pools = nn.ModuleList([
            nn.Sequential(
                nn.Linear(dim, dim // 4),
                nn.Tanh(),
                nn.Linear(dim // 4, 1),
            )
            for _ in scales
        ])
        self.fusion = nn.Sequential(
            nn.Linear(dim * len(scales), dim),
            nn.LayerNorm(dim),
            nn.GELU(),
            nn.Dropout(0.1),
        )

    def forward(self, x):
        B, N, D = x.shape
        scale_features = []
        for scale, attn_pool in zip(self.scales, self.attention_pools):
            if scale > 1:
                x_scaled = x.view(B, N // scale, scale, D).mean(dim=2)
            else:
                x_scaled = x
            attn_weights = attn_pool(x_scaled)
            attn_weights = F.softmax(attn_weights, dim=1)
            pooled = torch.sum(x_scaled * attn_weights, dim=1)
            scale_features.append(pooled)
        fused = torch.cat(scale_features, dim=-1)
        return self.fusion(fused)


class FrameMultiscaleProj(nn.Module):
    """Concat multi-scale frame features then project back to encoder_dim."""

    def __init__(self, dim):
        super().__init__()
        self.proj = nn.Sequential(
            nn.Linear(dim * 3, dim),
            nn.LayerNorm(dim),
            nn.GELU(),
        )

    def forward(self, x):
        B, N, D = x.shape
        x2 = F.avg_pool1d(x.transpose(1, 2), kernel_size=2, stride=2, ceil_mode=True).transpose(1, 2)
        x4 = F.avg_pool1d(x.transpose(1, 2), kernel_size=4, stride=4, ceil_mode=True).transpose(1, 2)
        x2_up = F.interpolate(x2.transpose(1, 2), size=N, mode='linear', align_corners=False).transpose(1, 2)
        x4_up = F.interpolate(x4.transpose(1, 2), size=N, mode='linear', align_corners=False).transpose(1, 2)
        return self.proj(torch.cat([x, x2_up, x4_up], dim=-1))


class HybridHFTT(nn.Module):
    def __init__(
        self,
        num_target_classes: int = 4,
        beats_model_path: str = './pretrained_models/BEATs_iter3_plus_AS2M.pt',
        freeze_encoder: bool = False,
        use_multi_scale: bool = True,
        spec_transform=None,
        frame_multiscale: bool = False,
    ):
        super().__init__()
        checkpoint = torch.load(beats_model_path, map_location='cpu')
        cfg = BEATsConfig({
            **checkpoint['cfg'],
            'predictor_class': num_target_classes,
            'finetuned_model': False,
            'spec_transform': spec_transform,
        })
        self.beats = BEATs(cfg)
        self.beats.load_state_dict(checkpoint['model'])
        if freeze_encoder:
            for param in self.beats.parameters():
                param.requires_grad = False

        self.encoder_dim = cfg.encoder_embed_dim
        self.final_feat_dim = self.encoder_dim
        self.use_multi_scale = use_multi_scale
        self.frame_multiscale = frame_multiscale

        if use_multi_scale:
            self.freq_temp_attn = FrequencyTemporalAttention(
                dim=self.encoder_dim, num_heads=12, dropout=0.1)
            self.multi_scale_pool = MultiScaleTemporalPooling(dim=self.encoder_dim)
        else:
            self.attention_pool = nn.Sequential(
                nn.Linear(self.encoder_dim, self.encoder_dim // 4),
                nn.Tanh(),
                nn.Linear(self.encoder_dim // 4, 1),
            )
        if frame_multiscale:
            self.frame_ms_proj = FrameMultiscaleProj(self.encoder_dim)

    def encode_frames(self, source, padding_mask=None):
        """Return per-frame features [B, T, D] after freq-temporal attention."""
        if padding_mask is not None:
            x, _ = self.beats.extract_features(source, padding_mask)
        else:
            x, _ = self.beats.extract_features(source)
        if self.use_multi_scale:
            x = x + self.freq_temp_attn(x)
        if self.frame_multiscale:
            x = self.frame_ms_proj(x)
        return x

    def forward(self, source, padding_mask=None, training=False):
        x = self.encode_frames(source, padding_mask)
        if self.use_multi_scale:
            x_pooled = self.multi_scale_pool(x)
            return x_pooled.unsqueeze(1)
        attn_weights = self.attention_pool(x)
        attn_weights = F.softmax(attn_weights, dim=1)
        x_pooled = torch.sum(x * attn_weights, dim=1)
        return x_pooled.unsqueeze(1)


def get_hftt_model(
    beats_model_path='./pretrained_models/BEATs_iter3_plus_AS2M.pt',
    freeze_encoder=False,
    spec_transform=None,
    frame_multiscale=False,
):
    return HybridHFTT(
        num_target_classes=4,
        beats_model_path=beats_model_path,
        freeze_encoder=freeze_encoder,
        use_multi_scale=True,
        spec_transform=spec_transform,
        frame_multiscale=frame_multiscale,
    )
