from grantflow.api import runtime_state
from grantflow.tests.portfolio_review_workflow_helpers import client


def test_portfolio_review_workflow_endpoint_aggregates_jobs_and_filters():
    donor = "portfolio_review_workflow_test_donor"
    runtime_state.JOB_STORE.set(
        "portfolio-review-workflow-job-1",
        {
            "status": "done",
            "hitl_enabled": True,
            "generate_preflight": {"warning_level": "medium", "risk_level": "medium"},
            "state": {
                "donor_id": donor,
                "critic_notes": {
                    "fatal_flaws": [
                        {
                            "finding_id": "prw-snap-1",
                            "code": "TOC_SCHEMA_INVALID",
                            "severity": "high",
                            "section": "toc",
                            "status": "open",
                            "message": "Schema mismatch.",
                            "updated_at": "2026-02-27T10:00:00+00:00",
                            "due_at": "2026-02-25T09:00:00+00:00",
                        }
                    ]
                },
            },
            "review_comments": [
                {
                    "comment_id": "prw-snap-comment-1",
                    "ts": "2026-02-27T10:05:00+00:00",
                    "section": "toc",
                    "status": "open",
                    "message": "Need stronger assumptions.",
                    "linked_finding_id": "prw-snap-1",
                    "due_at": "2026-02-25T10:00:00+00:00",
                }
            ],
            "job_events": [
                {
                    "event_id": "prw-snap-evt-1",
                    "ts": "2026-02-27T10:00:00+00:00",
                    "type": "critic_finding_status_changed",
                    "finding_id": "prw-snap-1",
                    "status": "open",
                    "section": "toc",
                    "severity": "high",
                },
                {
                    "event_id": "prw-snap-evt-2",
                    "ts": "2026-02-27T10:05:00+00:00",
                    "type": "review_comment_added",
                    "comment_id": "prw-snap-comment-1",
                    "section": "toc",
                },
            ],
        },
    )
    runtime_state.JOB_STORE.set(
        "portfolio-review-workflow-job-2",
        {
            "status": "done",
            "hitl_enabled": True,
            "generate_preflight": {"warning_level": "low", "risk_level": "low"},
            "state": {
                "donor_id": donor,
                "critic_notes": {
                    "fatal_flaws": [
                        {
                            "finding_id": "prw-snap-2",
                            "code": "MEL_BASELINE_MISSING",
                            "severity": "medium",
                            "section": "toc",
                            "status": "acknowledged",
                            "message": "Baseline missing.",
                            "acknowledged_at": "2026-02-27T11:00:00+00:00",
                            "due_at": "2026-02-25T11:00:00+00:00",
                        }
                    ]
                },
            },
            "review_comments": [
                {
                    "comment_id": "prw-snap-comment-2",
                    "ts": "2026-02-27T11:05:00+00:00",
                    "section": "toc",
                    "status": "resolved",
                    "message": "Resolved after review.",
                    "linked_finding_id": "prw-snap-2",
                    "resolved_at": "2026-02-27T11:10:00+00:00",
                }
            ],
            "job_events": [
                {
                    "event_id": "prw-snap-evt-3",
                    "ts": "2026-02-27T11:00:00+00:00",
                    "type": "critic_finding_status_changed",
                    "finding_id": "prw-snap-2",
                    "status": "acknowledged",
                    "section": "toc",
                    "severity": "medium",
                },
                {
                    "event_id": "prw-snap-evt-4",
                    "ts": "2026-02-27T11:05:00+00:00",
                    "type": "review_comment_status_changed",
                    "comment_id": "prw-snap-comment-2",
                    "status": "resolved",
                    "section": "toc",
                },
            ],
        },
    )
    runtime_state.JOB_STORE.set(
        "portfolio-review-workflow-job-3",
        {
            "status": "done",
            "hitl_enabled": False,
            "state": {"donor_id": donor, "critic_notes": {"fatal_flaws": []}},
            "job_events": [],
        },
    )

    response = client.get(
        "/portfolio/review-workflow",
        params={
            "donor_id": donor,
            "status": "done",
            "hitl_enabled": "true",
            "finding_section": "toc",
            "overdue_after_hours": 12,
        },
    )
    assert response.status_code == 200
    body = response.json()
    assert body["filters"]["donor_id"] == donor
    assert body["filters"]["status"] == "done"
    assert body["filters"]["hitl_enabled"] is True
    assert body["filters"]["finding_section"] == "toc"
    assert body["filters"]["overdue_after_hours"] == 12
    assert body["job_count"] == 2
    assert body["jobs_with_activity"] == 2
    assert body["jobs_without_activity"] == 0
    assert body["jobs_with_overdue"] == 2
    assert body["jobs_without_overdue"] == 0
    assert body["summary"]["finding_count"] == 2
    assert body["summary"]["comment_count"] == 2
    assert body["summary"]["open_finding_count"] == 1
    assert body["summary"]["acknowledged_finding_count"] == 1
    assert body["summary"]["open_comment_count"] == 1
    assert body["summary"]["resolved_comment_count"] == 1
    assert body["summary"]["timeline_event_count"] == 4
    assert body["top_event_type"] == "critic_finding_status_changed"
    assert body["top_event_type_count"] == 2
    assert body["top_donor_id"] == donor
    assert body["top_donor_event_count"] == 4
    assert body["timeline_event_type_counts"]["critic_finding_status_changed"] == 2
    assert body["timeline_event_type_counts"]["review_comment_status_changed"] == 1
    assert body["timeline_event_type_counts"]["review_comment_added"] == 1
    assert body["donor_event_counts"][donor] == 4
    assert body["job_event_counts"]["portfolio-review-workflow-job-1"] == 2
    assert body["job_event_counts"]["portfolio-review-workflow-job-2"] == 2
    assert body["latest_timeline_limit"] == 200
    assert body["latest_timeline_truncated"] is False
    assert len(body["latest_timeline"]) == 4
    assert body["latest_timeline"][0]["job_id"] == "portfolio-review-workflow-job-2"
    assert body["latest_timeline"][0]["donor_id"] == donor
