# grantflow/tests/test_integration_review_workflow_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_trends_endpoint_returns_bucketed_series():
    job_id = "review-workflow-trends-job-1"
    runtime_state.JOB_STORE.set(
        job_id,
        {
            "status": "done",
            "state": {
                "critic_notes": {
                    "fatal_flaws": [
                        {
                            "finding_id": "finding-wt-1",
                            "code": "TOC_SCHEMA_INVALID",
                            "severity": "high",
                            "section": "toc",
                            "status": "open",
                            "message": "ToC mismatch.",
                            "updated_at": "2026-02-26T11:00:00+00:00",
                        },
                        {
                            "finding_id": "finding-wt-2",
                            "code": "MEL_BASELINE_MISSING",
                            "severity": "medium",
                            "section": "logframe",
                            "status": "acknowledged",
                            "message": "MEL baseline missing.",
                            "updated_at": "2026-02-27T11:00:00+00:00",
                        },
                    ]
                }
            },
            "review_comments": [
                {
                    "comment_id": "comment-wt-1",
                    "ts": "2026-02-26T10:00:00+00:00",
                    "section": "toc",
                    "status": "open",
                    "message": "Needs stronger assumptions.",
                    "linked_finding_id": "finding-wt-1",
                },
                {
                    "comment_id": "comment-wt-2",
                    "ts": "2026-02-27T10:00:00+00:00",
                    "section": "logframe",
                    "status": "resolved",
                    "message": "Resolved in review pass.",
                    "linked_finding_id": "finding-wt-2",
                },
            ],
            "job_events": [
                {
                    "event_id": "rwf-tr-1",
                    "ts": "2026-02-26T10:00:00+00:00",
                    "type": "review_comment_added",
                    "comment_id": "comment-wt-1",
                    "section": "toc",
                },
                {
                    "event_id": "rwf-tr-2",
                    "ts": "2026-02-26T11:00:00+00:00",
                    "type": "critic_finding_status_changed",
                    "finding_id": "finding-wt-1",
                    "status": "open",
                    "section": "toc",
                    "severity": "high",
                },
                {
                    "event_id": "rwf-tr-3",
                    "ts": "2026-02-27T10:00:00+00:00",
                    "type": "review_comment_status_changed",
                    "comment_id": "comment-wt-2",
                    "status": "resolved",
                    "section": "logframe",
                },
                {
                    "event_id": "rwf-tr-4",
                    "ts": "2026-02-27T11:00:00+00:00",
                    "type": "critic_finding_status_changed",
                    "finding_id": "finding-wt-2",
                    "status": "acknowledged",
                    "section": "logframe",
                    "severity": "medium",
                },
            ],
        },
    )

    resp = client.get(f"/status/{job_id}/review/workflow/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["timeline_event_count"] == 4
    assert body["top_event_type"] == "critic_finding_status_changed"
    assert body["top_event_type_count"] == 2
    assert body["total_series"] == [
        {"bucket": "2026-02-26", "count": 2},
        {"bucket": "2026-02-27", "count": 2},
    ]
    assert body["event_type_series"]["review_comment_added"] == [{"bucket": "2026-02-26", "count": 1}]
    assert body["event_type_series"]["critic_finding_status_changed"] == [
        {"bucket": "2026-02-26", "count": 1},
        {"bucket": "2026-02-27", "count": 1},
    ]

    code_filtered = client.get(
        f"/status/{job_id}/review/workflow/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["bucket_count"] == 1
    assert code_payload["timeline_event_count"] == 2
    assert code_payload["total_series"] == [{"bucket": "2026-02-26", "count": 2}]
