import io
import zipfile

from fastapi.testclient import TestClient

from grantflow.api.app import app
from grantflow.tests.pilot_exports_rfq_annex_helpers import katch_submission_kit_toc_draft, export_both_zip

client = TestClient(app)


def test_export_both_zip_includes_katch_submission_kit_files():
    export = export_both_zip(client, katch_submission_kit_toc_draft())

    assert export.status_code == 200
    with zipfile.ZipFile(io.BytesIO(export.content)) as archive:
        names = set(archive.namelist())
        file1 = "rfq_submission_kit/FILE 1 - TECHNICAL PROPOSAL (RFQ-2025-001-KATCH-10019).md"
        file2 = "rfq_submission_kit/FILE 2 - FINANCIAL PROPOSAL (RFQ-2025-001-KATCH-10019).md"
        file3 = "rfq_submission_kit/FILE 3 - ANNEX TEMPLATES.md"
        file4 = "rfq_submission_kit/FILE 4 - READY EMAIL (EN).txt"
        assert file1 in names
        assert file2 in names
        assert file3 in names
        assert file4 in names
        technical = archive.read(file1).decode("utf-8")
        financial = archive.read(file2).decode("utf-8")
        annexes = archive.read(file3).decode("utf-8")
        email = archive.read(file4).decode("utf-8")
        assert "## 1. Organization Information" in technical
        assert "## 2. Analysis and Proposed Approaches / Methodologies" in technical
        assert "## 3. Work Plan" in technical
        assert "## 4. Proposed LOE" in technical
        assert "## 5. Technical Experience & Past Performance" in technical
        assert "## 6. Personnel and Team Composition" in technical
        assert "## 7. Deliverables" in technical
        assert "## A. Budget Table" in financial
        assert "## B. Budget Narrative" in financial
        assert "## C. Fixed Price by Deliverable" in financial
        assert "## Annex A. Evaluation Matrix (short template)" in annexes
        assert "## Annex D. Report Structure" in annexes
        assert "Subject: KATCH FINAL ASSESSMENT - RFQ-2025-001-KATCH-10019" in email
