"""Selection/refit tests use the real learner, including one-class stock fits."""
from pathlib import Path
import ast
import sys

import numpy as np
import pandas as pd
import pytest
import torch
from sklearn.ensemble import HistGradientBoostingClassifier

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from data import Table
from experiment import (Config, VARIANTS, _failure_scores, _select_best,
                        select_recipes, refit_variant, load_bundle, regimes,
                        interventions)


def fixture_table(semesters=(0, 1, 2, 3, 4, 5, 6), per_faculty=12):
    rows = [(s, f, i) for s in semesters for f in range(2)
            for i in range(per_faculty)]
    sem, faculty, student = np.asarray(rows).T
    y = (student % 3 == 0).astype(np.int8)
    # Histories carry real grades/outcomes and overlap students across semesters.
    x = np.c_[student % 4, (student + sem) % 7].astype(float)
    x[student % 7 == 0, 1] = np.nan
    course = np.asarray([f"c{i % 3}" for i in student], dtype=object)
    return Table(x=x, course_meta=np.tile(np.arange(17), (len(rows), 1)),
                 y=y, grade=np.where(y, 3., 8.), semester=sem,
                 student=np.asarray([f"f{f}s{i}" for f, i in zip(faculty, student)], dtype=object),
                 course=course, faculty=faculty,
                 sample_id=np.asarray([f"{s}:{f}:{i}" for s, f, i in rows], dtype=object),
                 committee=np.full(len(rows), np.nan),
                 faculty_names=("A", "B"), feature_names=("raw0", "raw1"))


def tiny_config(**kw):
    defaults = dict(depth_grid=(2,), tree_grid=(1, 2), max_trees=3,
                    path_grid=(0., 1.), min_leaf=2, min_hessian=.01,
                    bins=8, max_history=4, hidden=4, reader_epochs=1,
                    reader_max_rows=36, reader_batch=12, adapt_epochs=1,
                    adapt_max_rows=24, adapt_batch=12, batch_size=64,
                    seeds=(11, 23))
    defaults.update(kw)
    return Config(**defaults)


def audit(origins):
    return pd.DataFrame([dict(semester=o, faculty=f, eligible=True,
                             origin_eligible=True, selected=True)
                         for o in origins for f in ("A", "B")])


@pytest.mark.parametrize("label", [0, 1])
def test_real_stock_one_class_staged_scores_map_classes(label):
    x = np.arange(20).reshape(-1, 1)
    model = HistGradientBoostingClassifier(max_iter=3, min_samples_leaf=2,
                                           early_stopping=False).fit(x, np.full(20, label))
    for proba in model.staged_predict_proba(x):
        np.testing.assert_array_equal(_failure_scores(model, proba), np.full(20, label))


@pytest.mark.parametrize("label", [0, 1])
def test_stock_selection_with_one_class_training_uses_fitted_class(tmp_path, label):
    table = fixture_table()
    older = table.semester < 3
    table.y[older] = label
    table.grade[older] = 3. if label else 8.
    selected = select_recipes(table, [3], audit([3]),
                               tiny_config(variants=("stock",)), tmp_path)
    assert selected["stock"]["trees"] == 1
    cells = pd.read_csv(tmp_path / "selection_cells.csv")
    np.testing.assert_allclose(cells.ap, 1 / 3)


def test_tie_prefers_cycle_then_trees_depth_and_path():
    rows = [dict(variant="mcgb", development_ap=.5, cycle=c, trees=t,
                 depth=d, path_strength=p) for c, t, d, p in
            [(1, 1, 1, 0.), (0, 2, 1, 0.), (0, 1, 3, 0.),
             (0, 1, 2, 1.), (0, 1, 2, 0.)]]
    recipe = _select_best(pd.DataFrame(rows), "mcgb")
    assert (recipe["cycle"], recipe["trees"], recipe["depth"], recipe["path_strength"]) == (0, 1, 2, 0.)


def test_selection_is_cell_weighted_and_independent_per_origin_seed(tmp_path):
    table = fixture_table()
    config = tiny_config(variants=("stock", "native"))
    recipes = select_recipes(table, [3, 6], audit([3, 6]), config, tmp_path)
    assert set(recipes) == {"stock", "native"}
    cells = pd.read_csv(tmp_path / "selection_cells.csv")
    summary = pd.read_csv(tmp_path / "selection_summary.csv")
    keys = ["variant", "depth", "trees", "path_strength", "cycle"]
    assert (cells.groupby(keys).size() == 8).all()
    expected = cells.groupby(keys).ap.mean().reset_index(name="expected")
    joined = summary.merge(expected, on=keys, validate="one_to_one")
    np.testing.assert_allclose(joined.development_ap, joined.expected)
    log = pd.read_csv(tmp_path / "forest_training.csv")
    assert set(log.seed) == {11, 23}
    assert set(log.origin) == {3, 6}
    assert (log.train_max_semester < log.origin).all()
    assert (log.train_rows == log.origin * 24).all()


def test_unequal_cell_sizes_do_not_weight_selection_by_rows(tmp_path):
    table = fixture_table()
    keep = (table.faculty == 0) | np.asarray([int(s.rsplit("s", 1)[1]) < 4 for s in table.student])
    table = table.subset(keep)
    table.x[:, 0] = np.where(table.faculty == 0, table.y, .5)
    select_recipes(table, [3], audit([3]),
                   tiny_config(variants=("stock",), seeds=(11,)), tmp_path)
    cells = pd.read_csv(tmp_path / "selection_cells.csv")
    summary = pd.read_csv(tmp_path / "selection_summary.csv")
    assert set(cells.rows) == {4, 12}
    for _, row in summary.iterrows():
        candidate = cells[cells.trees == row.trees]
        assert row.development_ap == pytest.approx(candidate.ap.mean())
        assert row.development_ap != pytest.approx(np.average(candidate.ap, weights=candidate.rows))


def test_incremental_native_validation_is_exact_prefix_scoring(monkeypatch):
    import experiment
    from data import Preprocessor
    table = fixture_table()
    bank_table, query_table = table.subset(table.semester < 6), table.subset(table.semester == 6)
    prep = Preprocessor.fit(bank_table, np.arange(len(bank_table)))
    config = tiny_config(variants=("deterministic",), seeds=(11,))
    bank, query = prep.transform(bank_table), prep.transform(query_table)
    train = experiment._provider(bank, bank, config, "deterministic")
    validation = experiment._provider(bank, query, config, "deterministic")
    observed = {}
    original = experiment._cell_records

    def capture(table, score, names, **candidate):
        observed[candidate["trees"]] = score.copy()
        return original(table, score, names, **candidate)
    monkeypatch.setattr(experiment, "_cell_records", capture)
    forest = experiment._native_selection(train, validation, np.ones(len(bank)), config,
                                           2, 3, dict(phase="test", origin=6, seed=11),
                                           ("A", "B"), [], [], [("deterministic", 0)])
    for trees, score in observed.items():
        np.testing.assert_allclose(score, forest.predict_proba(validation, trees=trees), atol=0, rtol=0)


def test_selection_ignores_labels_after_the_selected_origin(tmp_path):
    table = fixture_table()
    config = tiny_config(variants=("global", "path"), seeds=(11,), path_grid=(1.,))
    first = select_recipes(table, [3], audit([3]), config, tmp_path / "first")
    table.y[table.semester > 3] = 1 - table.y[table.semester > 3]
    table.grade[table.semester > 3] = np.where(table.y[table.semester > 3], 3., 8.)
    second = select_recipes(table, [3], audit([3]), config, tmp_path / "second")
    assert first == second
    pd.testing.assert_frame_equal(pd.read_csv(tmp_path / "first/selection_cells.csv"),
                                  pd.read_csv(tmp_path / "second/selection_cells.csv"))


def test_all_six_variants_select_on_identical_cells(tmp_path):
    recipes = select_recipes(fixture_table(), [3, 6], audit([3, 6]),
                              tiny_config(), tmp_path)
    assert tuple(recipes) == VARIANTS
    cells = pd.read_csv(tmp_path / "selection_cells.csv")
    for _, group in cells.groupby(["variant", "depth", "trees", "path_strength", "cycle"]):
        assert set(zip(group.origin, group.seed, group.faculty)) == {
            (o, s, f) for o in (3, 6) for s in (11, 23) for f in ("A", "B")}
    reader_log = pd.read_csv(tmp_path / "reader_training.csv")
    assert set(zip(reader_log.origin, reader_log.seed)) == {(o, s) for o in (3, 6) for s in (11, 23)}


def test_selection_rejects_audited_unsupported_origin(tmp_path):
    unsupported = audit([3])
    unsupported["origin_eligible"] = False
    with pytest.raises(ValueError, match="supported"):
        select_recipes(fixture_table(), [3], unsupported,
                       tiny_config(variants=("stock",)), tmp_path)


@pytest.mark.parametrize("variant", VARIANTS)
def test_real_refit_and_reload_match_hard_scores(variant, tmp_path):
    table = fixture_table()
    history = table.subset(table.semester < 6)
    target = table.subset(table.semester == 6)
    recipe = dict(depth=2, trees=2, path_strength=1. if variant in ("path", "mcgb") else 0.,
                  cycle=int(variant == "mcgb"), development_ap=.5)
    bundle, scores, artifacts = refit_variant(history, target, variant, recipe,
                                               tiny_config(), seed=11)
    checkpoint = tmp_path / f"{variant}.pt"
    torch.save(bundle, checkpoint)
    model, provider, reloaded = load_bundle(torch.load(checkpoint, weights_only=False), history, target)
    np.testing.assert_allclose(scores, reloaded, atol=0, rtol=0)
    assert len(scores) == len(target)
    assert np.isfinite(scores).all()
    if variant != "stock":
        assert len(model.trees) == 2
        np.testing.assert_allclose(scores, model.predict_proba(provider), atol=0, rtol=0)
    if variant in ("global", "path", "mcgb"):
        assert all(row["seed"] == 11 for row in artifacts["reader_log"])
        # Auxiliary heads do not enter prediction or reader/forest fingerprints.
        with torch.no_grad():
            for name, param in provider.reader.named_parameters():
                if "head" in name:
                    param.add_(100.)
        np.testing.assert_allclose(scores, model.predict_proba(provider), atol=0, rtol=0)


@pytest.mark.parametrize("label", [0, 1])
def test_real_one_class_refit_reload(label):
    table = fixture_table()
    table.y[:] = label
    table.grade[:] = 3. if label else 8.
    bundle, score, _ = refit_variant(table.subset(table.semester < 6),
                                     table.subset(table.semester == 6), "stock",
                                     dict(depth=2, trees=2, path_strength=0., cycle=0),
                                     tiny_config(variants=("stock",)), 11)
    _, _, loaded = load_bundle(bundle, table.subset(table.semester < 6),
                                table.subset(table.semester == 6))
    np.testing.assert_array_equal(score, np.full(24, label))
    np.testing.assert_array_equal(loaded, score)


def test_target_labels_and_grades_do_not_change_refit_scores():
    table = fixture_table()
    history, target = table.subset(table.semester < 6), table.subset(table.semester == 6)
    recipe = dict(depth=2, trees=2, path_strength=1., cycle=0)
    _, first, _ = refit_variant(history, target, "path", recipe, tiny_config(), 11)
    target.y[:] = 1 - target.y
    target.grade[:] = np.where(target.y, 3., 8.)
    _, second, _ = refit_variant(history, target, "path", recipe, tiny_config(), 11)
    np.testing.assert_array_equal(first, second)


@pytest.mark.parametrize("corruption", ["reader", "forest"])
def test_checkpoint_rejects_corrupt_reader_and_forest_pair(corruption):
    table = fixture_table()
    history, target = table.subset(table.semester < 6), table.subset(table.semester == 6)
    bundle, _, _ = refit_variant(history, target, "global",
                                 dict(depth=2, trees=2, path_strength=0., cycle=0), tiny_config(), 11)
    if corruption == "reader":
        bundle["reader_state"]["same_bonus"] += 1.
    else:
        bundle["forest"]["reader_hash"] = "changed_hash"
    with pytest.raises(ValueError, match="fingerprint"):
        load_bundle(bundle, history, target)


@pytest.mark.parametrize("corruption", ["path_strength", "trees", "depth", "memory_names",
                                         "comparable_features", "raw_names"])
def test_native_reload_rejects_checkpoint_recipe_or_provider_mismatch(corruption):
    table = fixture_table()
    history, target = table.subset(table.semester < 6), table.subset(table.semester == 6)
    bundle, _, _ = refit_variant(history, target, "path",
                                 dict(depth=2, trees=2, path_strength=1., cycle=0), tiny_config(), 11)
    if corruption in ("path_strength", "trees", "depth"):
        bundle["recipe"][corruption] += 1
    elif corruption == "memory_names":
        bundle["forest"]["memory_names"][0] = "wrong_evidence"
    elif corruption == "comparable_features":
        bundle["forest"]["comparable_features"] += 1
    else:
        bundle["forest"]["raw_names"][0] = "wrong_raw"
    with pytest.raises(ValueError, match="recipe|schema|feature"):
        load_bundle(bundle, history, target)


@pytest.mark.parametrize("corruption", ["trees", "depth"])
def test_stock_reload_rejects_checkpoint_recipe_mismatch(corruption):
    table = fixture_table()
    history, target = table.subset(table.semester < 6), table.subset(table.semester == 6)
    bundle, _, _ = refit_variant(history, target, "stock",
                                 dict(depth=2, trees=2, path_strength=0., cycle=0), tiny_config(), 11)
    bundle["recipe"][corruption] += 1
    with pytest.raises(ValueError, match="recipe"):
        load_bundle(bundle, history, target)


def test_no_path_intervention_is_permitted_after_consistent_baseline_reload():
    table = fixture_table()
    history, target = table.subset(table.semester < 6), table.subset(table.semester == 6)
    bundle, scores, _ = refit_variant(history, target, "path",
                                      dict(depth=2, trees=2, path_strength=1., cycle=0), tiny_config(), 11)
    model, provider, loaded = load_bundle(bundle, history, target)
    np.testing.assert_array_equal(loaded, scores)
    changed = provider.clone("no_path", seed=11)
    assert changed.path_strength == 0.
    assert np.isfinite(model.predict_proba(changed)).all()


def test_mcgb_reference_budget_is_fixed_and_final_budget_selected(tmp_path):
    table = fixture_table()
    config = tiny_config(variants=("mcgb",), seeds=(11,), path_grid=(1.,))
    select_recipes(table, [6], audit([6]), config, tmp_path)
    log = pd.read_csv(tmp_path / "forest_training.csv")
    assert (log.trees == config.max_trees).all()
    assert set(log.cycle) == {0, 1}
    bundle, _, artifacts = refit_variant(table.subset(table.semester < 6),
                                        table.subset(table.semester == 6), "mcgb",
                                        dict(depth=2, trees=1, path_strength=1., cycle=1),
                                        config, 11)
    assert len(bundle["forest"]["trees"]) == 1
    assert [r["trees"] for r in artifacts["forest_log"]] == [3, 1]
    assert artifacts["forest_log"][0]["reader_hash"] != artifacts["forest_log"][1]["reader_hash"]
    assert all(row["seed"] == 11 for row in artifacts["adapt_log"])


def test_memory_regimes_and_interventions_keep_raw_features():
    table = fixture_table()
    history = table.subset(table.semester < 6)
    target = table.subset(table.semester == 6)
    _, score, artifacts = refit_variant(history, target, "path",
                                        dict(depth=2, trees=2, path_strength=1., cycle=0),
                                        tiny_config(), 11)
    model, provider = artifacts["model"], artifacts["provider"]
    frame = regimes(target, score, provider, "path", 11)
    assert len(frame) == 12  # 4 history bins and 2 course bins, every faculty.
    assert set(frame.faculty) == {"A", "B"}
    metrics, predictions = interventions(model, provider, target.y, "path", 11)
    assert len(metrics) == 12
    assert len(predictions) == len(target)
    for intervention in ("no_memory", "no_outcomes", "uniform_attention", "no_path",
                         "history_8", "shuffled_history"):
        np.testing.assert_array_equal(provider.clone(intervention, seed=11).raw, provider.raw)
        assert intervention in set(metrics.intervention)
        assert "suite27_path_" + intervention in predictions


def test_raw_diagnostic_frames_have_headers_when_empty():
    table = fixture_table()
    _, _, art = refit_variant(table.subset(table.semester < 6), table.subset(table.semester == 6),
                             "native", dict(depth=2, trees=1, path_strength=0., cycle=0),
                             tiny_config(), 11)
    metric, score = interventions(art["model"], art["provider"], table.y[table.semester == 6],
                                  "native", 11)
    assert metric.empty and len(metric.columns)
    assert score.empty and len(score.columns)


def test_fit_start_progress_and_immediate_cell_ap_logging(tmp_path, capsys, monkeypatch):
    from native import Booster

    def forbid_prefix_predictions(*args, **kwargs):
        raise AssertionError("Progress logging must not request whole-prefix predictions")
    monkeypatch.setattr(Booster, "predict_proba", forbid_prefix_predictions)
    select_recipes(fixture_table(), [3], audit([3]),
                   tiny_config(variants=("stock", "native"), seeds=(11,)), tmp_path)
    output = capsys.readouterr().out
    lines = output.splitlines()
    assert len([line for line in lines if line.startswith("STOCK START ")]) == 1
    assert len([line for line in lines if line.startswith("FOREST START ")]) == 1
    progress = [ast.literal_eval(line.removeprefix("FOREST PROGRESS "))
                for line in lines if line.startswith("FOREST PROGRESS ")]
    assert [row["trees"] for row in progress] == [1, 2]
    checkpoints = [ast.literal_eval(line.removeprefix("SELECTION CHECKPOINT "))
                   for line in lines if line.startswith("SELECTION CHECKPOINT ")]
    assert {(r["variant"], r["trees"], r["faculty"]) for r in checkpoints} == {
        (v, t, f) for v in ("stock", "native") for t in (1, 2) for f in ("A", "B")}
    for row in checkpoints + progress:
        assert row["seed"] == 11 and row["origin"] == 3
        assert row["phase"] == "selection" and row["depth"] == 2
        assert row["path_strength"] == 0. and row["cycle"] == 0
    assert output.index("STOCK START ") < output.index("SELECTION CHECKPOINT ")
    assert output.index("FOREST START ") < output.index("FOREST PROGRESS ")
    assert output.rindex("SELECTION CHECKPOINT ") < output.index("Selection summary")


def test_native_refit_progress_reports_tree_one_every_sixteen_and_last(capsys):
    table = fixture_table()
    refit_variant(table.subset(table.semester < 6), table.subset(table.semester == 6),
                  "native", dict(depth=2, trees=17, path_strength=0., cycle=0),
                  tiny_config(tree_grid=(1, 17), max_trees=17), 23)
    lines = capsys.readouterr().out.splitlines()
    progress = [ast.literal_eval(line.removeprefix("FOREST PROGRESS "))
                for line in lines if line.startswith("FOREST PROGRESS ")]
    assert [row["trees"] for row in progress] == [1, 16, 17]
    for row in progress:
        assert row["seed"] == 23 and row["phase"] == "refit" and row["variant"] == "native"
        assert row["depth"] == 2 and row["path_strength"] == 0. and row["cycle"] == 0
        assert row["seconds"] >= 0 and row["tree_budget"] == 17


def test_stock_refit_announces_fit_context_before_training(capsys):
    table = fixture_table()
    refit_variant(table.subset(table.semester < 6), table.subset(table.semester == 6),
                  "stock", dict(depth=2, trees=2, path_strength=0., cycle=0), tiny_config(), 23)
    starts = [ast.literal_eval(line.removeprefix("STOCK START "))
              for line in capsys.readouterr().out.splitlines() if line.startswith("STOCK START ")]
    assert len(starts) == 1
    row = starts[0]
    assert row["seed"] == 23 and row["phase"] == "refit" and row["variant"] == "stock"
    assert row["depth"] == 2 and row["path_strength"] == 0. and row["cycle"] == 0
    assert row["tree_budget"] == 2 and row["train_rows"] == 144
