# grantflow/tests/test_integration_portfolio_metrics_exports.py

import gzip

import json


from fastapi.testclient import TestClient


from grantflow.api.app import app

client = TestClient(app)


def test_portfolio_metrics_export_endpoint_supports_csv_json_and_gzip():
    csv_resp = client.get(
        "/portfolio/metrics/export",
        params={"donor_id": "usaid", "status": "done", "grounding_risk_level": "high", "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 "grantflow_portfolio_metrics_usaid_done.csv" in csv_disposition
    csv_text = csv_resp.text
    assert csv_text.startswith("field,value\n")
    assert "filters.donor_id,usaid" in csv_text
    assert "filters.status,done" in csv_text
    assert "filters.grounding_risk_level,high" in csv_text
    assert "avg_time_to_terminal_seconds," in csv_text

    json_resp = client.get(
        "/portfolio/metrics/export",
        params={"donor_id": "usaid", "grounding_risk_level": "high", "format": "json"},
    )
    assert json_resp.status_code == 200
    assert json_resp.headers["content-type"].startswith("application/json")
    json_disposition = json_resp.headers.get("content-disposition", "")
    assert "grantflow_portfolio_metrics_usaid.json" in json_disposition
    payload = json_resp.json()
    assert "job_count" in payload
    assert payload["filters"]["donor_id"] == "usaid"
    assert payload["filters"]["grounding_risk_level"] == "high"

    toc_csv_resp = client.get(
        "/portfolio/metrics/export",
        params={"donor_id": "usaid", "toc_text_risk_level": "high", "format": "csv"},
    )
    assert toc_csv_resp.status_code == 200
    assert toc_csv_resp.headers["content-type"].startswith("text/csv")
    toc_csv_text = toc_csv_resp.text
    assert "filters.toc_text_risk_level,high" in toc_csv_text

    toc_json_resp = client.get(
        "/portfolio/metrics/export",
        params={"donor_id": "usaid", "toc_text_risk_level": "high", "format": "json"},
    )
    assert toc_json_resp.status_code == 200
    assert toc_json_resp.headers["content-type"].startswith("application/json")
    toc_payload = toc_json_resp.json()
    assert toc_payload["filters"]["donor_id"] == "usaid"
    assert toc_payload["filters"]["toc_text_risk_level"] == "high"

    mel_csv_resp = client.get(
        "/portfolio/metrics/export",
        params={"donor_id": "usaid", "mel_risk_level": "medium", "format": "csv"},
    )
    assert mel_csv_resp.status_code == 200
    mel_csv_text = mel_csv_resp.text
    assert "filters.mel_risk_level,medium" in mel_csv_text

    mel_json_resp = client.get(
        "/portfolio/metrics/export",
        params={"donor_id": "usaid", "mel_risk_level": "medium", "format": "json"},
    )
    assert mel_json_resp.status_code == 200
    mel_payload = mel_json_resp.json()
    assert mel_payload["filters"]["donor_id"] == "usaid"
    assert mel_payload["filters"]["mel_risk_level"] == "medium"

    csv_gzip_resp = client.get(
        "/portfolio/metrics/export", params={"donor_id": "usaid", "format": "csv", "gzip": "true"}
    )
    assert csv_gzip_resp.status_code == 200
    assert csv_gzip_resp.headers["content-type"].startswith("application/gzip")
    assert "grantflow_portfolio_metrics_usaid.csv.gz" in (csv_gzip_resp.headers.get("content-disposition") or "")
    csv_gzip_text = gzip.decompress(csv_gzip_resp.content).decode("utf-8")
    assert "field,value\n" in csv_gzip_text

    json_gzip_resp = client.get(
        "/portfolio/metrics/export", params={"donor_id": "usaid", "format": "json", "gzip": "true"}
    )
    assert json_gzip_resp.status_code == 200
    assert json_gzip_resp.headers["content-type"].startswith("application/gzip")
    assert "grantflow_portfolio_metrics_usaid.json.gz" in (json_gzip_resp.headers.get("content-disposition") or "")
    json_gzip_payload = json.loads(gzip.decompress(json_gzip_resp.content).decode("utf-8"))
    assert json_gzip_payload["filters"]["donor_id"] == "usaid"
