import numpy as np
import pandas as pd

from suite21_reporting import best_fbeta_threshold, build_operational_evaluation, classification_metrics


def test_best_threshold_and_confusion_are_valid():
    y = np.array([0, 0, 0, 1, 1, 1])
    s = np.array([.05, .1, .3, .4, .8, .9])
    threshold, f2, rate = best_fbeta_threshold(y, s, 2.0)
    assert 0 <= threshold <= 1
    assert 0 <= f2 <= 1
    assert 0 <= rate <= 1
    m = classification_metrics(y, s >= threshold, 2.0)
    assert m["tp"] + m["fn"] == 3
    assert m["tn"] + m["fp"] == 3


def test_operational_policy_uses_dev_and_evaluates_holdout():
    rows = []
    rng = np.random.default_rng(3)
    for fold in range(1, 8):
        n = 40
        y = (rng.random(n) < (.08 + .01 * fold)).astype(int)
        score = np.clip(.1 + .65 * y + rng.normal(0, .15, n), 0, 1)
        for i in range(n):
            rows.append({
                "fold": fold,
                "faculty": "f1",
                "actual": int(y[i]),
                "student_id": str(i),
                "course_id": "c",
                "target_semester": fold,
                "history_semesters": 3,
                "committee": float(score[i]),
                "suite21_guarded_temporal_residual": float(score[i]),
            })
    df = pd.DataFrame(rows)
    policies, evaluation, decisions = build_operational_evaluation(df, [1,2,3,4,5], [6,7], 2.0)
    assert len(policies) == 2
    assert set(evaluation.fold) == {6, 7}
    assert {"fixed_tp", "fixed_fp", "drift_tp", "drift_fp"}.issubset(evaluation.columns)
    assert len(decisions) > 0
