from __future__ import annotations

import json

from grantflow.eval import harness


def test_eval_harness_cli_require_seed_readiness_requires_manifest(tmp_path):
    json_out = tmp_path / "eval-report.json"
    text_out = tmp_path / "eval-report.txt"
    exit_code = harness.main(
        [
            "--require-seed-readiness",
            "--json-out",
            str(json_out),
            "--text-out",
            str(text_out),
        ]
    )
    assert exit_code == 2
    assert not json_out.exists()
    assert not text_out.exists()


def test_eval_harness_cli_require_seed_readiness_fails_on_missing_doc_families(tmp_path, monkeypatch):
    manifest = tmp_path / "manifest.jsonl"
    manifest.write_text("{}\n", encoding="utf-8")
    json_out = tmp_path / "eval-report.json"
    text_out = tmp_path / "eval-report.txt"

    monkeypatch.setattr(
        harness,
        "load_eval_cases",
        lambda fixtures_dir=None, case_files=None: [
            {
                "case_id": "strict-usaid",
                "donor_id": "usaid",
                "expected_doc_families": ["donor_policy", "country_context"],
            }
        ],
    )
    monkeypatch.setattr(
        harness,
        "seed_rag_corpus_from_manifest",
        lambda manifest_path, allowed_donor_ids=None: {
            "manifest_path": str(manifest_path),
            "seeded_total": 1,
            "errors": [],
            "donor_counts": {"usaid": 1},
            "donor_doc_family_counts": {"usaid": {"donor_policy": 1}},
        },
    )

    run_called = {"value": False}

    def _fake_run_eval_suite(cases, *, suite_label=None, skip_expectations=False):
        run_called["value"] = True
        return {
            "suite_label": suite_label or "baseline",
            "expectations_skipped": bool(skip_expectations),
            "case_count": 1,
            "passed_count": 1,
            "failed_count": 0,
            "all_passed": True,
            "cases": [],
        }

    monkeypatch.setattr(harness, "run_eval_suite", _fake_run_eval_suite)

    exit_code = harness.main(
        [
            "--seed-rag-manifest",
            str(manifest),
            "--require-seed-readiness",
            "--json-out",
            str(json_out),
            "--text-out",
            str(text_out),
        ]
    )
    assert exit_code == 1
    assert run_called["value"] is False
    assert not json_out.exists()
    assert not text_out.exists()


def test_eval_harness_cli_require_seed_readiness_passes_and_writes_readiness(tmp_path, monkeypatch):
    manifest = tmp_path / "manifest.jsonl"
    manifest.write_text("{}\n", encoding="utf-8")
    json_out = tmp_path / "eval-report.json"
    text_out = tmp_path / "eval-report.txt"

    monkeypatch.setattr(
        harness,
        "load_eval_cases",
        lambda fixtures_dir=None, case_files=None: [
            {
                "case_id": "strict-usaid",
                "donor_id": "usaid",
                "expected_doc_families": ["donor_policy", "country_context"],
                "llm_mode": False,
                "architect_rag_enabled": True,
            }
        ],
    )
    monkeypatch.setattr(
        harness,
        "seed_rag_corpus_from_manifest",
        lambda manifest_path, allowed_donor_ids=None: {
            "manifest_path": str(manifest_path),
            "seeded_total": 2,
            "errors": [],
            "donor_counts": {"usaid": 2},
            "donor_doc_family_counts": {"usaid": {"donor_policy": 1, "country_context": 1}},
        },
    )
    monkeypatch.setattr(
        harness,
        "run_eval_suite",
        lambda cases, suite_label=None, skip_expectations=False: {
            "suite_label": suite_label or "baseline",
            "expectations_skipped": bool(skip_expectations),
            "case_count": 1,
            "passed_count": 1,
            "failed_count": 0,
            "all_passed": True,
            "cases": [],
        },
    )

    exit_code = harness.main(
        [
            "--suite-label",
            "llm-eval-grounded-strict",
            "--force-llm",
            "--force-architect-rag",
            "--seed-rag-manifest",
            str(manifest),
            "--require-seed-readiness",
            "--seed-readiness-min-per-family",
            "1",
            "--json-out",
            str(json_out),
            "--text-out",
            str(text_out),
        ]
    )
    assert exit_code == 0
    payload = json.loads(json_out.read_text(encoding="utf-8"))
    assert payload["runtime_overrides"]["require_seed_readiness"] is True
    assert payload["runtime_overrides"]["seed_readiness_min_per_family"] == 1
    assert payload["seeded_corpus_readiness"]["all_ready"] is True
    assert payload["seeded_corpus_readiness"]["donors"]["usaid"]["missing_doc_families"] == []
