# -*- coding: utf-8 -*-
from __future__ import annotations
import math
import torch
from torch import nn
import torch.nn.functional as F


def _mlp(inp,hid,out,drop=.1):
    return nn.Sequential(nn.Linear(inp,hid),nn.LayerNorm(hid),nn.GELU(),nn.Dropout(drop),nn.Linear(hid,out))

class GlobalDistilledExpert(nn.Module):
    def __init__(self,flat_dim,context_dim,n_faculties,d_model=64,faculty_dim=8,dropout=.1):
        super().__init__();self.fac=nn.Embedding(n_faculties,faculty_dim)
        inp=flat_dim+context_dim+faculty_dim
        self.backbone=nn.Sequential(nn.Linear(inp,2*d_model),nn.LayerNorm(2*d_model),nn.GELU(),nn.Dropout(dropout),
                                    nn.Linear(2*d_model,d_model),nn.LayerNorm(d_model),nn.GELU())
        self.label=nn.Linear(d_model,1);self.teacher=nn.Linear(d_model,3)
    def forward(self,flat,context,faculty):
        h=self.backbone(torch.cat([flat,context,self.fac(faculty)],1))
        return h,self.label(h).squeeze(1),self.teacher(h)

class StudentTrajectoryExpert(nn.Module):
    def __init__(self,input_dim,context_dim,n_faculties,d_model,faculty_dim=8,dropout=.1):
        super().__init__();self.inp=nn.Linear(input_dim,d_model);self.gru=nn.GRU(d_model,d_model,batch_first=True)
        self.query=nn.Linear(d_model,1);self.fac=nn.Embedding(n_faculties,faculty_dim);self.ctx=_mlp(context_dim+faculty_dim,d_model,d_model,dropout)
        self.out=nn.Sequential(nn.Linear(2*d_model,d_model),nn.LayerNorm(d_model),nn.GELU())
    def forward(self,x,mask,context,faculty):
        z=self.inp(x);z,_=self.gru(z);score=self.query(torch.tanh(z)).squeeze(-1);score=score.masked_fill(~mask,-1e4)
        a=torch.softmax(score,1);a=a*mask.float();a=a/(a.sum(1,keepdim=True)+1e-6);h=(z*a.unsqueeze(-1)).sum(1)
        c=self.ctx(torch.cat([context,self.fac(faculty)],1));return self.out(torch.cat([h,c],1))

class CourseCurriculumExpert(nn.Module):
    def __init__(self,input_dim,context_dim,d_model,relation_types=13,dropout=.1):
        super().__init__();self.node=_mlp(input_dim,d_model,d_model,dropout);self.rel=nn.Embedding(relation_types,d_model)
        self.att=nn.Linear(2*d_model,1);self.ctx=_mlp(context_dim,d_model,d_model,dropout)
        self.out=nn.Sequential(nn.Linear(3*d_model,d_model),nn.LayerNorm(d_model),nn.GELU())
    def forward(self,target,neighbors,mask,rel_type,context):
        t=self.node(target);n=self.node(neighbors)+self.rel(rel_type.clamp(min=0,max=self.rel.num_embeddings-1))
        q=t[:,None,:].expand_as(n);score=self.att(torch.cat([q,n],-1)).squeeze(-1).masked_fill(~mask,-1e4)
        a=torch.softmax(score,1);a=a*mask.float();a=a/(a.sum(1,keepdim=True)+1e-6);agg=(n*a.unsqueeze(-1)).sum(1)
        c=self.ctx(context);return self.out(torch.cat([t,agg,c],1))

class CompatibilityExpert(nn.Module):
    def __init__(self,event_dim,course_dim,context_dim,n_faculties,d_model,faculty_dim=8,relation_types=14,dropout=.1):
        super().__init__();self.event=_mlp(event_dim,d_model,d_model,dropout);self.target=_mlp(course_dim,d_model,d_model,dropout);self.rel=nn.Embedding(relation_types,d_model)
        self.q=nn.Linear(d_model,d_model);self.k=nn.Linear(d_model,d_model);self.fac=nn.Embedding(n_faculties,faculty_dim);self.ctx=_mlp(context_dim+faculty_dim,d_model,d_model,dropout)
        self.out=nn.Sequential(nn.Linear(4*d_model,d_model),nn.LayerNorm(d_model),nn.GELU())
    def forward(self,events,mask,rel_type,target_course,context,faculty):
        e=self.event(events)+self.rel(rel_type.clamp(0,self.rel.num_embeddings-1));t=self.target(target_course)
        sc=(self.k(e)*self.q(t)[:,None,:]).sum(-1)/math.sqrt(e.shape[-1]);sc=sc.masked_fill(~mask,-1e4)
        a=torch.softmax(sc,1);a=a*mask.float();a=a/(a.sum(1,keepdim=True)+1e-6);hist=(e*a.unsqueeze(-1)).sum(1)
        cross=t*hist;diff=torch.abs(t-hist);c=self.ctx(torch.cat([context,self.fac(faculty)],1));return self.out(torch.cat([t,hist,cross+diff,c],1))

class MasteryEventExpert(nn.Module):
    def __init__(self,numeric_dim,context_dim,n_faculties,n_component_types,d_model,faculty_dim=8,type_dim=12,dropout=.1):
        super().__init__();self.typ=nn.Embedding(n_component_types,type_dim);self.event=_mlp(numeric_dim+type_dim,d_model,d_model,dropout)
        self.query=nn.Parameter(torch.randn(d_model)*.02);self.fac=nn.Embedding(n_faculties,faculty_dim);self.ctx=_mlp(context_dim+faculty_dim,d_model,d_model,dropout)
        self.out=nn.Sequential(nn.Linear(2*d_model,d_model),nn.LayerNorm(d_model),nn.GELU())
    def forward(self,events,mask,typ,context,faculty):
        e=self.event(torch.cat([events,self.typ(typ.clamp(0,self.typ.num_embeddings-1))],-1));sc=(e*self.query).sum(-1)/math.sqrt(e.shape[-1]);sc=sc.masked_fill(~mask,-1e4)
        a=torch.softmax(sc,1);a=a*mask.float();a=a/(a.sum(1,keepdim=True)+1e-6);h=(e*a.unsqueeze(-1)).sum(1);c=self.ctx(torch.cat([context,self.fac(faculty)],1));return self.out(torch.cat([h,c],1))

class EvidenceExchange(nn.Module):
    def __init__(self,d_model,n_experts=5,heads=4,dropout=.1):
        super().__init__();heads=max(1,min(heads,d_model));
        while d_model%heads:heads-=1
        self.types=nn.Embedding(n_experts,d_model);self.attn=nn.MultiheadAttention(d_model,heads,dropout=dropout,batch_first=True)
        self.gamma=nn.Parameter(torch.full((n_experts,),-2.0));self.norm=nn.LayerNorm(d_model);self.proj=nn.Linear(3*d_model,d_model)
    def forward(self,h,availability):
        b,k,d=h.shape;typ=self.types(torch.arange(k,device=h.device))[None,:,:];x=h+typ
        msg,att=self.attn(x,x,x,key_padding_mask=~availability,need_weights=True,average_attn_weights=False)
        g=torch.sigmoid(self.gamma)[None,:,None];mixed=self.norm(h+g*msg)
        token=self.proj(torch.cat([h,msg,h-msg],-1));token=self.norm(token+mixed)
        token=token*availability.unsqueeze(-1).float();return token,att

class CollaborativeEvidenceMoE(nn.Module):
    def __init__(self,flat_dim,e0_context_dim,fusion_context_dim,e1_context_dim,e2_context_dim,e3_context_dim,e4_context_dim,student_dim,course_dim,relation_dim,component_numeric_dim,n_faculties,n_component_types,d_model=64,
                 dropout=.1,e0_dropout=.15,expert_dropout=.05):
        super().__init__();self.e0_dropout=float(e0_dropout);self.expert_dropout=float(expert_dropout)
        self.e0=GlobalDistilledExpert(flat_dim,e0_context_dim,n_faculties,d_model,dropout=dropout)
        self.e1=StudentTrajectoryExpert(student_dim,e1_context_dim,n_faculties,d_model,dropout=dropout)
        # E2 intentionally has no target-student faculty embedding.  Its private
        # representation is course/curriculum evidence only.
        self.e2=CourseCurriculumExpert(course_dim,e2_context_dim,d_model,dropout=dropout)
        self.e3=CompatibilityExpert(relation_dim,course_dim,e3_context_dim,n_faculties,d_model,dropout=dropout)
        self.e4=MasteryEventExpert(component_numeric_dim,e4_context_dim,n_faculties,n_component_types,d_model,dropout=dropout)
        self.private_heads=nn.ModuleList([nn.Linear(d_model,1) for _ in range(5)])
        self.exchange=EvidenceExchange(d_model,5,4,dropout)
        self.gate=nn.Sequential(nn.Linear(5*d_model+fusion_context_dim,2*d_model),nn.GELU(),nn.Dropout(dropout),nn.Linear(2*d_model,5))
        self.value=nn.ModuleList([nn.Linear(d_model,d_model) for _ in range(5)])
        self.final=nn.Sequential(nn.Linear(d_model+fusion_context_dim,d_model),nn.LayerNorm(d_model),nn.GELU(),nn.Dropout(dropout),nn.Linear(d_model,1))
    def _availability(self,b,device,student_mask,course_neighbor_mask,relation_mask,component_mask):
        a=torch.ones(b,5,dtype=torch.bool,device=device)
        a[:,1]=student_mask.any(1);a[:,2]=True;a[:,3]=relation_mask.any(1);a[:,4]=component_mask.any(1)
        base=a.clone()
        if self.training:
            if self.e0_dropout>0:a[:,0]&=(torch.rand(b,device=device)>=self.e0_dropout)
            if self.expert_dropout>0:
                drop=torch.rand(b,4,device=device)<self.expert_dropout;a[:,1:]&=~drop
            dead=~a.any(1)
            if dead.any():a[dead,0]=True
        return a,base
    def forward(self,flat,e0_context,fusion_context,e1_context,e2_context,e3_context,e4_context,faculty,student_seq,student_mask,course_target,course_neighbors,course_neighbor_mask,course_relation_type,
                relation_events,relation_mask,relation_type,component_events,component_mask,component_type,force_expert_off=None):
        h0,l0,teacher=self.e0(flat,e0_context,faculty)
        h1=self.e1(student_seq,student_mask,e1_context,faculty)
        h2=self.e2(course_target,course_neighbors,course_neighbor_mask,course_relation_type,e2_context)
        h3=self.e3(relation_events,relation_mask,relation_type,course_target,e3_context,faculty)
        h4=self.e4(component_events,component_mask,component_type,e4_context,faculty)
        h=torch.stack([h0,h1,h2,h3,h4],1);private=torch.stack([l0]+[self.private_heads[i](h[:,i,:]).squeeze(1) for i in range(1,5)],1)
        avail,base_avail=self._availability(len(flat),flat.device,student_mask,course_neighbor_mask,relation_mask,component_mask)
        if force_expert_off is not None:
            j=int(force_expert_off);avail[:,j]=False;base_avail[:,j]=False
            dead=~avail.any(1)
            if dead.any():avail[dead,0]=True
        tok,att=self.exchange(h,avail);gate_logits=self.gate(torch.cat([tok.reshape(len(flat),-1),fusion_context],1)).masked_fill(~avail,-1e4)
        w=torch.softmax(gate_logits,1);vals=torch.stack([self.value[i](tok[:,i,:]) for i in range(5)],1);fused=(vals*w.unsqueeze(-1)).sum(1)
        final=self.final(torch.cat([fused,fusion_context],1)).squeeze(1)
        return {'final_logit':final,'private_logits':private,'teacher_logits':teacher,'gate_weights':w,'expert_tokens':h,'collaborative_tokens':tok,'attention':att,'availability':avail,'base_availability':base_avail}
