# -*- coding: utf-8 -*-
"""
dl_bench.py — single-task regression benchmark for course-grade prediction.

MODEL:
  - one MLP regressor predicts target_grade10
  - warning score = event - predicted_grade
  - warn when predicted_grade < event

LOSS:
  - weighted MSE
  - samples with y < event receive LOW_WEIGHT times the base weight

EVALUATION:
  - development folds: tune the warning-score threshold that maximizes pooled F1
  - holdout folds: report AP/AUC and precision/recall/F1 using both:
      @tau  = fixed threshold learned from development folds
      @rate = drift-aware top-K warning rate based on previous-period prevalence

REPRODUCIBILITY:
  - Python, NumPy, CPU/CUDA PyTorch seeds are fixed
  - deterministic PyTorch/cuDNN behavior is requested where supported

Example:
  python dl_bench.py --faculty khoa_cong_nghe_thong_tin \
      --dev_folds 1,2,3,4,5 --test_folds 6,7 --report both
"""
import argparse
import os
import random
import sys
from pathlib import Path

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"

# ---- Hyperparameters ----
TRUNK = (256, 128)
HEAD = 64
DROPOUT = 0.3
EPOCHS = 100
BATCH = 512
LR = 1e-3
WEIGHT_DECAY = 1e-5
LOW_WEIGHT = 5.0  # Relative weight for samples with y < event
SEED = 42



def set_global_seed(seed=SEED):
    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 get_device():
    return torch.device("cuda" if torch.cuda.is_available() else "cpu")


# =========================================================================== #
#  SINGLE-TASK MODEL: shared MLP trunk + one regression head.                 #
# =========================================================================== #
class RegressionNet(nn.Module):
    def __init__(self, d_in, trunk=TRUNK, head=HEAD, dropout=DROPOUT):
        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),
        )

    def forward(self, x):
        return self.reg_head(self.trunk(x))


def build_and_predict(X_train, y_train, X_test, event=5.0, desc=""):
    """Train RegressionNet and return grade predictions on the original 10-point scale.

    The only objective is weighted MSE. Samples with y < event receive
    LOW_WEIGHT times the weight of the remaining samples.
    """
    set_global_seed(SEED)
    device = get_device()

    Xtr = X_train.to_numpy(dtype=np.float32)
    Xte = X_test.to_numpy(dtype=np.float32)

    # Feature standardization using training statistics only.
    mu, sd = Xtr.mean(0), Xtr.std(0)
    sd[sd == 0] = 1.0
    Xtr = (Xtr - mu) / sd
    Xte = (Xte - mu) / sd

    # Target standardization; inverted after prediction.
    ym = float(y_train.mean())
    ys = float(y_train.std()) or 1.0
    yz = ((y_train - ym) / ys).astype(np.float32)

    # Failure-sensitive regression weights.
    weights = np.where(y_train < event, LOW_WEIGHT, 1.0).astype(np.float32)
    weights /= weights.mean()

    Xtr_t = torch.from_numpy(Xtr).to(device)
    yz_t = torch.from_numpy(yz).view(-1, 1).to(device)
    weights_t = torch.from_numpy(weights).view(-1, 1).to(device)

    model = RegressionNet(Xtr.shape[1]).to(device)
    optimizer = torch.optim.Adam(
        model.parameters(),
        lr=LR,
        weight_decay=WEIGHT_DECAY,
    )

    # Dedicated generator makes batch permutations repeatable.
    generator = torch.Generator(device=device)
    generator.manual_seed(SEED)

    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 epoch in range(EPOCHS):
            permutation = torch.randperm(n, generator=generator, device=device)
            model.train()
            running_loss, steps = 0.0, 0

            for start in range(0, n, BATCH):
                indices = permutation[start:start + BATCH]
                bar.update(1)

                # BatchNorm requires at least two samples in training mode.
                if len(indices) < 2:
                    continue

                xb = Xtr_t[indices]
                yb = yz_t[indices]
                wb = weights_t[indices]

                optimizer.zero_grad(set_to_none=True)
                prediction = model(xb)
                loss = (wb * (prediction - yb) ** 2).mean()
                loss.backward()
                optimizer.step()

                running_loss += loss.item()
                steps += 1

            bar.set_postfix(
                epoch=epoch + 1,
                loss=f"{running_loss / max(steps, 1):.4f}",
            )

    Xte_t = torch.from_numpy(Xte)
    model.eval()
    outputs = []

    with torch.no_grad():
        for start in range(0, len(Xte_t), 8192):
            xb = Xte_t[start:start + 8192].to(device)
            outputs.append(model(xb).view(-1).cpu())

    pred_grade = torch.cat(outputs).numpy() * ys + ym
    return pred_grade
# =========================================================================== #

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

    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, 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")
    return dict(acc=acc, prec=p, rec=r, f1=f1, flag_rate=(tp + fp) / n if n else 0.0)


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):
    """Train one regressor per fold and return (fold, target, warning_score)."""
    output = []

    for fold in folds:
        data = load_fold(faculty, fold, splits_dir)
        if data is None:
            print(f"  fold {fold}: skip (missing files / fewer than 50 train samples)")
            continue

        Xtr, Xte, ytr, yte, _ = data
        pred_grade = build_and_predict(
            Xtr,
            ytr,
            Xte,
            event,
            desc=f"fold {fold}",
        )

        # Larger score means higher risk of falling below the event threshold.
        warning_score = event - np.asarray(pred_grade, dtype=float)
        output.append((fold, np.asarray(yte, dtype=float), warning_score))

    return output


def tune_on_dev(dev_recs, event):
    """Tune one regression-derived warning score on pooled development folds."""
    y = np.concatenate([record[1] for record in dev_recs])
    score = np.concatenate([record[2] for record in dev_recs])
    ybin = y < event
    prevalence = float(ybin.mean())

    ap = average_precision(ybin, score)
    tau, flag_rate = best_f1_threshold(ybin, score)
    metrics = prf(ybin, score >= tau)

    info = {
        "ap": ap,
        "tau": tau,
        "flag_rate": flag_rate,
        "ratio": flag_rate / max(prevalence, 1e-6),
        "f1": metrics["f1"],
        "prec": metrics["prec"],
        "rec": metrics["rec"],
    }
    return info, prevalence

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 report_block(title, recs, tau, ratio, prev_map, dev_prev, event):
    """Report regression-derived warning performance per fold and pooled."""
    print(f"\n### {title}  [signal=event-predicted_grade]")
    print(
        f"{'fold':>6} {'test':>6} {'base%':>6} | {'AP':>6} {'AUC':>6} | "
        f"{'P@tau':>6} {'R@tau':>6} {'F1@tau':>7} | "
        f"{'rate%':>6} {'P@rt':>6} {'R@rt':>6} {'F1@rt':>7}"
    )

    all_y, all_scores, all_rate_warnings = [], [], []

    for fold, y, score in recs:
        ybin = y < event
        rate = float(
            np.clip(
                ratio * prev_map.get(fold - 1, dev_prev),
                1e-4,
                0.99,
            )
        )

        ap = average_precision(ybin, score)
        auc = roc_auc(ybin, score)
        fixed_metrics = prf(ybin, score >= tau)
        rate_warning = warn_by_rate(score, rate)
        rate_metrics = prf(ybin, rate_warning)

        print(
            f"{fold:>6} {len(y):>6} {ybin.mean()*100:5.1f}% | "
            f"{ap:6.3f} {auc:6.3f} | "
            f"{fixed_metrics['prec']:6.3f} {fixed_metrics['rec']:6.3f} "
            f"{fixed_metrics['f1']:7.3f} | "
            f"{rate*100:5.1f}% {rate_metrics['prec']:6.3f} "
            f"{rate_metrics['rec']:6.3f} {rate_metrics['f1']:7.3f}"
        )

        all_y.append(y)
        all_scores.append(score)
        all_rate_warnings.append(rate_warning)

    if not all_y:
        return

    y = np.concatenate(all_y)
    score = np.concatenate(all_scores)
    ybin = y < event

    ap = average_precision(ybin, score)
    auc = roc_auc(ybin, score)
    fixed_metrics = prf(ybin, score >= tau)
    rate_metrics = prf(ybin, np.concatenate(all_rate_warnings))

    print("-" * 100)
    print(
        f"{'POOLED':>6} {len(y):>6} {ybin.mean()*100:5.1f}% | "
        f"{ap:6.3f} {auc:6.3f} | "
        f"{fixed_metrics['prec']:6.3f} {fixed_metrics['rec']:6.3f} "
        f"{fixed_metrics['f1']:7.3f} | "
        f"{'':>6} {rate_metrics['prec']:6.3f} "
        f"{rate_metrics['rec']:6.3f} {rate_metrics['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 main():
    ap = argparse.ArgumentParser(description="Single-task MLP regression walk-forward for y < event warnings.")
    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()

    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}) | model=RegressionNet(single output)")
    print(f"SEED: {SEED} | deterministic=True")
    print(f"DEV folds={dev} | HOLDOUT folds={test} | tune=pooled F1")
    print("=" * 96)

    # 1) DEV: tune the regression-derived warning threshold.
    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.")
    info, dev_prev = tune_on_dev(dev_recs, args.event)
    print(
        f"  regression risk: AP={info['ap']:.3f} | "
        f"F1-opt tau={info['tau']:.3f} "
        f"flag_rate={info['flag_rate']:.3f} "
        f"(ratio={info['ratio']:.2f}) -> "
        f"P={info['prec']:.3f} R={info['rec']:.3f} F1={info['f1']:.3f}"
    )
    tau, ratio = info["tau"], info["ratio"]

    # Fold N uses the already-observed prevalence from fold N-1.
    prev_map = {
        fold: float((y < args.event).mean())
        for fold, y, _ in dev_recs
    }

    if args.report in ("dev", "both"):
        report_block(
            "DEVELOPMENT (validation)",
            dev_recs,
            tau,
            ratio,
            prev_map,
            dev_prev,
            args.event,
        )

    # 2) HOLDOUT: fixed dev threshold plus drift-aware warning rate.
    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:
            prev_map[fold] = float((y < args.event).mean())
        if test_recs:
            report_block(
                "HOLDOUT (test)",
                test_recs,
                tau,
                ratio,
                prev_map,
                dev_prev,
                args.event,
            )
    print("=" * 100)


if __name__ == "__main__":
    main()
