from __future__ import annotations

from grantflow.eval import harness
from grantflow.eval.harness import apply_runtime_overrides_to_cases


def test_eval_harness_runtime_overrides_apply_to_cases_without_mutating_original():
    source_cases = [
        {"case_id": "c1", "donor_id": "usaid", "llm_mode": False, "architect_rag_enabled": False},
        {"case_id": "c2", "donor_id": "eu"},
    ]
    overridden = apply_runtime_overrides_to_cases(
        source_cases,
        force_llm=True,
        force_architect_rag=True,
    )
    assert overridden is not source_cases
    assert all(bool(case.get("llm_mode")) is True for case in overridden)
    assert all(bool(case.get("architect_rag_enabled")) is True for case in overridden)
    assert source_cases[0]["llm_mode"] is False
    assert "architect_rag_enabled" not in source_cases[1]


def test_eval_harness_runtime_overrides_can_disable_architect_rag_without_mutating_original():
    source_cases = [
        {"case_id": "c1", "donor_id": "usaid", "llm_mode": False, "architect_rag_enabled": True},
        {"case_id": "c2", "donor_id": "eu"},
    ]
    overridden = apply_runtime_overrides_to_cases(
        source_cases,
        force_no_architect_rag=True,
    )
    assert overridden is not source_cases
    assert all(bool(case.get("architect_rag_enabled")) is False for case in overridden)
    assert source_cases[0]["architect_rag_enabled"] is True
    assert "architect_rag_enabled" not in source_cases[1]


def test_build_initial_state_uses_canonical_state_contract():
    state = harness.build_initial_state(
        {
            "case_id": "s1",
            "donor_id": "USAID",
            "input_context": {"project": "AI training", "country": "Kazakhstan"},
            "llm_mode": True,
            "architect_rag_enabled": True,
            "max_iterations": 0,
        }
    )
    assert state["donor_id"] == "usaid"
    assert "donor" not in state
    assert state["input_context"]["project"] == "AI training"
    assert "input" not in state
    assert state["llm_mode"] is True
    assert state["architect_rag_enabled"] is True
    assert state["max_iterations"] == 1
    assert isinstance(state.get("critic_notes"), dict)


def test_run_eval_case_can_skip_expectations(monkeypatch):
    monkeypatch.setattr(
        harness,
        "grantflow_graph",
        type(
            "StubGraph",
            (),
            {
                "invoke": staticmethod(
                    lambda state: {"toc_validation": {"valid": True}, "toc_draft": {}, "logframe_draft": {}}
                )
            },
        )(),
    )
    result = harness.run_eval_case(
        {
            "case_id": "c1",
            "donor_id": "usaid",
            "input_context": {"project": "P", "country": "C"},
            "expectations": {"min_quality_score": 99},
        },
        skip_expectations=True,
    )
    assert result["passed"] is True
    assert result["expectations_skipped"] is True
    assert result["checks"] == []
    assert result["failed_checks"] == []
