# -*- coding: utf-8 -*-
from __future__ import annotations

import math
import random
from typing import Sequence

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):
    """Exact Suite5 residual expert."""

    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 FacultyAdapter(nn.Module):
    """Small residual bottleneck adapter.

    Only the output projection is zero initialized. This guarantees A(h)=0 at
    initialization while retaining a normally initialized feature extractor in
    the down projection. In isolation the output projection can receive gradient
    immediately. In Suite7, the residual experts also start with zero output
    layers, so the first minibatch activates that shared residual path; the
    adapter up projection then learns, followed by the down projection.
    """

    def __init__(self, dim: int, bottleneck_dim: int, dropout: float = 0.0):
        super().__init__()
        self.down = nn.Linear(int(dim), int(bottleneck_dim))
        self.activation = nn.GELU()
        self.dropout = nn.Dropout(float(dropout))
        self.up = nn.Linear(int(bottleneck_dim), int(dim))
        nn.init.zeros_(self.up.weight)
        nn.init.zeros_(self.up.bias)

    def forward(self, h: torch.Tensor) -> torch.Tensor:
        return self.up(self.dropout(self.activation(self.down(h))))


class SharedPrivateResidualMoE(nn.Module):
    """Suite5 Residual-MoE plus a faculty-private residual representation adapter.

    IMPORTANT: shared Suite5-equivalent modules are instantiated first and in the
    same order as Suite5. Adapters are instantiated afterwards. With the same
    torch seed, this preserves initialization of embedding/encoder/experts/router.
    """

    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=0.10,
        router_temperature=1.0,
        adapter_dim=16,
        adapter_dropout=0.0,
        adapter_scale=1.0,
    ):
        super().__init__()
        self.n_experts = int(n_experts)
        self.delta_max = float(delta_max)
        self.router_temperature = float(router_temperature)
        self.adapter_dim = int(adapter_dim)
        self.adapter_scale = float(adapter_scale)

        # --- Exact Suite5 module construction order starts here. ---
        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)
            ]
        )
        router_hidden = max(32, int(representation_dim) // 2)
        self.router = nn.Sequential(
            nn.Linear(int(representation_dim), router_hidden),
            nn.GELU(),
            nn.Dropout(dropout),
            nn.Linear(router_hidden, self.n_experts + 1),
        )
        # --- Exact Suite5 module construction order ends here. ---

        self.adapters = nn.ModuleList(
            [
                FacultyAdapter(
                    int(representation_dim), self.adapter_dim, float(adapter_dropout)
                )
                for _ in range(int(n_faculties))
            ]
        )

    def apply_faculty_adapters(self, h: torch.Tensor, faculty_idx: torch.Tensor):
        adapter_out = torch.zeros_like(h)
        # A batch usually contains many faculties; dispatch only the adapters that
        # are present. This keeps parameters strictly private per faculty.
        for fi in torch.unique(faculty_idx):
            idx = int(fi.item())
            mask = faculty_idx == fi
            adapter_out[mask] = self.adapters[idx](h[mask])
        adapted = h + self.adapter_scale * adapter_out
        return adapted, adapter_out

    def forward(self, x, faculty_idx, q):
        e = self.faculty_embedding(faculty_idx)
        h = self.encoder(torch.cat([x, e], dim=1))
        h_adapted, adapter_out = self.apply_faculty_adapters(h, faculty_idx)
        residuals = torch.stack([m(h_adapted) for m in self.experts], dim=1)
        logits = self.router(h_adapted) / 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),
            "shared_representation": h,
            "adapter_output": adapter_out,
            "adapted_representation": h_adapted,
            # Suite5-compatible alias used by some generic diagnostics.
            "representation": h_adapted,
            "correction": correction,
        }


def capability_targets(y, q, counterfactual_prob, temperature=0.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):
    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=0.5,
    specialization_lambda=0.01,
    magnitude_lambda=0.01,
    utility_temperature=0.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


def adapter_diagnostics(shared_representation, adapter_output, adapted_representation):
    h = np.asarray(shared_representation, float)
    a = np.asarray(adapter_output, float)
    hf = np.asarray(adapted_representation, float)
    if len(h) == 0:
        return {
            "mean_adapter_norm": np.nan,
            "mean_shared_norm": np.nan,
            "mean_adapted_norm": np.nan,
            "mean_relative_adapter_norm": np.nan,
            "mean_representation_cosine": np.nan,
            "mean_abs_adapter_activation": np.nan,
        }
    an = np.linalg.norm(a, axis=1)
    hn = np.linalg.norm(h, axis=1)
    hfn = np.linalg.norm(hf, axis=1)
    denom = np.maximum(hn * hfn, 1e-12)
    cos = np.sum(h * hf, axis=1) / denom
    return {
        "mean_adapter_norm": float(np.mean(an)),
        "mean_shared_norm": float(np.mean(hn)),
        "mean_adapted_norm": float(np.mean(hfn)),
        "mean_relative_adapter_norm": float(np.mean(an / np.maximum(hn, 1e-12))),
        "mean_representation_cosine": float(np.mean(np.clip(cos, -1.0, 1.0))),
        "mean_abs_adapter_activation": float(np.mean(np.abs(a))),
    }


def adapter_parameter_rows(model: SharedPrivateResidualMoE, faculty_names: Sequence[str]):
    rows = []
    for fi, name in enumerate(faculty_names):
        a = model.adapters[fi]
        with torch.no_grad():
            rows.append(
                {
                    "faculty": name,
                    "adapter_index": fi,
                    "down_weight_l2": float(torch.linalg.vector_norm(a.down.weight).cpu()),
                    "down_bias_l2": float(torch.linalg.vector_norm(a.down.bias).cpu()),
                    "up_weight_l2": float(torch.linalg.vector_norm(a.up.weight).cpu()),
                    "up_bias_l2": float(torch.linalg.vector_norm(a.up.bias).cpu()),
                }
            )
    return rows
