from __future__ import annotations

from fastapi.testclient import TestClient

from grantflow.api.app import app
from grantflow.tests.portfolio_quality_helpers import seed_portfolio_quality_jobs

client = TestClient(app)


def test_portfolio_quality_endpoint_filters_quality_signals():
    donor = seed_portfolio_quality_jobs()
    response = client.get("/portfolio/quality", params={"donor_id": donor, "status": "done", "hitl_enabled": "true"})
    assert response.status_code == 200
    body = response.json()
    warning_filtered = client.get("/portfolio/quality", params={"warning_level": "high"})
    assert warning_filtered.status_code == 200
    warning_filtered_body = warning_filtered.json()
    assert warning_filtered_body["filters"]["warning_level"] == "high"
    assert warning_filtered_body["job_count"] >= 1
    assert warning_filtered_body["warning_level_counts"] == {"high": warning_filtered_body["job_count"]}
    assert warning_filtered_body["warning_level_job_counts"]["high"] == warning_filtered_body["job_count"]
    assert warning_filtered_body["warning_level_job_counts"]["none"] == 0

    grounding_filtered = client.get("/portfolio/quality", params={"grounding_risk_level": "high"})
    assert grounding_filtered.status_code == 200
    grounding_filtered_body = grounding_filtered.json()
    assert grounding_filtered_body["filters"]["grounding_risk_level"] == "high"
    assert grounding_filtered_body["job_count"] >= 1
    assert grounding_filtered_body["grounding_risk_job_counts"]["high"] == grounding_filtered_body["job_count"]
    assert grounding_filtered_body["grounding_risk_job_counts"]["medium"] == 0
    assert grounding_filtered_body["grounding_risk_job_counts"]["low"] == 0

    finding_status_filtered = client.get("/portfolio/quality", params={"finding_status": "acknowledged"})
    assert finding_status_filtered.status_code == 200
    finding_status_filtered_body = finding_status_filtered.json()
    assert finding_status_filtered_body["filters"]["finding_status"] == "acknowledged"
    assert finding_status_filtered_body["job_count"] >= 1
    assert finding_status_filtered_body["finding_status_counts"]["acknowledged"] >= 1

    finding_severity_filtered = client.get("/portfolio/quality", params={"finding_severity": "high"})
    assert finding_severity_filtered.status_code == 200
    finding_severity_filtered_body = finding_severity_filtered.json()
    assert finding_severity_filtered_body["filters"]["finding_severity"] == "high"
    assert finding_severity_filtered_body["job_count"] >= 1
    assert finding_severity_filtered_body["finding_severity_counts"]["high"] >= 1

    toc_text_risk_filtered = client.get("/portfolio/quality", params={"toc_text_risk_level": "high"})
    assert toc_text_risk_filtered.status_code == 200
    toc_text_risk_filtered_body = toc_text_risk_filtered.json()
    assert toc_text_risk_filtered_body["filters"]["toc_text_risk_level"] == "high"
    assert toc_text_risk_filtered_body["job_count"] >= 1
    assert (
        toc_text_risk_filtered_body["toc_text_quality"]["risk_counts"]["high"]
        == toc_text_risk_filtered_body["job_count"]
    )
    assert toc_text_risk_filtered_body["toc_text_quality"]["risk_counts"]["medium"] == 0
    assert toc_text_risk_filtered_body["toc_text_quality"]["risk_counts"]["low"] == 0
    assert toc_text_risk_filtered_body["toc_text_quality"]["risk_counts"]["unknown"] == 0

    mel_level = next(
        (level for level, count in (body.get("mel_risk_job_counts") or {}).items() if int(count or 0) > 0),
        "unknown",
    )
    mel_risk_filtered = client.get("/portfolio/quality", params={"mel_risk_level": mel_level})
    assert mel_risk_filtered.status_code == 200
    mel_risk_filtered_body = mel_risk_filtered.json()
    assert mel_risk_filtered_body["filters"]["mel_risk_level"] == mel_level
    assert mel_risk_filtered_body["job_count"] >= 1
    assert mel_risk_filtered_body["mel_risk_job_counts"][mel_level] == mel_risk_filtered_body["job_count"]
