import pytest


def _get_any_project_report(client, admin_tokens, auth_headers) -> tuple[int, int, str]:
    list_response = client.get(
        "/api/v1/projects?limit=50",
        headers=auth_headers(admin_tokens),
    )
    assert list_response.status_code == 200
    projects = list_response.json().get("result") or []
    for project in projects:
        project_id = int(project["id"])
        details_response = client.get(
            f"/api/v1/projects/{project_id}",
            headers=auth_headers(admin_tokens),
        )
        if details_response.status_code != 200:
            continue
        reports = (details_response.json().get("result") or {}).get("reports") or []
        if reports:
            report = reports[0]
            return project_id, int(report["year"]), str(report["report_type"])
    pytest.skip("Не найден проект FORM_3 с отчетами для smoke-экспорта")


def test_export_project_report_smoke(client, admin_tokens, auth_headers):
    project_id, report_year, report_type = _get_any_project_report(client, admin_tokens, auth_headers)

    response = client.get(
        f"/api/v1/export/project/{project_id}/report/{report_year}/{report_type}",
        headers=auth_headers(admin_tokens),
    )
    assert response.status_code == 200
    assert response.headers["content-type"] == (
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    )
    assert "attachment; filename*=UTF-8''" in response.headers["content-disposition"]
    assert len(response.content) > 0
