import gzip
import json

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


def test_portfolio_review_workflow_export_supports_csv_json_and_gzip():
    donor = "portfolio_review_workflow_export_test_donor"
    runtime_state.JOB_STORE.set(
        "portfolio-review-workflow-export-job-1",
        {
            "status": "done",
            "hitl_enabled": True,
            "state": {
                "donor_id": donor,
                "critic_notes": {
                    "fatal_flaws": [
                        {
                            "finding_id": "prw-exp-1",
                            "code": "TOC_SCHEMA_INVALID",
                            "severity": "high",
                            "section": "toc",
                            "status": "open",
                            "updated_at": "2026-02-27T10:00:00+00:00",
                            "due_at": "2026-02-25T09:00:00+00:00",
                        }
                    ]
                },
            },
            "review_comments": [
                {
                    "comment_id": "prw-exp-comment-1",
                    "ts": "2026-02-27T10:05:00+00:00",
                    "section": "toc",
                    "status": "open",
                    "message": "Review needed.",
                    "linked_finding_id": "prw-exp-1",
                    "due_at": "2026-02-25T10:00:00+00:00",
                }
            ],
            "job_events": [
                {
                    "event_id": "prw-exp-evt-1",
                    "ts": "2026-02-27T10:00:00+00:00",
                    "type": "critic_finding_status_changed",
                    "finding_id": "prw-exp-1",
                    "status": "open",
                    "section": "toc",
                    "severity": "high",
                },
                {
                    "event_id": "prw-exp-evt-2",
                    "ts": "2026-02-27T10:05:00+00:00",
                    "type": "review_comment_added",
                    "comment_id": "prw-exp-comment-1",
                    "section": "toc",
                },
            ],
        },
    )

    csv_resp = client.get(
        "/portfolio/review-workflow/export",
        params={
            "donor_id": donor,
            "status": "done",
            "hitl_enabled": "true",
            "finding_section": "toc",
            "format": "csv",
        },
    )
    assert csv_resp.status_code == 200
    assert csv_resp.headers["content-type"].startswith("text/csv")
    csv_disposition = csv_resp.headers.get("content-disposition", "")
    assert f"grantflow_portfolio_review_workflow_{donor}_done_hitl_true.csv" in csv_disposition
    csv_text = csv_resp.text
    assert csv_text.startswith("field,value\n")
    assert f"filters.donor_id,{donor}" in csv_text
    assert "filters.status,done" in csv_text
    assert "filters.hitl_enabled,True" in csv_text
    assert "filters.finding_section,toc" in csv_text
    assert "summary.timeline_event_count,2" in csv_text

    json_resp = client.get(
        "/portfolio/review-workflow/export",
        params={"donor_id": donor, "finding_section": "toc", "format": "json"},
    )
    assert json_resp.status_code == 200
    assert json_resp.headers["content-type"].startswith("application/json")
    json_payload = json_resp.json()
    assert json_payload["filters"]["donor_id"] == donor
    assert json_payload["filters"]["finding_section"] == "toc"
    assert json_payload["summary"]["timeline_event_count"] == 2
    assert json_payload["summary"]["comment_count"] == 1

    gzip_resp = client.get(
        "/portfolio/review-workflow/export",
        params={"donor_id": donor, "format": "json", "gzip": "true"},
    )
    assert gzip_resp.status_code == 200
    assert gzip_resp.headers["content-type"].startswith("application/gzip")
    gzip_disposition = gzip_resp.headers.get("content-disposition", "")
    assert f"grantflow_portfolio_review_workflow_{donor}.json.gz" in gzip_disposition
    gzip_payload = json.loads(gzip.decompress(gzip_resp.content).decode("utf-8"))
    assert gzip_payload["filters"]["donor_id"] == donor
    assert gzip_payload["summary"]["timeline_event_count"] == 2
