from grantflow.exporters.donor_contracts import evaluate_export_contract
from grantflow.tests.exporters_helpers import _fixture_json


def test_export_contract_docx_mode_skips_workbook_sheet_requirements():
    contract = evaluate_export_contract(
        donor_id="usaid",
        toc_payload={
            "project_goal": "Improve civic services",
            "development_objectives": [{"do_id": "DO1", "description": "Improved digital delivery"}],
        },
    )
    assert contract["workbook_validation_enabled"] is False
    assert contract["status"] == "pass"
    assert contract["missing_required_sheets"] == []


def test_export_contract_detects_missing_primary_sheet_headers():
    contract = evaluate_export_contract(
        donor_id="usaid",
        toc_payload={
            "project_goal": "Improve civic services",
            "development_objectives": [{"do_id": "DO1", "description": "Improved digital delivery"}],
        },
        workbook_sheetnames=["LogFrame", "USAID_RF", "Template Meta"],
        workbook_primary_sheet_headers=["DO ID", "DO Description", "IR ID"],
    )
    assert contract["status"] == "warning"
    assert contract["missing_required_sheets"] == []
    assert "IR Description" in contract["missing_required_primary_sheet_headers"]
    assert "missing_required_primary_sheet_headers" in contract["warnings"]


def test_export_contract_matches_golden_snapshot():
    fixture = _fixture_json("export_contract_golden.json")
    cases = fixture.get("cases") if isinstance(fixture, dict) else None
    assert isinstance(cases, list) and cases
    for case in cases:
        assert isinstance(case, dict)
        payload = case.get("input")
        expected = case.get("expected")
        assert isinstance(payload, dict)
        assert isinstance(expected, dict)
        contract = evaluate_export_contract(**payload)
        for key, value in expected.items():
            assert contract.get(key) == value


def test_export_contract_normalizes_usaid_alias_payload_keys():
    contract = evaluate_export_contract(
        donor_id="usaid",
        toc_payload={
            "goal": "Improve service quality",
            "objectives": [
                {
                    "id": "OBJ-1",
                    "title": "Digital workflows adopted",
                    "results": [{"id": "R1", "title": "Civil servants trained"}],
                }
            ],
            "assumptions": ["Budget continuity"],
        },
    )
    assert contract["status"] == "pass"
    assert contract["missing_required_sections"] == []
    assert contract["template_key"] == "usaid"


def test_export_contract_resolves_evaluation_rfq_from_wrapped_state_payload():
    wrapped = {
        "state": {
            "donor_id": "un_agencies",
            "input_context": {
                "proposal_mode": "evaluation_rfq",
                "rfq_profile": "katch_final_assessment",
            },
            "toc": {
                "proposal_mode": "evaluation_rfq",
                "rfq_profile": "katch_final_assessment",
                "brief": "Technical response summary",
                "organization_information": "Registered nonprofit evaluation supplier.",
                "evaluation_purpose": "Assess performance and lessons learned.",
                "methodology_overview": "Mixed-methods evaluation design.",
                "methodology_components": [{"method": "Outcome Harvesting"}],
                "workplan_summary": ["Inception", "Fieldwork", "Reporting"],
                "level_of_effort_summary": "Activity-based LOE by phase.",
                "technical_experience_summary": "Comparable evaluation assignments delivered.",
                "team_composition": [{"role": "Team Lead"}],
                "deliverables": [{"deliverable": "Final report", "timing": "Week 8", "purpose": "Submission"}],
            },
        }
    }
    contract = evaluate_export_contract(donor_id="un_agencies", toc_payload=wrapped)
    assert contract["template_key"] == "evaluation_rfq"
    assert "brief" in contract["present_sections"]
