# grantflow/tests/test_integration_review_workflow_sla_trends.py

from fastapi.testclient import TestClient


from grantflow.api import runtime_state

from grantflow.api.app import app

client = TestClient(app)


def test_status_review_workflow_sla_trends_endpoint_returns_bucketed_series():
    job_id = "review-workflow-sla-trends-job-1"
    runtime_state.JOB_STORE.set(
        job_id,
        {
            "status": "done",
            "state": {
                "critic_notes": {
                    "fatal_flaws": [
                        {
                            "finding_id": "finding-t1",
                            "code": "TOC_SCHEMA_INVALID",
                            "severity": "high",
                            "section": "toc",
                            "status": "open",
                            "message": "ToC mismatch.",
                            "updated_at": "2026-02-27T14:00:00+00:00",
                            "due_at": "2026-02-27T08:00:00+00:00",
                            "sla_hours": 24,
                        },
                        {
                            "finding_id": "finding-t2",
                            "code": "MEL_BASELINE_MISSING",
                            "severity": "medium",
                            "section": "logframe",
                            "status": "acknowledged",
                            "message": "Baseline missing.",
                            "updated_at": "2026-02-27T14:30:00+00:00",
                            "due_at": "2026-02-26T08:00:00+00:00",
                            "sla_hours": 72,
                        },
                    ]
                }
            },
            "review_comments": [
                {
                    "comment_id": "comment-t1",
                    "ts": "2026-02-27T13:00:00+00:00",
                    "section": "toc",
                    "status": "open",
                    "message": "Need stronger assumptions.",
                    "linked_finding_id": "finding-t1",
                    "due_at": "2026-02-27T09:00:00+00:00",
                    "sla_hours": 24,
                },
                {
                    "comment_id": "comment-t2",
                    "ts": "2026-02-27T13:05:00+00:00",
                    "section": "logframe",
                    "status": "open",
                    "message": "Indicator wording update pending.",
                    "linked_finding_id": "finding-t2",
                    "due_at": "2026-02-26T09:00:00+00:00",
                    "sla_hours": 72,
                },
            ],
            "job_events": [
                {
                    "event_id": "rwf-sla-trend-1",
                    "ts": "2026-02-27T15:00:00+00:00",
                    "type": "critic_finding_status_changed",
                    "finding_id": "finding-t2",
                    "status": "acknowledged",
                    "section": "logframe",
                    "severity": "medium",
                }
            ],
        },
    )

    resp = client.get(f"/status/{job_id}/review/workflow/sla/trends", params={"overdue_after_hours": 2})
    assert resp.status_code == 200
    body = resp.json()
    assert body["job_id"] == job_id
    assert body["status"] == "done"
    assert body["filters"] == {"overdue_after_hours": 2}
    assert body["bucket_granularity"] == "day"
    assert body["bucket_count"] == 2
    assert body["time_window_start"] == "2026-02-26"
    assert body["time_window_end"] == "2026-02-27"
    assert body["overdue_total"] == 4
    assert body["total_series"] == [
        {"bucket": "2026-02-26", "count": 2},
        {"bucket": "2026-02-27", "count": 2},
    ]
    assert body["severity_series"]["high"] == [{"bucket": "2026-02-27", "count": 2}]
    assert body["severity_series"]["medium"] == [{"bucket": "2026-02-26", "count": 2}]
    assert body["section_series"]["toc"] == [{"bucket": "2026-02-27", "count": 2}]
    assert body["section_series"]["logframe"] == [{"bucket": "2026-02-26", "count": 2}]

    code_filtered = client.get(
        f"/status/{job_id}/review/workflow/sla/trends",
        params={"finding_code": "TOC_SCHEMA_INVALID", "overdue_after_hours": 2},
    )
    assert code_filtered.status_code == 200
    code_payload = code_filtered.json()
    assert code_payload["filters"]["finding_code"] == "TOC_SCHEMA_INVALID"
    assert code_payload["overdue_total"] == 2
    assert code_payload["bucket_count"] == 1
    assert code_payload["total_series"] == [{"bucket": "2026-02-27", "count": 2}]
