from fastapi.testclient import TestClient

from grantflow.api.app import app
from grantflow.exporters.donor_contracts import donor_submission_requirements

client = TestClient(app)


def test_known_donor_exposes_its_required_structure():
    req = donor_submission_requirements("eu")

    assert req["template_key"] == "eu"
    assert "EU Intervention Logic" in req["required_docx_sections"]
    assert "Overall Objective" in req["required_docx_sections"]
    assert "EU_Intervention" in req["required_xlsx_sheets"]
    assert req["required_toc_sections"]


def test_unmapped_donor_is_generic_not_a_silent_claim():
    req = donor_submission_requirements("some_unmapped_donor")

    assert req["template_key"] == "generic"
    # 'generic' means no donor-specific template is encoded — empty, not invented.
    assert req["required_docx_sections"] == []
    assert req["required_xlsx_sheets"] == []


def test_donors_endpoint_surfaces_submission_requirements_for_agents():
    response = client.get("/donors")
    assert response.status_code == 200

    donors = response.json()["donors"]
    assert donors

    for record in donors:
        assert "submission_requirements" in record
        assert "template_key" in record["submission_requirements"]

    eu = next(record for record in donors if record["id"] == "eu")
    sections = eu["submission_requirements"]["required_docx_sections"]
    assert "EU Intervention Logic" in sections
