# -*- 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).
"""
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
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")


# =========================================================================== #
#  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, event=5.0, desc=""):
    """Huan luyen MultiTaskNet; tra ve du doan diem BLENDED (thang 10) cho X_test.

    Loss = W_REG*weightedMSE(reg) + W_CLS*BCE(cls, pos_weight) + W_BLEND*weightedMSE(blended).
    weightedMSE: mau y<event nang LOW_WEIGHT (mat can bang vung diem thap).
    BCE pos_weight = (#neg / #pos) tu train (mat can bang lop y<event).
    """
    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}"
    )

    w = np.where(y_train < event, LOW_WEIGHT, 1.0).astype(np.float32)
    w = w / w.mean()                                          # chuan hoa cho LR on dinh
    # 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)
    w_t = torch.from_numpy(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")

    cb_low_t = torch.tensor(
        cb_low,
        dtype=torch.float32,
        device=device
    )

    cb_high_t = torch.tensor(
        cb_high,
        dtype=torch.float32,
        device=device
    )

    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, yzb, ybb, wb = Xtr_t[j], yz_t[j], yb_t[j], w_t[j]
                opt.zero_grad()
                reg, logit, _p, blended = model(xb)
                l_reg = (wb * (reg - yzb) ** 2).mean()
                cls_loss_each = bce(logit, ybb)

                cls_weight = torch.where(
                    ybb > 0.5,
                    cb_low_t,
                    cb_high_t,
                )

                l_cls = (cls_weight * cls_loss_each).mean()
                
                l_blend = (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

    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):
    """
    Positive class: failure, target_grade10 < event.
    Negative class: normal, target_grade10 >= event.
    """
    target = np.asarray(y_bin).astype(bool)
    prediction = np.asarray(warn).astype(bool)

    tp = int((target & prediction).sum())
    fp = int((~target & prediction).sum())
    fn = int((target & ~prediction).sum())
    tn = int((~target & ~prediction).sum())

    n = tp + fp + fn + tn

    precision = (
        tp / (tp + fp)
        if (tp + fp) > 0
        else float("nan")
    )

    recall = (
        tp / (tp + fn)
        if (tp + fn) > 0
        else float("nan")
    )

    specificity = (
        tn / (tn + fp)
        if (tn + fp) > 0
        else float("nan")
    )

    if (
        not np.isnan(precision)
        and not np.isnan(recall)
        and (precision + recall) > 0
    ):
        f1 = 2.0 * precision * recall / (precision + recall)
    else:
        f1 = 0.0

    accuracy = (
        (tp + tn) / n
        if n > 0
        else float("nan")
    )

    flag_rate = (
        (tp + fp) / n
        if n > 0
        else 0.0
    )

    return {
        "tn": tn,
        "fp": fp,
        "fn": fn,
        "tp": tp,
        "acc": accuracy,
        "prec": precision,
        "rec": recall,
        "specificity": specificity,
        "f1": f1,
        "flag_rate": flag_rate,
    }


def print_confusion_matrix(title, metrics):
    """
    Matrix orientation:

                         Predicted normal   Predicted failure
    Actual normal              TN                 FP
    Actual failure             FN                 TP
    """
    tn = metrics["tn"]
    fp = metrics["fp"]
    fn = metrics["fn"]
    tp = metrics["tp"]

    normal_total = tn + fp
    failure_total = fn + tp

    normal_correct_pct = (
        100.0 * tn / normal_total
        if normal_total > 0
        else float("nan")
    )

    failure_detected_pct = (
        100.0 * tp / failure_total
        if failure_total > 0
        else float("nan")
    )

    print(f"\n  Confusion matrix — {title}")
    print("  " + "-" * 66)
    print(
        f"  {'Actual / Predicted':<24}"
        f"{'Normal >=5':>16}"
        f"{'Failure <5':>16}"
    )
    print("  " + "-" * 66)
    print(
        f"  {'Normal >=5':<24}"
        f"{tn:>16,d}"
        f"{fp:>16,d}"
    )
    print(
        f"  {'Failure <5':<24}"
        f"{fn:>16,d}"
        f"{tp:>16,d}"
    )
    print("  " + "-" * 66)
    print(
        f"  Normal correctly rejected: {normal_correct_pct:.2f}%"
    )
    print(
        f"  Failures successfully detected: {failure_detected_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, _ = data
        pred_grade, p_event = build_and_predict(Xtr, ytr, Xte, 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(
                title=f"fold {fold} — fixed threshold @t={tau:.5f}",
                metrics=a,
            )

            print_confusion_matrix(
                title=f"fold {fold} — drift-aware @rate={rate:.3%}",
                metrics=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(
            title="POOLED — fixed development threshold",
            metrics=a,
        )

        print_confusion_matrix(
            title="POOLED — drift-aware warning policy",
            metrics=b,
        )


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}) | model=MultiTaskNet(reg+cls+blend)")
    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)

    # 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,
            )
    print("=" * 100)


if __name__ == "__main__":
    main()
