# -*- coding: utf-8 -*-
"""
dl_bench.py — harness MONOLITHIC (1 file): mo hinh DA-NHIEM (multi-task) hoc du doan
target_grade10, roi in ACCURACY / PRECISION / RECALL cho canh bao y<event.

MO HINH = 1 model duy nhat co 2 dau (thay vi 2 model ML rieng):
  - dau HOI QUY (regressor) -> du doan diem grade10
  - dau PHAN LOAI (classifier) -> P(y<event)
  - BLEND san trong model:  blended = p*anchor_low + (1-p)*reg
    (anchor_low = scalar HOC DUOC, khoi tao = mean diem <event cua train — giong
     risk-head cua pipeline nhung nay hoc chung end-to-end trong 1 model).
  Dau ra cuoi = diem blended -> warn = blended < event.

Setup: numpy + pandas + torch. TU DONG dung CUDA neu co, nguoc lai CPU.
  GPU (CUDA):  pip install torch --index-url https://download.pytorch.org/whl/cu121
  CPU      :  pip install torch --index-url https://download.pytorch.org/whl/cpu

CACH DUNG:
  1) Copy file nay + thu muc splits:  data/splits/<strategy>/fold_<n>/<faculty>_{train,test}.csv
  2) (Tuy y) sua sieu-tham-so o dau file / than build_and_predict().
  3) Chay:
       python dl_bench.py --faculty khoa_cong_nghe_thong_tin --folds 6,7
       python dl_bench.py --faculty khoa_quan_tri_kinh_doanh_va_du_lich --folds all
       python dl_bench.py --list                # liet ke faculty co san

DANH GIA (frontier-aware):
  - DEV (fold 1-5): voi MOI tin hieu [blended grade | P(y<event)] tinh AP va tune
    nguong toi uu F1 RIENG cho tung tin hieu (in ca 2 de tham khao).
  - HOLDOUT (fold 6-7): bao cao RIENG cho CA HAI tin hieu (blended + classification
    head), moi tin hieu dung tau/ratio tune tren dev cua chinh no:
      AP/AUC (chat luong frontier) + ACC/PRECISION/RECALL/F1 tai
        @t  = nguong co dinh tu dev
        @r  = DRIFT-AWARE (flag top-K, K=ratio*prevalence ky lien truoc; leak-safe) —
              khoi phuc recall khi base-rate y<event tang dan theo ky.
  Metric per fold + POOLED (micro-average: gop tat ca fold roi tinh 1 lan).

STABLE TEMPORAL WEIGHTING (NEXT EXPERIMENT):
  - Half-life H=4 over MEANINGFUL observed-semester steps, not raw ordinal gaps.
  - Tiny/special semesters do not create a full decay step.
  - Temporal multipliers are normalized separately inside failure and normal
    classes, shrunk toward 1, and bounded to avoid extreme combined weights.
  - Recency weighting is applied ONLY to the classification loss. Regression
    and blended MSE retain the original baseline weighting so the stable grade
    pathway and original model architecture remain unchanged.
  - Equal-warning-budget metrics are printed for fair model comparison.
"""
import argparse
import sys
from pathlib import Path

import random
import numpy as np
import pandas as pd
import torch
import torch.nn as nn

try:
    from tqdm.auto import tqdm
except ImportError:                      # portable: chay duoc ca khi thieu tqdm
    class tqdm:                          # no-op fallback (cung API toi thieu)
        def __init__(self, *a, **k): pass
        def update(self, n=1): pass
        def set_postfix(self, *a, **k): pass
        def __enter__(self): return self
        def __exit__(self, *a): return False

# Cot KHONG phai feature (dong bo src/ml_platform/modeling/__main__.py).
EXCLUDE_COLS = {
    "dataset_split", "student_faculty_key", "student_faculty_name", "IDSV",
    "target_NHHK", "target_semester_ordinal", "IDKQDK", "IDToHoc", "IDMH",
    "target_IDCongDiem", "target_grade10", "target_grade4", "target_status",
    "base_row_id", "course_cluster_label",
}
TARGET = "target_grade10"

# ---- Sieu tham so (sua o day) ----
TRUNK = (256, 128)   # so noron than chung (shared)
HEAD = 64            # so noron moi dau (reg / cls)
DROPOUT = 0.3
EPOCHS = 100
BATCH = 512
LR = 1e-3
WEIGHT_DECAY = 1e-5
LOW_WEIGHT = 5.0     # CUSTOM LOSS: trong so mau diem thap (y<event) trong weighted-MSE
CB_BETA = 0.9999

# Stable temporal weighting. Age is counted in meaningful observed-semester
# transitions, not raw target_semester_ordinal gaps.
TEMPORAL_HALF_LIFE = 4.0
MIN_MEANINGFUL_SEMESTER_ROWS = 100
MIN_MEANINGFUL_SEMESTER_FRACTION = 0.005
TEMPORAL_STRENGTH = 0.50       # shrink recency multipliers halfway toward 1
TEMPORAL_MULTIPLIER_MIN = 0.50
TEMPORAL_MULTIPLIER_MAX = 1.50
EQUAL_BUDGET_RATES = (0.05, 0.10, 0.15, 0.20)

W_REG = 1.0          # trong so loss dau hoi quy
W_CLS = 1.0          # trong so loss dau phan loai (BCE)
W_BLEND = 1.0        # trong so loss dau ra BLENDED (toi uu truc tiep dau ra cuoi)
SEED = 42


def get_device():
    return torch.device("cuda" if torch.cuda.is_available() else "cpu")


def compute_meaningful_semester_ages(
    semester_ordinals,
    min_rows=MIN_MEANINGFUL_SEMESTER_ROWS,
    min_fraction=MIN_MEANINGFUL_SEMESTER_FRACTION,
):
    """Return age in meaningful observed-semester steps.

    A semester is meaningful when its row count is at least:
        max(min_rows, ceil(min_fraction * number_of_training_rows)).

    Tiny/special semesters remain in training, but they do not consume a full
    decay step. A sample's age is the number of meaningful semesters strictly
    newer than its own semester.
    """
    ordinals = np.asarray(semester_ordinals, dtype=np.float64)
    if ordinals.ndim != 1 or len(ordinals) == 0:
        raise ValueError("semester_ordinals must be a non-empty 1D array")
    if not np.isfinite(ordinals).all():
        raise ValueError("semester_ordinals contains NaN or infinity")
    if min_rows < 1:
        raise ValueError("min_rows must be >= 1")
    if not (0.0 <= min_fraction <= 1.0):
        raise ValueError("min_fraction must be in [0, 1]")

    unique, counts = np.unique(ordinals, return_counts=True)
    threshold = max(int(min_rows), int(np.ceil(float(min_fraction) * len(ordinals))))
    meaningful = unique[counts >= threshold]

    # Defensive fallback for very small datasets.
    if len(meaningful) == 0:
        meaningful = unique
        threshold = 1

    ages = np.array(
        [np.count_nonzero(meaningful > ordinal) for ordinal in ordinals],
        dtype=np.float32,
    )
    return ages, meaningful.astype(np.float64), threshold


def compute_raw_temporal_decay(semester_ages, half_life=TEMPORAL_HALF_LIFE):
    """Exponential decay over meaningful semester steps."""
    ages = np.asarray(semester_ages, dtype=np.float64)
    if ages.ndim != 1 or len(ages) == 0:
        raise ValueError("semester_ages must be a non-empty 1D array")
    if not np.isfinite(ages).all() or (ages < 0).any():
        raise ValueError("semester_ages must be finite and non-negative")
    if half_life <= 0:
        raise ValueError("half_life must be > 0")
    return np.power(0.5, ages / float(half_life)).astype(np.float32)


def _bounded_mean_one(values, lower, upper):
    """Scale then clip positive values so their final mean is exactly one."""
    values = np.asarray(values, dtype=np.float64)
    if len(values) == 0:
        return values.astype(np.float32)
    if not np.isfinite(values).all() or (values <= 0).any():
        raise ValueError("values must be finite and strictly positive")
    if not (0 < lower <= 1.0 <= upper):
        raise ValueError("bounds must satisfy 0 < lower <= 1 <= upper")

    lo, hi = 0.0, max(1.0, upper / max(values.min(), 1e-12)) * 4.0
    for _ in range(100):
        mid = 0.5 * (lo + hi)
        mean = np.clip(mid * values, lower, upper).mean()
        if mean < 1.0:
            lo = mid
        else:
            hi = mid
    return np.clip(hi * values, lower, upper).astype(np.float32)


def stabilize_temporal_multipliers(
    raw_temporal_decay,
    y_binary,
    strength=TEMPORAL_STRENGTH,
    lower=TEMPORAL_MULTIPLIER_MIN,
    upper=TEMPORAL_MULTIPLIER_MAX,
):
    """Create bounded, class-preserving recency multipliers.

    Each class has mean temporal multiplier exactly one. Consequently,
    multiplying these values by effective-number class weights reallocates
    weight from old to recent examples WITHIN each class without changing the
    total failure-versus-normal loss mass established by class balancing.
    """
    raw = np.asarray(raw_temporal_decay, dtype=np.float32)
    y = np.asarray(y_binary, dtype=np.float32)
    if raw.shape != y.shape or raw.ndim != 1 or len(raw) == 0:
        raise ValueError("raw_temporal_decay and y_binary must be equal non-empty 1D arrays")
    if not np.isfinite(raw).all() or (raw <= 0).any():
        raise ValueError("raw_temporal_decay must be finite and strictly positive")
    if not (0.0 <= strength <= 1.0):
        raise ValueError("strength must be in [0, 1]")

    multipliers = np.ones_like(raw, dtype=np.float32)
    for cls in (0.0, 1.0):
        mask = (y > 0.5) if cls == 1.0 else (y <= 0.5)
        if not mask.any():
            continue
        relative = raw[mask] / max(float(raw[mask].mean()), 1e-12)
        # Shrink toward the baseline multiplier 1.0 before hard bounding.
        relative = 1.0 + float(strength) * (relative - 1.0)
        multipliers[mask] = _bounded_mean_one(relative, lower, upper)

    return multipliers.astype(np.float32)


def build_combined_sample_weights(
    y_binary, temporal_multipliers, cb_low, cb_high, low_weight=LOW_WEIGHT
):
    """Build stable loss weights.

    Regression and blend preserve the exact baseline low-grade weighting.
    Classification combines class-preserving temporal multipliers with the
    existing effective-number class weights.
    """
    y = np.asarray(y_binary, dtype=np.float32)
    temporal = np.asarray(temporal_multipliers, dtype=np.float32)
    if y.shape != temporal.shape or y.ndim != 1 or len(y) == 0:
        raise ValueError("y_binary and temporal_multipliers must be equal non-empty 1D arrays")
    if not np.isfinite(temporal).all() or (temporal <= 0).any():
        raise ValueError("temporal_multipliers must be finite and strictly positive")

    # Exact original weighting for regression and blended grade losses.
    reg_blend_weights = np.where(y > 0.5, low_weight, 1.0).astype(np.float32)
    reg_blend_weights /= max(float(reg_blend_weights.mean()), 1e-12)

    class_weights = np.where(y > 0.5, cb_low, cb_high).astype(np.float32)
    classification_weights = class_weights * temporal
    classification_weights /= max(float(classification_weights.mean()), 1e-12)

    return reg_blend_weights.astype(np.float32), classification_weights.astype(np.float32)


def effective_sample_size(weights):
    weights = np.asarray(weights, dtype=np.float64)
    if len(weights) == 0 or not np.isfinite(weights).all() or (weights < 0).any():
        return float("nan")
    denominator = float(np.square(weights).sum())
    return float(weights.sum() ** 2 / denominator) if denominator > 0 else float("nan")


def print_temporal_weight_summary(
    semester_ordinals,
    semester_codes,
    semester_ages,
    y_binary,
    raw_decay,
    temporal_multipliers,
    meaningful_ordinals,
    meaningful_threshold,
    desc="",
):
    """Print raw decay and final bounded multiplier by historical semester."""
    ordinals = np.asarray(semester_ordinals, dtype=float)
    codes = np.asarray(semester_codes)
    ages = np.asarray(semester_ages, dtype=float)
    y = np.asarray(y_binary, dtype=np.float32)
    raw = np.asarray(raw_decay, dtype=np.float32)
    temporal = np.asarray(temporal_multipliers, dtype=np.float32)
    total_mass = float(temporal.sum())
    meaningful_set = set(float(v) for v in meaningful_ordinals)

    print(
        f"[Temporal policy] {desc} H={TEMPORAL_HALF_LIFE:g} meaningful_threshold="
        f"{meaningful_threshold} strength={TEMPORAL_STRENGTH:g} "
        f"bounds=[{TEMPORAL_MULTIPLIER_MIN:g},{TEMPORAL_MULTIPLIER_MAX:g}] | "
        f"raw_range=({raw.min():.4f},{raw.max():.4f}) "
        f"final_range=({temporal.min():.4f},{temporal.max():.4f}) "
        f"temporal_ESS={effective_sample_size(temporal):.0f}/{len(temporal)}"
    )
    print(
        f"  {'semester':>10} {'ordinal':>8} {'used':>6} {'age':>5} {'rows':>8} "
        f"{'fail%':>8} {'raw':>8} {'final':>8} {'mass%':>8}"
    )
    for ordinal in np.sort(np.unique(ordinals)):
        mask = ordinals == ordinal
        code_values = codes[mask]
        code = str(code_values[0]) if len(code_values) else "?"
        rows = int(mask.sum())
        fail_pct = 100.0 * float(y[mask].mean()) if rows else float("nan")
        age = float(ages[mask][0]) if rows else float("nan")
        raw_mean = float(raw[mask].mean()) if rows else float("nan")
        final_mean = float(temporal[mask].mean()) if rows else float("nan")
        mass_pct = 100.0 * float(temporal[mask].sum()) / max(total_mass, 1e-12)
        used = "yes" if float(ordinal) in meaningful_set else "no"
        print(
            f"  {code:>10} {ordinal:8.0f} {used:>6} {age:5.0f} {rows:8d} "
            f"{fail_pct:7.2f}% {raw_mean:8.4f} {final_mean:8.4f} {mass_pct:7.2f}%"
        )


# =========================================================================== #
#  MODEL DA-NHIEM: than chung + dau hoi quy + dau phan loai + blend hoc duoc.  #
# =========================================================================== #
class MultiTaskNet(nn.Module):
    def __init__(self, d_in, trunk=TRUNK, head=HEAD, dropout=DROPOUT, anchor_low_norm=0.0):
        super().__init__()
        layers, d = [], d_in
        for h in trunk:
            layers += [nn.Linear(d, h), nn.BatchNorm1d(h), nn.ReLU(), nn.Dropout(dropout)]
            d = h
        self.trunk = nn.Sequential(*layers)
        self.reg_head = nn.Sequential(nn.Linear(d, head), nn.ReLU(), nn.Linear(head, 1))
        self.cls_head = nn.Sequential(nn.Linear(d, head), nn.ReLU(), nn.Linear(head, 1))
        # anchor_low: muc "keo" khi P(y<event) cao — HOC DUOC (space da chuan hoa).
        self.anchor_low = nn.Parameter(torch.tensor(float(anchor_low_norm)))

    def forward(self, x):
        z = self.trunk(x)
        reg = self.reg_head(z)                 # diem (da chuan hoa)
        logit = self.cls_head(z)               # logit P(y<event)
        p = torch.sigmoid(logit)
        blended = p * self.anchor_low + (1.0 - p) * reg
        return reg, logit, p, blended


def build_and_predict(
    X_train, y_train, X_test, train_semester_ordinals, train_semester_codes,
    event=5.0, desc=""
):
    """Huan luyen MultiTaskNet; tra ve du doan diem BLENDED (thang 10) cho X_test.

    Loss = W_REG*baseline weighted-MSE(reg)
         + W_CLS*stable temporal-and-class-weighted BCE(cls)
         + W_BLEND*baseline weighted-MSE(blended).
    Architecture, preprocessing, regression weighting and blend weighting stay
    identical to the original effective-number baseline.
    """
    torch.manual_seed(SEED)
    device = get_device()
    Xtr = X_train.to_numpy(dtype=np.float32)
    Xte = X_test.to_numpy(dtype=np.float32)

    # Chuan hoa feature (thong ke tu TRAIN).
    mu, sd = Xtr.mean(0), Xtr.std(0)
    sd[sd == 0] = 1.0
    Xtr = (Xtr - mu) / sd
    Xte = (Xte - mu) / sd

    # Chuan hoa target (inverse lai khi du doan).
    ym = float(y_train.mean())
    ys = float(y_train.std()) or 1.0
    yz = ((y_train - ym) / ys).astype(np.float32)

    # Nhan phan loai + trong so.
    yb = (y_train < event).astype(np.float32)

    n_low = float(yb.sum())          # y < 5
    n_high = float(len(yb) - n_low)  # y >= 5

    if n_low == 0 or n_high == 0:
        cb_low = 1.0
        cb_high = 1.0
    else:
        raw_low = (1.0 - CB_BETA) / (1.0 - CB_BETA ** n_low)
        raw_high = (1.0 - CB_BETA) / (1.0 - CB_BETA ** n_high)

        # Normalize so the average sample weight remains 1.
        normalizer = (
            n_low * raw_low + n_high * raw_high
        ) / (n_low + n_high)

        cb_low = raw_low / normalizer
        cb_high = raw_high / normalizer

    raw_inverse_ratio = n_high / max(n_low, 1.0)
    effective_ratio = cb_low / max(cb_high, 1e-12)

    print(
        f"[Class weights] "
        f"n_low={int(n_low)}, n_high={int(n_high)} | "
        f"raw_inverse_ratio={raw_inverse_ratio:.3f} | "
        f"CB_low={cb_low:.4f}, CB_high={cb_high:.4f} | "
        f"CB_ratio={effective_ratio:.3f}"
    )

    # Count age in meaningful observed-semester transitions so tiny or
    # special semesters do not cause an artificial full decay step.
    semester_ages, meaningful_ordinals, meaningful_threshold = (
        compute_meaningful_semester_ages(train_semester_ordinals)
    )
    raw_temporal_decay = compute_raw_temporal_decay(
        semester_ages, half_life=TEMPORAL_HALF_LIFE
    )

    # Preserve the original effective-number positive/negative total mass.
    # Recency only redistributes influence from old to recent examples WITHIN
    # each class. Shrinkage and bounds prevent extreme combined weights.
    temporal_multiplier = stabilize_temporal_multipliers(
        raw_temporal_decay, yb
    )
    print_temporal_weight_summary(
        train_semester_ordinals,
        train_semester_codes,
        semester_ages,
        yb,
        raw_temporal_decay,
        temporal_multiplier,
        meaningful_ordinals,
        meaningful_threshold,
        desc,
    )

    # Temporal weighting is applied only to classification. Regression and
    # blended MSE keep the exact baseline low-grade sample weights.
    reg_blend_w, cls_w = build_combined_sample_weights(
        y_binary=yb,
        temporal_multipliers=temporal_multiplier,
        cb_low=cb_low,
        cb_high=cb_high,
        low_weight=LOW_WEIGHT,
    )

    baseline_cls_w = np.where(yb > 0.5, cb_low, cb_high).astype(np.float32)
    baseline_cls_w /= max(float(baseline_cls_w.mean()), 1e-12)
    baseline_pos_mass = float(baseline_cls_w[yb > 0.5].sum())
    baseline_neg_mass = float(baseline_cls_w[yb <= 0.5].sum())
    cls_pos_mass = float(cls_w[yb > 0.5].sum())
    cls_neg_mass = float(cls_w[yb <= 0.5].sum())

    print(
        f"[Combined weights] reg/blend=BASELINE mean={reg_blend_w.mean():.4f} "
        f"range=({reg_blend_w.min():.4f},{reg_blend_w.max():.4f}) | "
        f"cls mean={cls_w.mean():.4f} range=({cls_w.min():.4f},{cls_w.max():.4f}) "
        f"ESS={effective_sample_size(cls_w):.0f}/{len(cls_w)} | "
        f"pos/neg mass baseline="
        f"{baseline_pos_mass / max(baseline_neg_mass, 1e-12):.4f} "
        f"new={cls_pos_mass / max(cls_neg_mass, 1e-12):.4f}"
    )

    # anchor_low khoi tao = mean diem <event cua train, quy ve space chuan hoa.
    low_vals = y_train[y_train < event]
    anchor_raw = float(low_vals.mean()) if low_vals.size else (event / 2.0)
    anchor_low_norm = (anchor_raw - ym) / ys

    # Tensor -> device.
    Xtr_t = torch.from_numpy(Xtr).to(device)
    yz_t = torch.from_numpy(yz).view(-1, 1).to(device)
    yb_t = torch.from_numpy(yb).view(-1, 1).to(device)
    reg_blend_w_t = torch.from_numpy(reg_blend_w).view(-1, 1).to(device)
    cls_w_t = torch.from_numpy(cls_w).view(-1, 1).to(device)

    model = MultiTaskNet(Xtr.shape[1], anchor_low_norm=anchor_low_norm).to(device)
    opt = torch.optim.AdamW(model.parameters(), lr=LR, weight_decay=WEIGHT_DECAY)
    bce = nn.BCEWithLogitsLoss(reduction="none")

    n = len(Xtr_t)
    n_batches = (n + BATCH - 1) // BATCH
    with tqdm(total=EPOCHS * n_batches, desc=f"train {desc}".strip(), leave=False) as bar:
        for ep in range(EPOCHS):
            perm = torch.randperm(n, device=device)
            model.train()
            run_loss, steps = 0.0, 0
            for s in range(0, n, BATCH):
                j = perm[s:s + BATCH]
                bar.update(1)
                if len(j) < 2:                 # BatchNorm can >=2 mau
                    continue
                xb = Xtr_t[j]
                yzb = yz_t[j]
                ybb = yb_t[j]
                reg_wb = reg_blend_w_t[j]
                cls_wb = cls_w_t[j]

                opt.zero_grad()
                reg, logit, _p, blended = model(xb)
                l_reg = (reg_wb * (reg - yzb) ** 2).mean()
                cls_loss_each = bce(logit, ybb)
                l_cls = (cls_wb * cls_loss_each).mean()
                l_blend = (reg_wb * (blended - yzb) ** 2).mean()
                loss = W_REG * l_reg + W_CLS * l_cls + W_BLEND * l_blend
                loss.backward()
                opt.step()
                run_loss += loss.item(); steps += 1
            bar.set_postfix(epoch=ep + 1, loss=f"{run_loss / max(steps, 1):.4f}")

    # Du doan theo lo (an toan bo nho GPU). Tra CA HAI tin hieu:
    #   pred_grade = diem blended (thang 10);  p_event = P(y<event) tu dau phan loai.
    Xte_t = torch.from_numpy(Xte)
    model.eval()
    b_out, p_out = [], []
    with torch.no_grad():
        for s in range(0, len(Xte_t), 8192):
            xb = Xte_t[s:s + 8192].to(device)
            _, _, p, blended = model(xb)
            b_out.append(blended.view(-1).cpu())
            p_out.append(p.view(-1).cpu())
    pred_grade = torch.cat(b_out).numpy() * ys + ym   # inverse chuan hoa
    p_event = torch.cat(p_out).numpy()
    return pred_grade, p_event
# =========================================================================== #


def load_fold(faculty, fold, splits_dir):
    """Nap 1 fold — tai hien fold_data.py: bo null target, ep so, impute mean TRAIN."""
    tr_path = splits_dir / f"fold_{fold}" / f"{faculty}_train.csv"
    te_path = splits_dir / f"fold_{fold}" / f"{faculty}_test.csv"
    if not tr_path.exists() or not te_path.exists():
        return None

    tr = pd.read_csv(tr_path, low_memory=False)
    te = pd.read_csv(te_path, low_memory=False)
    for df in (tr, te):
        df[TARGET] = pd.to_numeric(df[TARGET], errors="coerce")
        if "target_grade4" in df.columns:
            df["target_grade4"] = pd.to_numeric(df["target_grade4"], errors="coerce")
    need = [TARGET] + (["target_grade4"] if "target_grade4" in tr.columns else [])
    tr = tr.dropna(subset=need)
    te = te.dropna(subset=need)
    if len(tr) < 50:
        return None

    if "target_semester_ordinal" not in tr.columns:
        raise ValueError(
            "Temporal weighting requires target_semester_ordinal in every training fold."
        )
    train_semester_ordinals = pd.to_numeric(
        tr["target_semester_ordinal"], errors="coerce"
    ).to_numpy(dtype=float)
    if not np.isfinite(train_semester_ordinals).all():
        raise ValueError(
            "target_semester_ordinal contains missing/non-numeric values after target filtering."
        )
    if "target_NHHK" in tr.columns:
        train_semester_codes = tr["target_NHHK"].astype(str).to_numpy()
    else:
        train_semester_codes = train_semester_ordinals.astype(int).astype(str)

    feats = [c for c in tr.columns if c not in EXCLUDE_COLS]
    Xtr = tr[feats].apply(pd.to_numeric, errors="coerce")
    Xte = te[feats].apply(pd.to_numeric, errors="coerce")
    means = Xtr.mean()
    Xtr = Xtr.fillna(means).fillna(0.0)     # cot toan-null train -> 0 (DL khong chiu NaN)
    Xte = Xte.fillna(means).fillna(0.0)

    ytr = tr[TARGET].to_numpy(dtype=float)
    yte = te[TARGET].to_numpy(dtype=float)
    return (
        Xtr, Xte, ytr, yte,
        train_semester_ordinals, train_semester_codes, len(feats)
    )


# ---------- Metric numpy thuan (khong sklearn) ----------
def _rankdata(a):
    a = np.asarray(a, float); order = np.argsort(a, kind="mergesort"); sa = a[order]
    r = np.arange(1, len(a) + 1, dtype=float); i = 0
    while i < len(sa):
        j = i
        while j + 1 < len(sa) and sa[j + 1] == sa[i]:
            j += 1
        if j > i:
            r[i:j + 1] = (i + 1 + j + 1) / 2.0
        i = j + 1
    out = np.empty(len(a), float); out[order] = r
    return out


def roc_auc(y_bin, score):
    y = np.asarray(y_bin).astype(bool); npos, nneg = int(y.sum()), int((~y).sum())
    if npos == 0 or nneg == 0:
        return float("nan")
    ranks = _rankdata(score)
    return (ranks[y].sum() - npos * (npos + 1) / 2.0) / (npos * nneg)


def average_precision(y_bin, score):
    y = np.asarray(y_bin).astype(int); npos = int(y.sum())
    if npos == 0:
        return float("nan")
    order = np.argsort(-np.asarray(score, float), kind="mergesort"); y = y[order]
    tp = np.cumsum(y); fp = np.cumsum(1 - y)
    prec = tp / np.maximum(tp + fp, 1); rec = tp / npos
    return float(np.sum((rec - np.concatenate([[0.0], rec[:-1]])) * prec))


def prf(y_bin, warn):
    t = np.asarray(y_bin).astype(bool); w = np.asarray(warn).astype(bool)
    tp = int((t & w).sum()); fp = int((~t & w).sum()); fn = int((t & ~w).sum()); tn = int((~t & ~w).sum())
    n = tp + fp + fn + tn
    p = tp / (tp + fp) if tp + fp else float("nan")
    r = tp / (tp + fn) if tp + fn else float("nan")
    f1 = 2 * p * r / (p + r) if (p and r and not np.isnan(p) and not np.isnan(r) and p + r) else 0.0
    acc = (tp + tn) / n if n else float("nan")
    specificity = tn / (tn + fp) if tn + fp else float("nan")
    return dict(
        tn=tn, fp=fp, fn=fn, tp=tp,
        acc=acc, prec=p, rec=r, specificity=specificity, f1=f1,
        flag_rate=(tp + fp) / n if n else 0.0,
    )


def print_confusion_matrix(title, metrics):
    """Print matrix with positive class=failure (grade < event)."""
    tn, fp = metrics["tn"], metrics["fp"]
    fn, tp = metrics["fn"], metrics["tp"]
    normal_total = tn + fp
    failure_total = fn + tp
    normal_pct = 100.0 * tn / normal_total if normal_total else float("nan")
    failure_pct = 100.0 * tp / failure_total if failure_total else float("nan")

    print(f"\n  Confusion matrix — {title}")
    print("  " + "-" * 66)
    print(f"  {'Actual / Predicted':<24}{'Normal >=5':>16}{'Failure <5':>16}")
    print("  " + "-" * 66)
    print(f"  {'Normal >=5':<24}{tn:>16,d}{fp:>16,d}")
    print(f"  {'Failure <5':<24}{fn:>16,d}{tp:>16,d}")
    print("  " + "-" * 66)
    print(f"  Normal correctly rejected: {normal_pct:.2f}%")
    print(f"  Failures successfully detected: {failure_pct:.2f}%")


def best_f1_threshold(y_bin, score, min_flag=5):
    """Quet nguong -> tra (tau, flag_rate) toi uu F1. warn = score >= tau."""
    cand = np.unique(np.round(score, 5))
    if len(cand) > 400:
        cand = np.quantile(score, np.linspace(0.01, 0.99, 400))
    best_f, best_tau, best_fr = -1.0, float(np.median(score)), 0.0
    for tau in cand:
        m = prf(y_bin, score >= tau)
        if m["flag_rate"] * len(y_bin) >= min_flag and m["f1"] > best_f:
            best_f, best_tau, best_fr = m["f1"], float(tau), m["flag_rate"]
    return best_tau, best_fr


def list_faculties(splits_dir):
    facs = set()
    for f in sorted(splits_dir.glob("fold_*")):
        for p in f.glob("*_train.csv"):
            facs.add(p.name[:-len("_train.csv")])
    return sorted(facs)


def predict_folds(faculty, folds, splits_dir, event):
    """Walk-forward: moi fold train rieng -> tra list (fold, y, risk_blended, risk_p).
    risk = diem cao hon => rui ro hon. blended: risk = event - grade ; P: risk = P(y<event)."""
    out = []
    for fold in folds:
        data = load_fold(faculty, fold, splits_dir)
        if data is None:
            print(f"  fold {fold}: skip (thieu file / <50 mau train)")
            continue
        (
            Xtr, Xte, ytr, yte,
            train_semester_ordinals, train_semester_codes, _
        ) = data
        pred_grade, p_event = build_and_predict(
            Xtr, ytr, Xte,
            train_semester_ordinals=train_semester_ordinals,
            train_semester_codes=train_semester_codes,
            event=event,
            desc=f"fold {fold}",
        )
        out.append((fold, np.asarray(yte, float), event - np.asarray(pred_grade, float),
                    np.asarray(p_event, float)))
    return out


SIGNALS = ["blended", "P(y<e)"]  # risk_blended, risk_p (index 2,3 trong tuple)


def _risk(rec, signal):
    return rec[2] if signal == "blended" else rec[3]


def warn_by_rate(score, rate):
    """Drift-aware: gan co cho top-`rate` phan tram theo score (khong dung nhan)."""
    if rate <= 0:
        return np.zeros(len(score), bool)
    k = max(1, int(round(rate * len(score))))
    thr = np.partition(score, -k)[-k]
    return score >= thr


def tune_on_dev(dev_recs, event):
    """Voi moi signal: gop dev, tinh AP + tau/flag_rate toi uu F1. Chon signal AP cao hon.
    ratio = flag_rate / prevalence (do 'manh tay' hoc tu dev, de scale theo drift)."""
    y = np.concatenate([r[1] for r in dev_recs]); ybin = (y < event)
    prev = float(ybin.mean())
    info = {}
    for sig in SIGNALS:
        score = np.concatenate([_risk(r, sig) for r in dev_recs])
        ap = average_precision(ybin, score)
        tau, fr = best_f1_threshold(ybin, score)
        m = prf(ybin, score >= tau)
        info[sig] = dict(ap=ap, tau=tau, flag_rate=fr, ratio=fr / max(prev, 1e-6),
                         f1=m["f1"], prec=m["prec"], rec=m["rec"])
    chosen = max(SIGNALS, key=lambda s: (info[s]["ap"] if not np.isnan(info[s]["ap"]) else -1))
    return chosen, info, prev


def report_block(
    title, recs, sig, tau, ratio, prev_map, dev_prev, event,
    show_confusion=False
):
    """In per-fold + POOLED cho MOT signal (blended hoac P(y<e)):
      AP/AUC (frontier, tinh tren score rui-ro lien tuc) +
      ACC/PREC/REC/F1 tai:
        @t  = nguong co dinh tu dev (warn = score_rui_ro >= tau)
        @r  = DRIFT-AWARE: flag top-K fold N, K = ratio * prevalence(fold N-1) (leak-safe).
    POOLED = GOP tat ca fold roi tinh MOT lan (micro-average) — KHONG lay trung binh
    per-fold; day moi la con so dung khi base-rate khac nhau giua cac fold.
    """
    print(f"\n### {title}  [signal={sig}]")
    print(f"{'fold':>6} {'test':>6} {'base%':>6} | {'AP':>6} {'AUC':>6} | "
          f"{'ACC@t':>6} {'P@t':>6} {'R@t':>6} {'F1@t':>6} | "
          f"{'rate%':>6} {'ACC@r':>6} {'P@r':>6} {'R@r':>6} {'F1@r':>6}")
    ys, scs, warns_rate = [], [], []
    for fold, y, rb, rp in recs:
        score = rb if sig == "blended" else rp
        ybin = (y < event)
        rate = float(np.clip(ratio * prev_map.get(fold - 1, dev_prev), 1e-4, 0.99))  # drift-aware
        ap = average_precision(ybin, score); auc = roc_auc(ybin, score)
        a = prf(ybin, score >= tau)
        wr = warn_by_rate(score, rate); b = prf(ybin, wr)
        print(f"{fold:>6} {len(y):>6} {ybin.mean()*100:5.1f}% | {ap:6.3f} {auc:6.3f} | "
              f"{a['acc']:6.3f} {a['prec']:6.3f} {a['rec']:6.3f} {a['f1']:6.3f} | "
              f"{rate*100:5.1f}% {b['acc']:6.3f} {b['prec']:6.3f} {b['rec']:6.3f} {b['f1']:6.3f}")
        if show_confusion:
            print_confusion_matrix(
                f"fold {fold} — fixed threshold @t={tau:.5f}", a
            )
            print_confusion_matrix(
                f"fold {fold} — drift-aware @rate={rate:.3%}", b
            )
        ys.append(y); scs.append(score); warns_rate.append(wr)
    if not ys:
        return
    y = np.concatenate(ys); score = np.concatenate(scs); ybin = (y < event)
    ap = average_precision(ybin, score); auc = roc_auc(ybin, score)
    a = prf(ybin, score >= tau); b = prf(ybin, np.concatenate(warns_rate))
    print("-" * 108)
    print(f"{'POOLED':>6} {len(y):>6} {ybin.mean()*100:5.1f}% | {ap:6.3f} {auc:6.3f} | "
          f"{a['acc']:6.3f} {a['prec']:6.3f} {a['rec']:6.3f} {a['f1']:6.3f} | "
          f"{b['flag_rate']*100:5.1f}% {b['acc']:6.3f} {b['prec']:6.3f} {b['rec']:6.3f} {b['f1']:6.3f}")
    if show_confusion:
        print_confusion_matrix("POOLED — fixed development threshold", a)
        print_confusion_matrix("POOLED — drift-aware warning policy", b)



def report_equal_warning_budgets(
    title, recs, sig, event, rates=EQUAL_BUDGET_RATES
):
    """Compare risk rankings at identical per-semester warning budgets.

    For pooled metrics, each fold independently flags the requested percentage,
    then all decisions are concatenated. This avoids giving one fold a larger
    budget merely because its score scale differs from another fold's scale.
    """
    if not recs:
        return
    print(f"\n### {title} — EQUAL WARNING BUDGETS  [signal={sig}]")
    print(
        f"{'budget':>8} {'flagged':>8} {'TP':>7} {'FP':>7} {'FN':>7} "
        f"{'PREC':>7} {'REC':>7} {'F1':>7}"
    )
    for rate in rates:
        all_y, all_warn = [], []
        for rec in recs:
            y = np.asarray(rec[1], dtype=float)
            score = np.asarray(_risk(rec, sig), dtype=float)
            all_y.append(y < event)
            all_warn.append(warn_by_rate(score, float(rate)))
        ybin = np.concatenate(all_y)
        warn = np.concatenate(all_warn)
        m = prf(ybin, warn)
        print(
            f"{rate*100:7.1f}% {int(warn.sum()):8d} {m['tp']:7d} {m['fp']:7d} "
            f"{m['fn']:7d} {m['prec']:7.3f} {m['rec']:7.3f} {m['f1']:7.3f}"
        )

def _parse_folds(spec, splits_dir):
    if spec.strip().lower() == "all":
        return sorted(int(p.name.split("_")[1]) for p in splits_dir.glob("fold_*"))
    return [int(x) for x in spec.split(",") if x.strip()]

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


def main():
    ap = argparse.ArgumentParser(description="MLP regression walk-forward -> acc/prec/recall y<event.")
    ap.add_argument("--faculty", default="khoa_cong_nghe_thong_tin", help="ten khoa (xem --list)")
    ap.add_argument("--dev_folds", default="1,2,3,4,5", help="fold phat trien/validation")
    ap.add_argument("--test_folds", default="6,7", help="fold holdout (chi xem khi chot)")
    ap.add_argument("--report", default="both", choices=["dev", "test", "both"],
                    help="dev = tune tren fold 1-5; test = holdout 6-7; both = ca hai")
    ap.add_argument("--splits_dir", default=None, help="mac dinh <repo>/data/splits/<strategy>")
    ap.add_argument("--strategy", default="expanding", choices=["expanding", "sliding"])
    ap.add_argument("--event", type=float, default=5.0, help="nguong su kien (y<event); mac dinh 5.0")
    ap.add_argument("--list", action="store_true", help="liet ke faculty roi thoat")
    args = ap.parse_args()

    SEED = 42

    seed_everything(SEED)

    splits_dir = Path(args.splits_dir) if args.splits_dir else \
        Path(__file__).resolve().parent / "data" / "splits" / args.strategy

    if args.list:
        print(f"Splits dir: {splits_dir}")
        for f in list_faculties(splits_dir):
            print("  ", f)
        return
    if not splits_dir.exists():
        sys.exit(f"[ERR] splits_dir khong ton tai: {splits_dir}")
    avail = list_faculties(splits_dir)
    if args.faculty not in avail:
        print(f"[ERR] faculty '{args.faculty}' khong co. Co san:")
        for f in avail:
            print("  ", f)
        sys.exit(1)

    dev = _parse_folds(args.dev_folds, splits_dir)
    test = _parse_folds(args.test_folds, splits_dir)

    dev_ct = torch.cuda.get_device_name(0) if torch.cuda.is_available() else "CPU"
    print("=" * 96)
    print(f"FACULTY: {args.faculty} | strategy={args.strategy} | event y<{args.event}")
    print(
        f"DEVICE: {get_device()} ({dev_ct}) | "
        f"model=MultiTaskNet(reg+cls+blend) | stable_temporal_cls: "
        f"H={TEMPORAL_HALF_LIFE:g}, strength={TEMPORAL_STRENGTH:g}, "
        f"bounds=[{TEMPORAL_MULTIPLIER_MIN:g},{TEMPORAL_MULTIPLIER_MAX:g}]"
    )
    print(f"DEV folds={dev} | HOLDOUT folds={test} | tune=F1 (chon signal theo AP dev)")
    print("=" * 96)

    # 1) DEV: chon signal (AP cao) + tau/flag_rate toi uu F1.
    print("\n>>> DEVELOPMENT (tune tren fold dev)")
    dev_recs = predict_folds(args.faculty, dev, splits_dir, args.event)
    if not dev_recs:
        sys.exit("[ERR] khong co fold dev hop le de tune.")
    chosen, info, dev_prev = tune_on_dev(dev_recs, args.event)
    for sig in SIGNALS:
        i = info[sig]; mark = " <= CHON (AP cao nhat)" if sig == chosen else ""
        print(f"  signal {sig:8}: AP={i['ap']:.3f} | F1-opt tau={i['tau']:.3f} "
              f"flag_rate={i['flag_rate']:.3f} (ratio={i['ratio']:.2f}) -> "
              f"P={i['prec']:.3f} R={i['rec']:.3f} F1={i['f1']:.3f}{mark}")
    # prevalence map (leak-safe: fold N dung prevalence fold N-1 da biet o walk-forward).
    prev_map = {fold: float((y < args.event).mean()) for fold, y, _, _ in dev_recs}

    if args.report in ("dev", "both"):
        report_block("DEVELOPMENT (validation) — BLENDED", dev_recs, "blended",
                     info["blended"]["tau"], info["blended"]["ratio"], prev_map, dev_prev, args.event)
        report_block("DEVELOPMENT (validation) — CLASSIFICATION HEAD P(y<e)", dev_recs, "P(y<e)",
                     info["P(y<e)"]["tau"], info["P(y<e)"]["ratio"], prev_map, dev_prev, args.event)
        report_equal_warning_budgets(
            "DEVELOPMENT (validation) — CLASSIFICATION HEAD",
            dev_recs, "P(y<e)", args.event,
        )

    # 2) HOLDOUT: tau co dinh (dev) + drift-aware flag-rate = ratio * prevalence(ky truoc).
    if args.report in ("test", "both"):
        print("\n" + "!" * 100)
        print("!! HOLDOUT — tau/ratio LAY TU DEV. @tau=nguong co dinh; "
              "@rate=drift-aware (K=ratio*prevalence ky lien truoc, leak-safe).")
        print("!" * 100)
        test_recs = predict_folds(args.faculty, test, splits_dir, args.event)
        for fold, y, _, _ in test_recs:      # bo sung prevalence holdout cho fold ke tiep
            prev_map[fold] = float((y < args.event).mean())
        if test_recs:
            report_block("HOLDOUT (test) — BLENDED", test_recs, "blended",
                         info["blended"]["tau"], info["blended"]["ratio"], prev_map, dev_prev, args.event)
            report_block(
                "HOLDOUT (test) — CLASSIFICATION HEAD P(y<e)",
                test_recs, "P(y<e)",
                info["P(y<e)"]["tau"], info["P(y<e)"]["ratio"],
                prev_map, dev_prev, args.event, show_confusion=True
            )
            report_equal_warning_budgets(
                "HOLDOUT (test) — CLASSIFICATION HEAD",
                test_recs, "P(y<e)", args.event,
            )
    print("=" * 100)


if __name__ == "__main__":
    main()