from __future__ import annotations

from pathlib import Path

from experiment_b_suite.runner import experiment_directory, resolve_phase
from run_experiment_b import parse_experiments


def test_experiment_parser_supports_all_and_named_variants():
    all_names = parse_experiments("all")
    assert "B0" in all_names and "B8" in all_names
    assert parse_experiments("B0,B6_Q") == ["B0", "B6_Q"]


def test_phase_resolution_keeps_development_as_tuning_for_holdout():
    folds, tuning = resolve_phase("holdout", [1, 2, 3, 4, 5], [6, 7])
    assert folds == [6, 7]
    assert tuning == [1, 2, 3, 4, 5]


def test_experiment_output_path_is_separate_and_deterministic(tmp_path: Path):
    path = experiment_directory(tmp_path, "faculty", "B0", "baseline_exact")
    assert path == tmp_path / "faculty" / "B0_baseline_exact"


def test_aggregate_experiment_outputs_reads_separate_runs(tmp_path: Path):
    import json
    import pandas as pd
    from aggregate_results import aggregate_experiment_outputs

    faculty = "faculty"
    exp_dir = tmp_path / faculty / "B0_baseline_exact"
    exp_dir.mkdir(parents=True)
    summary = {
        "experiment": "B0", "slug": "baseline_exact", "selected_signal": "classification",
        "feature_count_min": 276, "feature_count_max": 276,
        "pooled": {
            "blended": {"ap": 0.1, "auc": 0.6, "fixed_f1": 0.2, "fixed_recall": 0.2, "fixed_precision": 0.2, "drift_f1": 0.2, "drift_recall": 0.2},
            "classification": {"ap": 0.2, "auc": 0.7, "fixed_f1": 0.3, "fixed_recall": 0.4, "fixed_precision": 0.25, "drift_f1": 0.31, "drift_recall": 0.41},
        },
    }
    (exp_dir / "summary_dev.json").write_text(json.dumps(summary))
    pd.DataFrame([
        {"fold": 1, "signal": "blended", "ap": 0.1},
        {"fold": 1, "signal": "classification", "ap": 0.2},
    ]).to_csv(exp_dir / "fold_metrics_dev.csv", index=False)
    pd.DataFrame([
        {"signal": "classification", "budget": 0.15, "recall": 0.5, "f2": 0.45},
        {"signal": "blended", "budget": 0.15, "recall": 0.4, "f2": 0.35},
    ]).to_csv(exp_dir / "equal_budget_metrics_dev.csv", index=False)
    result = aggregate_experiment_outputs(tmp_path, faculty, "dev", ["B0"])
    assert result.loc[0, "classification_ap"] == 0.2
    assert result.loc[0, "classification_recall_at_15pct"] == 0.5
