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


def test_portfolio_review_workflow_trends_endpoint_aggregates_jobs_and_filters():
    donor = "portfolio_review_workflow_trends_test_donor"
    runtime_state.JOB_STORE.set(
        "portfolio-review-workflow-trends-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-1",
                            "code": "TOC_SCHEMA_INVALID",
                            "severity": "high",
                            "section": "toc",
                            "status": "open",
                            "message": "Schema mismatch.",
                        }
                    ]
                },
                "citations": [{"citation_type": "fallback_namespace"}],
            },
            "review_comments": [
                {
                    "comment_id": "prw-comment-1",
                    "ts": "2026-02-26T10:00:00+00:00",
                    "section": "toc",
                    "status": "open",
                    "message": "Need stronger assumptions.",
                    "linked_finding_id": "prw-1",
                }
            ],
            "job_events": [
                {
                    "event_id": "prw-evt-1",
                    "ts": "2026-02-26T10:00:00+00:00",
                    "type": "review_comment_added",
                    "comment_id": "prw-comment-1",
                    "section": "toc",
                },
                {
                    "event_id": "prw-evt-2",
                    "ts": "2026-02-26T10:10:00+00:00",
                    "type": "critic_finding_status_changed",
                    "finding_id": "prw-1",
                    "status": "open",
                    "section": "toc",
                    "severity": "high",
                },
                {
                    "event_id": "prw-evt-3",
                    "ts": "2026-02-27T11:00:00+00:00",
                    "type": "review_comment_status_changed",
                    "comment_id": "prw-comment-1",
                    "status": "resolved",
                    "section": "toc",
                },
            ],
        },
    )
    runtime_state.JOB_STORE.set(
        "portfolio-review-workflow-trends-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-2",
                            "code": "MEL_BASELINE_MISSING",
                            "severity": "medium",
                            "section": "toc",
                            "status": "acknowledged",
                            "message": "Baseline missing.",
                        }
                    ]
                },
                "citations": [{"citation_type": "rag_support"}],
            },
            "review_comments": [
                {
                    "comment_id": "prw-comment-2",
                    "ts": "2026-02-27T12:05:00+00:00",
                    "section": "toc",
                    "status": "open",
                    "message": "Need indicator source.",
                    "linked_finding_id": "prw-2",
                }
            ],
            "job_events": [
                {
                    "event_id": "prw-evt-4",
                    "ts": "2026-02-27T12:00:00+00:00",
                    "type": "critic_finding_status_changed",
                    "finding_id": "prw-2",
                    "status": "acknowledged",
                    "section": "toc",
                    "severity": "medium",
                },
                {
                    "event_id": "prw-evt-5",
                    "ts": "2026-02-27T12:05:00+00:00",
                    "type": "review_comment_added",
                    "comment_id": "prw-comment-2",
                    "section": "toc",
                },
            ],
        },
    )
    runtime_state.JOB_STORE.set(
        "portfolio-review-workflow-trends-job-3",
        {
            "status": "error",
            "hitl_enabled": False,
            "generate_preflight": {"warning_level": "high", "risk_level": "high"},
            "state": {"donor_id": donor, "critic_notes": {"fatal_flaws": []}},
            "job_events": [
                {
                    "event_id": "prw-evt-6",
                    "ts": "2026-02-28T09:00:00+00:00",
                    "type": "review_comment_added",
                    "comment_id": "prw-comment-3",
                    "section": "toc",
                }
            ],
        },
    )

    response = client.get(
        "/portfolio/review-workflow/trends",
        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_events"] == 2
    assert body["jobs_without_events"] == 0
    assert body["timeline_event_count_total"] == 5
    assert body["bucket_count"] == 2
    assert body["time_window_start"] == "2026-02-26"
    assert body["time_window_end"] == "2026-02-27"
    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"] == 5
    assert body["donor_event_counts"][donor] == 5
    assert body["total_series"] == [
        {"bucket": "2026-02-26", "count": 2},
        {"bucket": "2026-02-27", "count": 3},
    ]
    assert body["event_type_series"]["critic_finding_status_changed"] == [
        {"bucket": "2026-02-26", "count": 1},
        {"bucket": "2026-02-27", "count": 1},
    ]
    assert body["donor_series"][donor] == [
        {"bucket": "2026-02-26", "count": 2},
        {"bucket": "2026-02-27", "count": 3},
    ]
    assert body["job_event_counts"]["portfolio-review-workflow-trends-job-1"] == 3
    assert body["job_event_counts"]["portfolio-review-workflow-trends-job-2"] == 2
