# grantflow/tests/test_integration_critic_findings_list.py

from fastapi.testclient import TestClient

from grantflow.api import runtime_state
from grantflow.api.app import app

client = TestClient(app)


def test_status_critic_findings_list_and_detail_support_filters():
    job_id = "critic-findings-list-1"
    runtime_state.JOB_STORE.set(
        job_id,
        {
            "status": "done",
            "state": {
                "quality_score": 6.0,
                "critic_score": 6.0,
                "needs_revision": True,
                "critic_notes": {
                    "engine": "rules",
                    "fatal_flaws": [
                        {
                            "finding_id": "f-open",
                            "code": "TOC_SCHEMA_INVALID",
                            "severity": "high",
                            "section": "toc",
                            "status": "open",
                            "version_id": "toc_v2",
                            "message": "ToC invalid.",
                            "source": "rules",
                        },
                        {
                            "finding_id": "f-ack",
                            "code": "LOGFRAME_BASELINE_MISSING",
                            "severity": "medium",
                            "section": "logframe",
                            "status": "acknowledged",
                            "version_id": "logframe_v1",
                            "acknowledged_at": "2026-02-27T09:30:00+00:00",
                            "message": "Missing baseline.",
                            "source": "rules",
                        },
                        {
                            "finding_id": "f-resolved",
                            "code": "GENERAL_REVIEW_NOTE",
                            "severity": "low",
                            "section": "general",
                            "status": "resolved",
                            "version_id": None,
                            "resolved_at": "2026-02-27T10:40:00+00:00",
                            "message": "General review note.",
                            "source": "rules",
                        },
                    ],
                    "rule_checks": [],
                },
            },
        },
    )

    list_resp = client.get(f"/status/{job_id}/critic/findings")
    assert list_resp.status_code == 200
    list_body = list_resp.json()
    assert list_body["job_id"] == job_id
    assert list_body["summary"]["finding_count"] == 3
    assert list_body["summary"]["triage_summary"]["top_priority_finding_ids"][0] == "f-open"
    assert len(list_body["findings"]) == 3
    assert list_body["findings"][0]["triage_priority"] in {"urgent", "high", "medium", "normal", "resolved"}
    assert list_body["findings"][0]["review_bucket"] in {"grounding", "measurement", "logic", "compliance", "general"}
    assert list_body["findings"][0]["recommended_action"]
    assert list_body["findings"][0]["staleness_band"] in {"fresh", "aging", "overdue", "resolved"}

    open_resp = client.get(f"/status/{job_id}/critic/findings", params={"status": "open"})
    assert open_resp.status_code == 200
    open_body = open_resp.json()
    assert open_body["filters"]["status"] == "open"
    assert open_body["summary"]["finding_count"] == 1
    assert open_body["findings"][0]["finding_id"] == "f-open"

    unresolved_resp = client.get(f"/status/{job_id}/critic/findings", params={"include_resolved": "false"})
    assert unresolved_resp.status_code == 200
    unresolved_body = unresolved_resp.json()
    assert unresolved_body["filters"]["include_resolved"] is False
    assert unresolved_body["summary"]["finding_count"] == 2
    assert all(str(item.get("status") or "") != "resolved" for item in unresolved_body["findings"])

    resolved_workflow_resp = client.get(
        f"/status/{job_id}/critic/findings",
        params={"workflow_state": "resolved"},
    )
    assert resolved_workflow_resp.status_code == 200
    resolved_workflow_body = resolved_workflow_resp.json()
    assert resolved_workflow_body["summary"]["finding_count"] == 1
    assert resolved_workflow_body["findings"][0]["finding_id"] == "f-resolved"
    assert resolved_workflow_body["findings"][0]["workflow_state"] == "resolved"

    detail_resp = client.get(f"/status/{job_id}/critic/findings/f-ack")
    assert detail_resp.status_code == 200
    detail_body = detail_resp.json()
    assert detail_body["finding_id"] == "f-ack"
    assert detail_body["status"] == "acknowledged"
    assert detail_body["triage_priority"] in {"urgent", "high", "medium", "normal", "resolved"}

    invalid_status_resp = client.get(f"/status/{job_id}/critic/findings", params={"status": "invalid"})
    assert invalid_status_resp.status_code == 400
    assert "Unsupported finding status filter" in str(invalid_status_resp.json().get("detail") or "")
