"""Suite30 JSPR: coherent marginal and correlated joint failure probabilities.

A single parameter set learns course risks and, in joint modes, a common
Bernoulli risk state. No target labels enter forward(). Inputs are pre-outcome.
"""
from __future__ import annotations
import math
import torch
from torch import nn
from torch.nn import functional as F

TRAIN_MODES = ("course_only", "portfolio_independent", "joint_no_attention", "joint")

def poisson_binomial_pmf(prob, valid):
    """Exact independent count PMF, padded registrations excluded; [B,M+1]."""
    b, m = prob.shape
    dp = prob.new_zeros(b, m + 1)
    dp[:, 0] = 1.
    for j in range(m):
        p = torch.where(valid[:, j], prob[:, j], torch.zeros_like(prob[:, j]))
        dp = dp * (1 - p[:, None]) + F.pad(dp[:, :-1], (1, 0)) * p[:, None]
    return dp

def joint_log_prob(y, valid, p_low, p_high, mixture):
    """Per-group log P(Y vector | X), stable log-sum-exp mixture."""
    y = y.float()
    mask = valid.float()
    def state_loglik(p):
        ll = y * torch.log(p.clamp_min(1e-7)) + (1-y) * torch.log1p(-p.clamp(max=1-1e-7))
        return (ll * mask).sum(dim=1)
    w = mixture.clamp(1e-7, 1-1e-7)
    return torch.logaddexp(torch.log1p(-w) + state_loglik(p_low),
                           torch.log(w) + state_loglik(p_high))

class JointPortfolioRisk(nn.Module):
    """Forward x [B,M,D], valid/faculty/semester_z [B,M]."""
    def __init__(self, n_features, n_faculties, width=128, heads=4,
                 dropout=.20, course_dim=17):
        super().__init__()
        if width < 4 or heads < 1 or width % heads:
            raise ValueError("width must be divisible by heads")
        if n_features < course_dim:
            raise ValueError("input dimension smaller than 17 course features")
        self.n_features = int(n_features)
        self.n_faculties = int(n_faculties)
        self.width, self.heads, self.head_dim = width, heads, width // heads
        self.course_dim = int(course_dim)
        self.faculty_emb = nn.Embedding(n_faculties, 12)
        self.row_encoder = nn.Sequential(
            nn.Linear(n_features + 13, width), nn.LayerNorm(width), nn.GELU(),
            nn.Dropout(dropout), nn.Linear(width, width), nn.LayerNorm(width), nn.GELU())
        self.course_encoder = nn.Sequential(nn.Linear(course_dim, width),
            nn.LayerNorm(width), nn.GELU(), nn.Linear(width, width))
        self.q = nn.Linear(width, width)
        self.k = nn.Linear(width, width)
        self.v = nn.Linear(width, width)
        self.peer_out = nn.Linear(width, width)
        self.peer_gate = nn.Sequential(nn.Linear(width*2, width//2),
                                      nn.GELU(), nn.Linear(width//2, 1))
        self.peer_effect = nn.Sequential(nn.Linear(width*2, width), nn.GELU(),
                                         nn.Dropout(dropout), nn.Linear(width, 1))
        self.row_head = nn.Sequential(nn.Linear(width, width//2), nn.GELU(),
                                      nn.Dropout(dropout), nn.Linear(width//2, 1))
        self.group_encoder = nn.Sequential(nn.Linear(width*3+1, width),
            nn.LayerNorm(width), nn.GELU(), nn.Dropout(dropout))
        self.state_head = nn.Linear(width, 3)  # state prior, down-shift, up-shift
        nn.init.zeros_(self.peer_effect[-1].weight)
        nn.init.zeros_(self.peer_effect[-1].bias)
        with torch.no_grad():
            self.state_head.bias.copy_(torch.tensor([-1.5, -1.5, -1.5]))

    @staticmethod
    def pool_mean(x, valid):
        w = valid.unsqueeze(-1).to(x.dtype)
        return (x*w).sum(1) / w.sum(1).clamp_min(1.)

    def peer_attention(self, h, c, valid):
        b, m, d = h.shape
        q = self.q(h).reshape(b,m,self.heads,self.head_dim).transpose(1,2)
        k = self.k(c).reshape(b,m,self.heads,self.head_dim).transpose(1,2)
        v = self.v(c).reshape(b,m,self.heads,self.head_dim).transpose(1,2)
        allowed = (valid[:,None,None,:] &
                  ~torch.eye(m,device=h.device,dtype=torch.bool)[None,None,:,:])
        scores = (q @ k.transpose(-1,-2))/math.sqrt(self.head_dim)
        scores = scores.masked_fill(~allowed, -1e4)
        alpha = F.softmax(scores, dim=-1)*allowed.to(scores.dtype)
        alpha = alpha/alpha.sum(-1,keepdim=True).clamp_min(1e-9)
        alpha = alpha*valid[:,None,:,None].to(alpha.dtype)
        peer = (alpha@v).transpose(1,2).reshape(b,m,d)
        mean_att=alpha.mean(1)
        top=mean_att.max(-1).values
        # Conditional entropy of attended peers, normalized by log(number of peers).
        h=-(mean_att*mean_att.clamp_min(1e-9).log()).sum(-1)
        n=(valid.sum(1)-1).clamp_min(2).to(h.dtype)
        entropy=h/torch.log(n)[:,None]
        entropy=entropy*(valid.sum(1)>2).to(entropy.dtype)[:,None]
        return self.peer_out(peer),mean_att.sum(-1),top,entropy

    def forward(self, x, valid, faculty, semester_z, mode="joint",
                intervention="none", context_override=None, calibration_bias=None):
        if mode not in TRAIN_MODES:
            raise ValueError("invalid training mode: "+str(mode))
        if intervention not in ("none","no_attention","no_context","placebo"):
            raise ValueError("invalid intervention: "+str(intervention))
        if intervention=="placebo" and context_override is None:
            raise ValueError("placebo requires matched donor course features")
        if not valid.any(1).all():
            raise ValueError("student-semester groups must be nonempty")
        if faculty.shape != valid.shape or x.shape[:2] != valid.shape:
            raise ValueError("incorrect x/mask/faculty shapes")
        b,m,d=x.shape
        if d != self.n_features:
            raise ValueError("feature dimension mismatch")
        h = self.row_encoder(torch.cat(
            [x, self.faculty_emb(faculty), semester_z.unsqueeze(-1)],-1))
        source=x[...,-self.course_dim:] if context_override is None else context_override
        if source.shape != (b,m,self.course_dim):
            raise ValueError("context_override must be [B,M,17]")
        c = self.course_encoder(source)
        row_logits = self.row_head(h).squeeze(-1)
        use_attn = (mode in ("portfolio_independent","joint")
                    and intervention not in ("no_attention","no_context"))
        peer_mass=x.new_zeros(b,m)
        peer_top=x.new_zeros(b,m)
        peer_entropy=x.new_zeros(b,m)
        peer_delta=x.new_zeros(b,m)
        gate=x.new_zeros(b,m)
        if use_attn:
            peer,peer_mass,peer_top,peer_entropy=self.peer_attention(h,c,valid)
            gate = torch.sigmoid(self.peer_gate(torch.cat([h,peer],-1))).squeeze(-1)
            effect = self.peer_effect(torch.cat([h,peer],-1)).squeeze(-1)
            gate = gate*(valid.sum(1)>1).to(gate.dtype)[:,None]
            peer_delta=gate*effect
            row_logits = row_logits + peer_delta
        if calibration_bias is not None:
            if calibration_bias.shape != (b,m):
                raise ValueError("calibration_bias must be [B,M]")
            row_logits = row_logits + calibration_bias
        use_joint = mode in ("joint","joint_no_attention") and intervention!="no_context"
        if use_joint:
            mean_h = self.pool_mean(h,valid)
            mean_c = self.pool_mean(c,valid)
            variance_c=self.pool_mean((c-mean_c[:,None,:]).square(),valid)
            n=valid.sum(1,keepdim=True).float()
            group_h=self.group_encoder(torch.cat(
                [mean_h,mean_c,variance_c,torch.log1p(n)],-1))
            raw=self.state_head(group_h)
            pi=torch.sigmoid(raw[:,0])
            low_shift=F.softplus(raw[:,1])
            high_shift=F.softplus(raw[:,2])
            p_low=torch.sigmoid(row_logits-low_shift[:,None])
            p_high=torch.sigmoid(row_logits+high_shift[:,None])
        else:
            pi=x.new_full((b,),0.5)
            low_shift=x.new_zeros(b)
            high_shift=x.new_zeros(b)
            p_low=p_high=torch.sigmoid(row_logits)
        p=(1-pi[:,None])*p_low+pi[:,None]*p_high
        pmf_low=poisson_binomial_pmf(p_low,valid)
        pmf_high=poisson_binomial_pmf(p_high,valid)
        pmf=(1-pi[:,None])*pmf_low+pi[:,None]*pmf_high
        prob_any=1-pmf[:,0]
        prob_multi=(1-pmf[:,0]-pmf[:,1]).clamp(min=0.,max=1.)
        expected=(p*valid.to(p.dtype)).sum(1)
        return dict(prob=p,logit=torch.logit(p.clamp(1e-7,1-1e-7)),
            p_low=p_low,p_high=p_high,mixture=pi,
            low_shift=low_shift,high_shift=high_shift,
            peer_gate=gate,peer_mass=peer_mass,peer_top=peer_top,
            peer_entropy=peer_entropy,peer_logit_delta=peer_delta,
            prob_any=prob_any,prob_multi=prob_multi,
            expected_count=expected,count_pmf=pmf,valid=valid)

def objective(out, y, valid, semester_ord=None, max_history_ord=None,
              epoch=1, warmup_epochs=2, ramp_epochs=3,
              joint_weight=.20, any_weight=.03, multi_weight=.03,
              count_weight=.03, ranking_weight=.05,
              recency_half_life=6., recency_floor=.35):
    """Registration-normalized BCE/joint NLL and group-level auxiliaries."""
    mask=valid.float()
    y=y.float()
    if (y[valid] != y[valid].round()).any() or not ((y[valid]>=0)&(y[valid]<=1)).all():
        raise ValueError("labels must be binary")
    if semester_ord is not None and recency_half_life > 0:
        if max_history_ord is None:raise ValueError("must supply historical ordinal upper bound")
        lag=(float(max_history_ord)-semester_ord[:,0]).clamp_min(0)
        group_weights=torch.exp(-math.log(2)*lag/recency_half_life).clamp_min(recency_floor)
    else:
        group_weights=y.new_ones(len(y))
    row_weights=group_weights[:,None]*mask
    denom=row_weights.sum().clamp_min(1)
    course_ce=F.binary_cross_entropy(out["prob"].clamp(1e-6,1-1e-6),y,reduction="none")
    marginal=(course_ce*row_weights).sum()/denom
    log_joint=joint_log_prob(y,valid,out["p_low"],out["p_high"],out["mixture"])
    joint_nll=-(log_joint*group_weights).sum()/denom
    count=(y*mask).sum(1)
    any_gt=(count>=1).float()
    multi_gt=(count>=2).float()
    gd=group_weights.sum().clamp_min(1)
    any_loss=(F.binary_cross_entropy(out["prob_any"].clamp(1e-6,1-1e-6),
                                     any_gt,reduction="none")*group_weights).sum()/gd
    multi_mask=(valid.sum(1)>=2).float()
    multi_loss=(F.binary_cross_entropy(out["prob_multi"].clamp(1e-6,1-1e-6),
                                      multi_gt,reduction="none")*
                group_weights*multi_mask).sum()/(group_weights*multi_mask).sum().clamp_min(1)
    size=mask.sum(1).clamp_min(1)
    count_loss=(F.smooth_l1_loss(out["expected_count"]/size,count/size,
                                  reduction="none")*group_weights).sum()/gd
    # Global within-batch pair ranking; avoids quadratic full-dataset operations.
    positive=out["logit"][valid & (y>0.5)]
    negative=out["logit"][valid & (y<0.5)]
    if positive.numel() and negative.numel():
        # Hard-negative subsampling for bounded memory on large batches.
        hard=negative.topk(min(256,negative.numel())).values
        ranking=F.softplus(hard[None,:]-positive[:,None]).mean()
    else:
        ranking=marginal*0.
    ramp=max(0.,min(1.,(epoch-warmup_epochs)/max(ramp_epochs,1)))
    total=marginal+ramp*(joint_weight*joint_nll+any_weight*any_loss+
                         multi_weight*multi_loss+count_weight*count_loss+
                         ranking_weight*ranking)
    return dict(total=total,marginal=marginal,joint_nll=joint_nll,any=any_loss,
                multi=multi_loss,count=count_loss,rank=ranking,ramp=ramp)
