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

EPS=1e-7


def seed_everything(seed=42):
    random.seed(seed);np.random.seed(seed);torch.manual_seed(seed)
    if torch.cuda.is_available():torch.cuda.manual_seed_all(seed)


def choose_device(name='auto'):
    if name=='auto':return torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    return torch.device(name)


def logit_tensor(q):
    q=torch.clamp(q,EPS,1-EPS)
    return torch.log(q)-torch.log1p(-q)


class ResidualExpert(nn.Module):
    def __init__(self,dim,hidden,delta_max,dropout):
        super().__init__();self.delta_max=float(delta_max)
        self.net=nn.Sequential(nn.Linear(dim,hidden),nn.GELU(),nn.Dropout(dropout),nn.Linear(hidden,1))
        nn.init.zeros_(self.net[-1].weight);nn.init.zeros_(self.net[-1].bias)
    def forward(self,h):
        return self.delta_max*torch.tanh(self.net(h).squeeze(-1))


class ResidualMoE(nn.Module):
    def __init__(self,input_dim,n_faculties,n_experts=4,faculty_dim=8,encoder_hidden=256,representation_dim=128,
                 expert_hidden=64,delta_max=2.0,dropout=.10,router_temperature=1.0):
        super().__init__();self.n_experts=int(n_experts);self.delta_max=float(delta_max);self.router_temperature=float(router_temperature)
        self.faculty_embedding=nn.Embedding(int(n_faculties),int(faculty_dim))
        din=int(input_dim)+int(faculty_dim)
        self.encoder=nn.Sequential(nn.Linear(din,int(encoder_hidden)),nn.GELU(),nn.LayerNorm(int(encoder_hidden)),nn.Dropout(dropout),
                                   nn.Linear(int(encoder_hidden),int(representation_dim)),nn.GELU(),nn.LayerNorm(int(representation_dim)))
        self.experts=nn.ModuleList([ResidualExpert(int(representation_dim),int(expert_hidden),delta_max,dropout) for _ in range(self.n_experts)])
        self.router=nn.Sequential(nn.Linear(int(representation_dim),max(32,int(representation_dim)//2)),nn.GELU(),nn.Dropout(dropout),
                                  nn.Linear(max(32,int(representation_dim)//2),self.n_experts+1))

    def forward(self,x,faculty_idx,q):
        e=self.faculty_embedding(faculty_idx)
        h=self.encoder(torch.cat([x,e],dim=1))
        residuals=torch.stack([m(h) for m in self.experts],dim=1)
        logits=self.router(h)/max(self.router_temperature,1e-6)
        weights=torch.softmax(logits,dim=1)
        correction=(weights[:,1:]*residuals).sum(dim=1)
        zq=logit_tensor(q)
        final_logit=zq+correction
        cf_logit=zq[:,None]+residuals
        return {'final_logit':final_logit,'final_prob':torch.sigmoid(final_logit),'weights':weights,
                'residuals':residuals,'counterfactual_prob':torch.sigmoid(cf_logit),'representation':h,
                'correction':correction}


def capability_targets(y,q,counterfactual_prob,temperature=.25):
    y=y.float();q=torch.clamp(q,EPS,1-EPS);p=torch.clamp(counterfactual_prob,EPS,1-EPS)
    lq=F.binary_cross_entropy(q,y,reduction='none')
    yy=y[:,None].expand_as(p)
    lk=F.binary_cross_entropy(p,yy,reduction='none')
    utility=lq[:,None]-lk
    z=torch.cat([torch.zeros((len(y),1),device=y.device,dtype=y.dtype),utility],dim=1)/max(float(temperature),1e-6)
    target=torch.softmax(z.detach(),dim=1)
    return target,utility


def specialization_mutual_information(weights):
    # Conditional specialization among residual experts only. NULL mass does not
    # get pushed up or down by this regularizer.
    nonnull=weights[:,1:]
    mass=nonnull.sum(dim=1).clamp_min(1e-8)
    r=nonnull/mass[:,None]
    md=mass.detach()
    denom=md.sum().clamp_min(1e-8)
    mean_r=(r*md[:,None]).sum(dim=0)/denom
    h_mean=-(mean_r*torch.log(mean_r.clamp_min(1e-8))).sum()
    h_row=-(r*torch.log(r.clamp_min(1e-8))).sum(dim=1)
    mean_h=(h_row*md).sum()/denom
    return h_mean-mean_h


def residual_moe_loss(output,y,q,router_lambda=.5,specialization_lambda=.01,magnitude_lambda=.01,utility_temperature=.25):
    y=y.float();q=q.float()
    pred=F.binary_cross_entropy_with_logits(output['final_logit'],y)
    target,utility=capability_targets(y,q,output['counterfactual_prob'],utility_temperature)
    w=output['weights'].clamp_min(1e-8)
    router=F.kl_div(torch.log(w),target,reduction='batchmean')
    mi=specialization_mutual_information(w)
    mag=(w[:,1:].detach()*output['residuals'].pow(2)).sum(dim=1).mean()
    total=pred+float(router_lambda)*router-float(specialization_lambda)*mi+float(magnitude_lambda)*mag
    return {'total':total,'prediction':pred,'router':router,'specialization_mi':mi,'magnitude':mag,'utility':utility.detach(),'capability_target':target.detach()}


def router_diagnostics(weights,residuals):
    w=np.asarray(weights,float);r=np.asarray(residuals,float)
    ent=-(w*np.log(np.clip(w,1e-12,1))).sum(axis=1)
    norm=ent/max(math.log(w.shape[1]),1e-12)
    out={'mean_null_weight':float(w[:,0].mean()),'mean_entropy':float(ent.mean()),'normalized_entropy':float(norm.mean()),
         'mean_max_weight':float(w.max(axis=1).mean())}
    for j in range(w.shape[1]-1):
        out[f'mean_weight_expert_{j+1}']=float(w[:,j+1].mean())
        out[f'mean_residual_expert_{j+1}']=float(r[:,j].mean())
        out[f'std_residual_expert_{j+1}']=float(r[:,j].std())
    return out
