from __future__ import annotations

from beaver.memory.skills import SkillDraftEvalReport


def test_eval_report_defaults_preserve_legacy_payload_shape() -> None:
    report = SkillDraftEvalReport(
        report_id="eval-1",
        skill_name="debug",
        draft_id="draft-1",
        candidate_id="candidate-1",
        passed=True,
        baseline_score_avg=0.5,
        candidate_score_avg=0.8,
        score_delta=0.3,
        regression_count=0,
        improved_count=2,
        unchanged_count=0,
        cases=[{"run_id": "run-1"}],
        status="completed",
        created_at="now",
    )

    payload = report.to_dict()

    assert payload["eval_version"] == "heuristic-v1"
    assert payload["mode"] == "heuristic"
    assert payload["execution_coverage"] == 0.0
    assert payload["surrogate_coverage"] == 0.0
    assert payload["blocked_coverage"] == 0.0
    assert payload["confidence"] == "low"
    assert payload["case_reports"] == []
    assert payload["tool_mode_summary"] == {}
    assert payload["ability_score_summary"] == {}
    assert payload["tool_execution_summary"] == {}
    assert payload["case_selection_summary"] == {}
    assert payload["real_score_avg"] is None
    assert payload["synthetic_score_avg"] is None
    assert payload["overall_score_avg"] is None
    assert payload["preservation_report"] is None
    assert payload["cases"] == [{"run_id": "run-1"}]


def test_eval_report_reads_legacy_payload_without_replay_fields() -> None:
    report = SkillDraftEvalReport.from_dict(
        {
            "report_id": "eval-legacy",
            "skill_name": "debug",
            "draft_id": "draft-1",
            "candidate_id": "candidate-1",
            "passed": True,
            "baseline_score_avg": 0.4,
            "candidate_score_avg": 0.8,
            "score_delta": 0.4,
            "regression_count": 0,
            "improved_count": 1,
            "unchanged_count": 0,
            "cases": [{"run_id": "run-1"}],
            "status": "completed",
            "created_at": "now",
        }
    )

    assert report.eval_version == "heuristic-v1"
    assert report.mode == "heuristic"
    assert report.confidence == "low"
    assert report.case_reports == []


def test_eval_report_persists_ability_and_case_split_fields() -> None:
    report = SkillDraftEvalReport(
        report_id="eval-replay",
        skill_name="debug",
        draft_id="draft-1",
        candidate_id="candidate-1",
        passed=True,
        baseline_score_avg=0.5,
        candidate_score_avg=0.8,
        score_delta=0.3,
        regression_count=0,
        improved_count=1,
        unchanged_count=0,
        mode="replay",
        eval_version="replay-v2",
        real_score_avg=0.9,
        synthetic_score_avg=0.6,
        overall_score_avg=0.8,
        ability_score_summary={"score_role": "primary", "real_case_count": 1},
        tool_execution_summary={"score_role": "diagnostic_only", "executed": 1.0},
        case_selection_summary={"excluded_synthetic_without_validator": 2},
    )

    payload = report.to_dict()
    restored = SkillDraftEvalReport.from_dict(payload)

    assert payload["real_score_avg"] == 0.9
    assert payload["synthetic_score_avg"] == 0.6
    assert payload["overall_score_avg"] == 0.8
    assert restored.ability_score_summary == {"score_role": "primary", "real_case_count": 1}
    assert restored.tool_execution_summary == {"score_role": "diagnostic_only", "executed": 1.0}
    assert restored.case_selection_summary == {"excluded_synthetic_without_validator": 2}
