# grantflow/tests/test_integration_portfolio_quality_grounded_gate.py

from fastapi.testclient import TestClient


from grantflow.api import runtime_state

from grantflow.api.app import app

client = TestClient(app)


def test_portfolio_quality_endpoint_includes_grounded_gate_block_metrics():
    donor = "grounded_gate_metrics_test_donor"
    runtime_state.JOB_STORE.set(
        "portfolio-grounded-gate-job-1",
        {
            "status": "done",
            "hitl_enabled": False,
            "state": {
                "donor_id": donor,
                "quality_score": 7.0,
                "critic_score": 7.0,
                "grounded_quality_gate": {
                    "mode": "strict",
                    "passed": False,
                    "blocking": True,
                    "failed_sections": ["toc"],
                    "reason_details": [
                        {"code": "non_retrieval_rate_above_max", "section": "toc"},
                    ],
                },
            },
        },
    )
    runtime_state.JOB_STORE.set(
        "portfolio-grounded-gate-job-2",
        {
            "status": "done",
            "hitl_enabled": False,
            "state": {
                "donor_id": donor,
                "quality_score": 7.5,
                "critic_score": 7.5,
                "grounded_quality_gate": {
                    "mode": "strict",
                    "passed": False,
                    "blocking": True,
                    "reason_details": [
                        {"code": "retrieval_grounded_count_below_min", "section": "logframe"},
                    ],
                },
            },
        },
    )
    runtime_state.JOB_STORE.set(
        "portfolio-grounded-gate-job-3",
        {
            "status": "done",
            "hitl_enabled": False,
            "state": {
                "donor_id": donor,
                "quality_score": 8.0,
                "critic_score": 8.0,
                "grounded_quality_gate": {
                    "mode": "strict",
                    "passed": True,
                    "blocking": False,
                    "summary": "runtime_grounded_signals_ok",
                },
            },
        },
    )

    response = client.get("/portfolio/quality", params={"donor_id": donor, "status": "done"})
    assert response.status_code == 200
    body = response.json()

    assert body["filters"]["donor_id"] == donor
    assert body["grounded_gate_present_job_count"] == 3
    assert body["grounded_gate_blocked_job_count"] == 2
    assert body["grounded_gate_passed_job_count"] == 1
    assert body["grounded_gate_block_rate"] == 0.6667
    assert body["grounded_gate_block_rate_among_present"] == 0.6667
    assert body["grounded_gate_pass_rate_among_present"] == 0.3333
    assert body["grounded_gate_section_fail_counts"]["toc"] >= 1
    assert body["grounded_gate_section_fail_counts"]["logframe"] >= 1
    assert body["grounded_gate_reason_counts"]["non_retrieval_rate_above_max"] >= 1
    assert body["grounded_gate_reason_counts"]["retrieval_grounded_count_below_min"] >= 1

    donor_breakdown = body["donor_grounded_gate_breakdown"][donor]
    assert donor_breakdown["job_count"] == 3
    assert donor_breakdown["present_job_count"] == 3
    assert donor_breakdown["blocked_job_count"] == 2
    assert donor_breakdown["passed_job_count"] == 1
    assert donor_breakdown["block_rate"] == 0.6667
    assert donor_breakdown["block_rate_among_present"] == 0.6667
    assert donor_breakdown["pass_rate_among_present"] == 0.3333
    assert donor_breakdown["section_fail_counts"]["toc"] >= 1
    assert donor_breakdown["section_fail_counts"]["logframe"] >= 1
    assert donor_breakdown["reason_counts"]["non_retrieval_rate_above_max"] >= 1
    assert donor_breakdown["reason_counts"]["retrieval_grounded_count_below_min"] >= 1
