from grantflow.tests.security_openapi_helpers import openapi_spec, parameter_names_for

PORTFOLIO_REVIEW_WORKFLOW_PATHS = [
    "/portfolio/review-workflow",
    "/portfolio/review-workflow/export",
    "/portfolio/review-workflow/trends",
    "/portfolio/review-workflow/trends/export",
]

PORTFOLIO_REVIEW_WORKFLOW_SLA_PATHS = [
    "/portfolio/review-workflow/sla",
    "/portfolio/review-workflow/sla/export",
    "/portfolio/review-workflow/sla/hotspots",
    "/portfolio/review-workflow/sla/hotspots/export",
    "/portfolio/review-workflow/sla/hotspots/trends",
    "/portfolio/review-workflow/sla/hotspots/trends/export",
    "/portfolio/review-workflow/sla/trends",
    "/portfolio/review-workflow/sla/trends/export",
]

JOB_REVIEW_WORKFLOW_PATHS = [
    "/status/{job_id}/review/workflow",
    "/status/{job_id}/review/workflow/trends",
    "/status/{job_id}/review/workflow/trends/export",
    "/status/{job_id}/review/workflow/sla",
    "/status/{job_id}/review/workflow/sla/export",
    "/status/{job_id}/review/workflow/sla/hotspots",
    "/status/{job_id}/review/workflow/sla/hotspots/export",
    "/status/{job_id}/review/workflow/sla/hotspots/trends",
    "/status/{job_id}/review/workflow/sla/hotspots/trends/export",
    "/status/{job_id}/review/workflow/sla/trends",
    "/status/{job_id}/review/workflow/sla/trends/export",
]


def test_openapi_declares_portfolio_quality_filter_parameters():
    spec = openapi_spec()

    for path in [
        "/portfolio/metrics",
        "/portfolio/metrics/export",
        "/portfolio/quality",
        "/portfolio/quality/export",
    ]:
        names = parameter_names_for(spec, path, "get")
        assert "toc_text_risk_level" in names
        assert "mel_risk_level" in names


def test_openapi_declares_review_workflow_query_parameters():
    spec = openapi_spec()

    for path in PORTFOLIO_REVIEW_WORKFLOW_PATHS:
        names = parameter_names_for(spec, path, "get")
        for name in [
            "donor_id",
            "status",
            "hitl_enabled",
            "warning_level",
            "grounding_risk_level",
            "toc_text_risk_level",
            "event_type",
        ]:
            assert name in names

    for path in PORTFOLIO_REVIEW_WORKFLOW_SLA_PATHS:
        names = parameter_names_for(spec, path, "get")
        for name in [
            "donor_id",
            "status",
            "hitl_enabled",
            "warning_level",
            "grounding_risk_level",
            "toc_text_risk_level",
        ]:
            assert name in names

    for path in [*PORTFOLIO_REVIEW_WORKFLOW_PATHS, *PORTFOLIO_REVIEW_WORKFLOW_SLA_PATHS, *JOB_REVIEW_WORKFLOW_PATHS]:
        names = parameter_names_for(spec, path, "get")
        for name in [
            "finding_id",
            "finding_code",
            "finding_section",
            "comment_status",
            "workflow_state",
            "overdue_after_hours",
        ]:
            assert name in names


def test_openapi_declares_review_workflow_sla_hotspot_and_export_parameters():
    spec = openapi_spec()

    hotspot_paths = [
        "/status/{job_id}/review/workflow/sla/hotspots",
        "/status/{job_id}/review/workflow/sla/hotspots/export",
        "/status/{job_id}/review/workflow/sla/hotspots/trends",
        "/status/{job_id}/review/workflow/sla/hotspots/trends/export",
        "/portfolio/review-workflow/sla/hotspots",
        "/portfolio/review-workflow/sla/hotspots/export",
        "/portfolio/review-workflow/sla/hotspots/trends",
        "/portfolio/review-workflow/sla/hotspots/trends/export",
    ]
    for path in hotspot_paths:
        names = parameter_names_for(spec, path, "get")
        for name in ["top_limit", "hotspot_kind", "hotspot_severity", "min_overdue_hours"]:
            assert name in names

    for path in [
        "/portfolio/review-workflow/trends/export",
        "/portfolio/review-workflow/export",
        "/portfolio/review-workflow/sla/export",
        "/portfolio/review-workflow/sla/hotspots/export",
        "/portfolio/review-workflow/sla/hotspots/trends/export",
        "/portfolio/review-workflow/sla/trends/export",
        "/status/{job_id}/review/workflow/trends/export",
        "/status/{job_id}/review/workflow/sla/trends/export",
        "/status/{job_id}/review/workflow/sla/export",
        "/status/{job_id}/review/workflow/sla/hotspots/export",
        "/status/{job_id}/review/workflow/sla/hotspots/trends/export",
    ]:
        names = parameter_names_for(spec, path, "get")
        assert "format" in names
        assert "gzip" in names

    for path in [
        "/portfolio/review-workflow/sla",
        "/portfolio/review-workflow/sla/export",
    ]:
        assert "top_limit" in parameter_names_for(spec, path, "get")
