from __future__ import annotations

import copy
import random
from dataclasses import dataclass

import numpy as np
import pandas as pd
import torch
import torch.nn.functional as F
from sklearn.metrics import average_precision_score, roc_auc_score
from torch.utils.data import DataLoader, Dataset, WeightedRandomSampler

from suite21_data import Suite21Bundle, faculty_sampling_weights
from suite21_models import probabilities_at_alpha


def seed_everything(seed: int):
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    if torch.cuda.is_available():
        torch.cuda.manual_seed_all(seed)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False


def choose_device(value: str) -> torch.device:
    if value == "cpu":
        return torch.device("cpu")
    if value == "cuda":
        if not torch.cuda.is_available():
            raise RuntimeError("CUDA requested but unavailable")
        return torch.device("cuda")
    return torch.device("cuda" if torch.cuda.is_available() else "cpu")


class BundleDataset(Dataset):
    def __init__(self, bundle: Suite21Bundle, indices: np.ndarray):
        self.b = bundle
        self.idx = np.asarray(indices, int)

    def __len__(self):
        return len(self.idx)

    def __getitem__(self, j):
        i = int(self.idx[j])
        return {
            "flat": torch.from_numpy(self.b.flat[i]),
            "student_seq": torch.from_numpy(self.b.student_seq[i]),
            "student_mask": torch.from_numpy(self.b.student_mask[i]),
            "course_target": torch.from_numpy(self.b.course_target[i]),
            "faculty_idx": torch.tensor(int(self.b.faculty_idx[i]), dtype=torch.long),
            "semester_scalar": torch.tensor(float(self.b.target_semester[i]), dtype=torch.float32),
            "committee_probs": torch.from_numpy(self.b.committee_probs[i]),
            "y": torch.tensor(float(self.b.y[i]), dtype=torch.float32),
            "index": torch.tensor(i, dtype=torch.long),
        }


def make_loader(bundle, mask, batch_size, training, seed, samples_per_epoch=0):
    idx = np.where(np.asarray(mask, bool))[0]
    ds = BundleDataset(bundle, idx)
    generator = torch.Generator()
    generator.manual_seed(int(seed))
    if training:
        local_w, _ = faculty_sampling_weights(bundle.faculty_idx[idx], bundle.faculty_names)
        num_samples = int(samples_per_epoch) if int(samples_per_epoch) > 0 else len(idx)
        sampler = WeightedRandomSampler(
            torch.as_tensor(local_w, dtype=torch.double),
            num_samples=num_samples,
            replacement=True,
            generator=generator,
        )
        return DataLoader(
            ds, batch_size=batch_size, sampler=sampler, num_workers=0,
            pin_memory=torch.cuda.is_available()
        )
    return DataLoader(
        ds, batch_size=batch_size, shuffle=False, num_workers=0,
        pin_memory=torch.cuda.is_available()
    )


def move_batch(batch, device):
    return {k: (v.to(device, non_blocking=True) if torch.is_tensor(v) else v) for k, v in batch.items()}


def safe_ap(y, p):
    y = np.asarray(y, int)
    return float(average_precision_score(y, p)) if np.unique(y).size > 1 else np.nan


def safe_auc(y, p):
    y = np.asarray(y, int)
    return float(roc_auc_score(y, p)) if np.unique(y).size > 1 else np.nan


def warn_by_rate(score, rate=.20):
    score = np.asarray(score, float)
    k = int(round(float(rate) * len(score)))
    k = max(0, min(len(score), k))
    out = np.zeros(len(score), np.int8)
    if k:
        out[np.argsort(-score, kind="mergesort")[:k]] = 1
    return out


def fbeta_from_predictions(y, pred, beta=2.0):
    y = np.asarray(y, int)
    pred = np.asarray(pred, int)
    tp = int(((y == 1) & (pred == 1)).sum())
    fp = int(((y == 0) & (pred == 1)).sum())
    fn = int(((y == 1) & (pred == 0)).sum())
    p = tp / max(tp + fp, 1)
    r = tp / max(tp + fn, 1)
    b2 = beta * beta
    return (1 + b2) * p * r / max(b2 * p + r, 1e-12)


def faculty_validation_delta(bundle, mask, candidate, beta=2.0):
    idx = np.where(np.asarray(mask, bool))[0]
    y = bundle.y[idx]
    q = bundle.committee[idx]
    f = bundle.faculty_idx[idx]
    rows = []
    for fi, name in enumerate(bundle.faculty_names):
        m = f == fi
        if not m.any() or np.unique(y[m]).size < 2:
            continue
        cap = safe_ap(y[m], candidate[m])
        bap = safe_ap(y[m], q[m])
        cf2 = fbeta_from_predictions(y[m], warn_by_rate(candidate[m], .20), beta)
        bf2 = fbeta_from_predictions(y[m], warn_by_rate(q[m], .20), beta)
        rows.append({
            "faculty": name,
            "candidate_ap": cap,
            "committee_ap": bap,
            "delta_ap": cap - bap,
            "candidate_f2_at_20": cf2,
            "committee_f2_at_20": bf2,
            "delta_f2_at_20": cf2 - bf2,
        })
    return pd.DataFrame(rows)


def robust_validation_score(delta_df: pd.DataFrame):
    if delta_df is None or not len(delta_df):
        return -np.inf, {}
    da = delta_df.delta_ap.to_numpy(float)
    df2 = delta_df.delta_f2_at_20.to_numpy(float)
    mean = float(np.nanmean(da))
    median = float(np.nanmedian(da))
    q25 = float(np.nanquantile(da, .25))
    worst = float(np.nanmin(da))
    mean_f2 = float(np.nanmean(df2))
    wins = int(np.sum(da > 0))
    # Broad-support objective. Identity alpha=0 has score 0 exactly.
    score = .45 * mean + .30 * median + .15 * q25 + .10 * mean_f2
    if worst < -.05:
        score -= .05 * abs(worst + .05)
    stats = {
        "robust_score": float(score),
        "mean_delta_ap": mean,
        "median_delta_ap": median,
        "q25_delta_ap": q25,
        "worst_delta_ap": worst,
        "mean_delta_f2_at_20": mean_f2,
        "ap_wins": wins,
        "faculties": int(len(delta_df)),
    }
    return float(score), stats




def committee_hard_pair_ranking_loss(out, y, faculty_idx, max_per_class=24):
    """Pairwise ranking loss focused on Committee mistakes, within faculty.

    For each faculty in the batch, use the lowest-Committee-risk positives and
    highest-Committee-risk negatives. This targets precisely the ordering errors
    that a residual model must fix to improve AP.
    """
    final_logit = out["base_logit"] + out["correction_members"].mean(1)
    base = out["base_logit"].detach()
    losses = []
    for fi in torch.unique(faculty_idx):
        m = faculty_idx == fi
        pos = torch.where(m & (y > .5))[0]
        neg = torch.where(m & (y <= .5))[0]
        if len(pos) == 0 or len(neg) == 0:
            continue
        pos = pos[torch.argsort(base[pos])[: int(max_per_class)]]
        neg = neg[torch.argsort(base[neg], descending=True)[: int(max_per_class)]]
        diff = final_logit[pos][:, None] - final_logit[neg][None, :]
        losses.append(F.softplus(-diff).mean())
    if not losses:
        return torch.zeros((), device=y.device)
    return torch.stack(losses).mean()

def loss_components(
    out,
    y,
    faculty_idx,
    residual_penalty: float,
    positive_preserve_penalty: float,
    temporal_aux_weight: float,
    aux_pos_weight: float,
    ranking_weight: float,
):
    target = y[:, None].expand_as(out["correction_members"])
    member_logits = out["base_logit"][:, None] + out["correction_members"]
    bce = F.binary_cross_entropy_with_logits(member_logits, target)
    residual = out["correction_members"].pow(2).mean()
    positives = y > .5
    if positives.any():
        final_mean_logit = member_logits.mean(1)
        preserve = torch.relu(out["base_logit"][positives] - final_mean_logit[positives]).mean()
    else:
        preserve = torch.zeros((), device=y.device)
    if temporal_aux_weight > 0 and out["temporal_aux_logit"].requires_grad:
        pw = torch.tensor(float(aux_pos_weight), device=y.device)
        aux = F.binary_cross_entropy_with_logits(out["temporal_aux_logit"], y, pos_weight=pw)
    else:
        aux = torch.zeros((), device=y.device)
    ranking = committee_hard_pair_ranking_loss(out, y, faculty_idx) if ranking_weight > 0 else torch.zeros((), device=y.device)
    total = (bce + float(residual_penalty) * residual + float(positive_preserve_penalty) * preserve
             + float(temporal_aux_weight) * aux + float(ranking_weight) * ranking)
    return total, {"bce": bce, "residual": residual, "positive_preserve": preserve, "aux": aux, "ranking": ranking}


def predict_raw(model, bundle, mask, batch_size, device, perturbation="none"):
    model.eval()
    outputs = {k: [] for k in [
        "base_probability", "base_logit", "correction_members", "authority", "guard_scale",
        "attention", "has_history", "temporal_aux_logit", "index"
    ]}
    with torch.no_grad():
        for batch in make_loader(bundle, mask, batch_size, False, 0):
            b = move_batch(batch, device)
            student_seq = b["student_seq"]
            student_mask = b["student_mask"]
            if perturbation == "reverse_history":
                student_seq = torch.flip(student_seq, dims=[1])
                student_mask = torch.flip(student_mask, dims=[1])
            elif perturbation == "zero_history":
                student_seq = torch.zeros_like(student_seq)
                student_mask = torch.zeros_like(student_mask)
            out = model(
                b["flat"], student_seq, student_mask, b["course_target"], b["faculty_idx"],
                b["semester_scalar"], b["committee_probs"]
            )
            for key in outputs:
                if key == "index":
                    outputs[key].append(b["index"].detach().cpu().numpy())
                else:
                    outputs[key].append(out[key].detach().cpu().numpy())
    for key in outputs:
        outputs[key] = np.concatenate(outputs[key], axis=0) if outputs[key] else np.array([])
    order = np.argsort(outputs["index"])
    for key in outputs:
        outputs[key] = outputs[key][order]
    return outputs


def probabilities_from_raw(raw, alpha: float):
    base = torch.as_tensor(raw["base_logit"], dtype=torch.float32)
    correction = torch.as_tensor(raw["correction_members"], dtype=torch.float32)
    prob, uncertainty, logits = probabilities_at_alpha(base, correction, alpha)
    return prob.numpy(), uncertainty.numpy(), logits.numpy()


def auxiliary_positive_weight(bundle, mask, cap=4.0):
    y = np.asarray(bundle.y[np.asarray(mask, bool)], int)
    n1 = max(int((y == 1).sum()), 1)
    n0 = max(int((y == 0).sum()), 1)
    return float(min(np.sqrt(n0 / n1), float(cap)))


def train_select_epochs(
    model,
    bundle,
    train_mask,
    val_mask,
    device,
    epochs,
    min_epochs,
    lr,
    weight_decay,
    batch_size,
    predict_batch_size,
    samples_per_epoch,
    patience,
    seed,
    alpha_grid,
    beta,
    residual_penalty,
    positive_preserve_penalty,
    temporal_aux_weight,
    ranking_weight,
    min_selection_score,
):
    optimizer = torch.optim.AdamW(model.parameters(), lr=lr, weight_decay=weight_decay)
    aux_pw = auxiliary_positive_weight(bundle, train_mask)
    best_score = -np.inf
    best_epoch = 1
    best_alpha = 0.0
    best_state = copy.deepcopy(model.state_dict())
    bad = 0
    rows = []

    for epoch in range(1, int(epochs) + 1):
        model.train()
        sums = {"loss": 0.0, "bce": 0.0, "residual": 0.0, "positive_preserve": 0.0, "aux": 0.0, "ranking": 0.0}
        seen = 0
        for batch in make_loader(bundle, train_mask, batch_size, True, seed + epoch, samples_per_epoch):
            b = move_batch(batch, device)
            optimizer.zero_grad(set_to_none=True)
            out = model(
                b["flat"], b["student_seq"], b["student_mask"], b["course_target"],
                b["faculty_idx"], b["semester_scalar"], b["committee_probs"]
            )
            loss, parts = loss_components(
                out, b["y"], b["faculty_idx"], residual_penalty, positive_preserve_penalty,
                temporal_aux_weight if model.temporal is not None else 0.0, aux_pw, ranking_weight
            )
            loss.backward()
            torch.nn.utils.clip_grad_norm_(model.parameters(), 5.0)
            optimizer.step()
            n = len(b["y"])
            sums["loss"] += float(loss.detach().cpu()) * n
            for k in parts:
                sums[k] += float(parts[k].detach().cpu()) * n
            seen += n

        raw = predict_raw(model, bundle, val_mask, predict_batch_size, device)
        epoch_best = None
        for alpha in alpha_grid:
            pred, _, _ = probabilities_from_raw(raw, alpha)
            delta_df = faculty_validation_delta(bundle, val_mask, pred, beta)
            score, stats = robust_validation_score(delta_df)
            row = {
                "epoch": epoch,
                "alpha": float(alpha),
                "robust_score": score,
                **stats,
                **{f"train_{k}": sums[k] / max(seen, 1) for k in sums},
            }
            rows.append(row)
            if epoch_best is None or score > epoch_best[0]:
                epoch_best = (score, float(alpha), stats)
            if score > best_score + 1e-5:
                best_score = score
                best_epoch = epoch
                best_alpha = float(alpha)
                best_state = copy.deepcopy(model.state_dict())
                bad = 0
        if epoch_best is not None:
            s, a, st = epoch_best
            print(
                f"[SELECT] epoch={epoch:02d}/{epochs} loss={sums['loss']/max(seen,1):.5f} "
                f"best_alpha={a:.2f} robust={s:+.5f} meanΔAP={st.get('mean_delta_ap', np.nan):+.4f} "
                f"medΔAP={st.get('median_delta_ap', np.nan):+.4f} q25={st.get('q25_delta_ap', np.nan):+.4f} "
                f"wins={st.get('ap_wins', 0)}/{st.get('faculties', 0)}"
            )
        if best_epoch != epoch:
            bad += 1
        if epoch >= int(min_epochs) and bad >= int(patience):
            break

    model.load_state_dict(best_state)
    if best_score < float(min_selection_score):
        best_alpha = 0.0
    return int(best_epoch), float(best_alpha), float(best_score), pd.DataFrame(rows)


def train_fixed_epochs(
    model,
    bundle,
    train_mask,
    device,
    epochs,
    lr,
    weight_decay,
    batch_size,
    samples_per_epoch,
    seed,
    residual_penalty,
    positive_preserve_penalty,
    temporal_aux_weight,
    ranking_weight,
):
    optimizer = torch.optim.AdamW(model.parameters(), lr=lr, weight_decay=weight_decay)
    aux_pw = auxiliary_positive_weight(bundle, train_mask)
    for epoch in range(1, int(epochs) + 1):
        model.train()
        total, seen = 0.0, 0
        for batch in make_loader(bundle, train_mask, batch_size, True, seed + 1000 + epoch, samples_per_epoch):
            b = move_batch(batch, device)
            optimizer.zero_grad(set_to_none=True)
            out = model(
                b["flat"], b["student_seq"], b["student_mask"], b["course_target"],
                b["faculty_idx"], b["semester_scalar"], b["committee_probs"]
            )
            loss, _ = loss_components(
                out, b["y"], b["faculty_idx"], residual_penalty, positive_preserve_penalty,
                temporal_aux_weight if model.temporal is not None else 0.0, aux_pw, ranking_weight
            )
            loss.backward()
            torch.nn.utils.clip_grad_norm_(model.parameters(), 5.0)
            optimizer.step()
            total += float(loss.detach().cpu()) * len(b["y"])
            seen += len(b["y"])
        if epoch == 1 or epoch == int(epochs):
            print(f"[REFIT] epoch={epoch:02d}/{epochs} loss={total/max(seen,1):.5f}")
