from __future__ import annotations

from grantflow.core.evaluation_rfq import evaluation_rfq_schema
from grantflow.swarm.nodes.architect_generation import _fallback_structured_toc


def test_fallback_structured_toc_supports_evaluation_rfq_mode():
    payload, engine = _fallback_structured_toc(
        evaluation_rfq_schema(),
        donor_id="un_agencies",
        project="KATCH Project Performance Evaluation",
        country="Kyrgyzstan",
        revision_hint="",
        evidence_hits=[],
        input_context={
            "proposal_mode": "evaluation_rfq",
            "methods": [
                "Outcome Harvesting",
                "Social Media Analysis",
                "Focus Group Discussions",
                "Survey of Beneficiaries",
            ],
        },
    )

    assert engine == "evaluation_rfq_contract_synthesizer"
    assert payload["proposal_mode"] == "evaluation_rfq"
    assert "technical response" in str(payload["brief"]).lower()
    assert isinstance(payload.get("methodology_components"), list) and payload["methodology_components"]
    assert isinstance(payload.get("deliverables"), list) and payload["deliverables"]


def test_fallback_structured_toc_supports_katch_evaluation_rfq_profile():
    payload, engine = _fallback_structured_toc(
        evaluation_rfq_schema(),
        donor_id="un_agencies",
        project="KATCH Project Performance Evaluation",
        country="Kazakhstan",
        revision_hint="",
        evidence_hits=[],
        input_context={
            "proposal_mode": "evaluation_rfq",
            "rfq_profile": "katch_final_assessment",
        },
    )

    assert engine == "evaluation_rfq_contract_synthesizer"
    assert payload["rfq_profile"] == "katch_final_assessment"
    assert payload["organization_information"]
    assert payload["technical_approach_summary"]
    assert payload["sampling_plan"]
    assert payload["level_of_effort_summary"]
    assert payload["technical_experience_summary"]
    assert payload["sample_outputs_summary"]
    assert payload["annex_readiness"]
    assert isinstance(payload.get("compliance_matrix"), list)
    assert len(payload["compliance_matrix"]) >= 5
    assert payload["compliance_matrix"][0]["requirement"]
    assert payload["compliance_matrix"][0]["response_section"]
    assert payload["compliance_matrix"][0]["evidence"]
    assert isinstance(payload.get("key_personnel"), list)
    assert len(payload["key_personnel"]) >= 2
    assert payload["key_personnel"][0]["name"]
    assert payload["key_personnel"][0]["cv_status"]
    assert payload["financial_summary"]
    assert isinstance(payload.get("cost_structure"), list)
    assert len(payload["cost_structure"]) >= 3
    assert payload["cost_structure"][0]["cost_bucket"]
    assert isinstance(payload.get("pricing_assumptions"), list)
    assert len(payload["pricing_assumptions"]) >= 2
    assert payload["payment_schedule_summary"]
    assert isinstance(payload.get("submission_package_checklist"), list)
    assert len(payload["submission_package_checklist"]) >= 5
    assert payload["submission_package_checklist"][0]["artifact"]
    assert payload["submission_package_checklist"][0]["owner"]
    assert payload["submission_package_checklist"][0]["status"]
    assert isinstance(payload.get("attachment_manifest"), list)
    assert len(payload["attachment_manifest"]) >= 5
    assert payload["attachment_manifest"][0]["attachment"]
    assert payload["attachment_manifest"][0]["required_for"]
    assert payload["attachment_manifest"][0]["owner"]
    assert isinstance(payload.get("evaluation_questions_matrix"), list)
    assert len(payload["evaluation_questions_matrix"]) >= 2
    assert payload["evaluation_questions_matrix"][0]["evaluation_question"]
    assert payload["evaluation_questions_matrix"][0]["key_methods"]
    assert payload["evaluation_questions_matrix"][0]["evidence_sources"]
    assert payload["evaluation_questions_matrix"][0]["reporting_use"]
    assert isinstance(payload.get("methods_coverage_matrix"), list)
    assert len(payload["methods_coverage_matrix"]) >= 3
    assert payload["methods_coverage_matrix"][0]["method"]
    assert payload["methods_coverage_matrix"][0]["covers_questions"]
    assert payload["methods_coverage_matrix"][0]["respondent_group"]
    assert payload["methods_coverage_matrix"][0]["expected_output"]
    assert isinstance(payload.get("deliverables_schedule_table"), list)
    assert len(payload["deliverables_schedule_table"]) >= 3
    assert payload["deliverables_schedule_table"][0]["deliverable"]
    assert payload["deliverables_schedule_table"][0]["due_window"]
    assert payload["deliverables_schedule_table"][0]["owner"]
    assert payload["deliverables_schedule_table"][0]["dependencies"]
    assert payload["deliverables_schedule_table"][0]["review_gate"]


def test_katch_evaluation_rfq_backfills_required_deliverables_from_partial_input():
    payload, engine = _fallback_structured_toc(
        evaluation_rfq_schema(),
        donor_id="un_agencies",
        project="KATCH Project Performance Evaluation",
        country="Kyrgyzstan",
        revision_hint="",
        evidence_hits=[],
        input_context={
            "proposal_mode": "evaluation_rfq",
            "rfq_profile": "katch_final_assessment",
            "deliverables": [
                "Inception Report",
                "Draft Evaluation Report",
                "Final Evaluation Report",
            ],
        },
    )

    assert engine == "evaluation_rfq_contract_synthesizer"
    titles = [str(row.get("deliverable") or "") for row in payload.get("deliverables", [])]
    rendered = " | ".join(titles).lower()
    assert "inception" in rendered
    assert "bi-week" in rendered
    assert "workshop" in rendered
    assert "draft evaluation report" in rendered
    assert "stand-alone brief" in rendered
    assert "final evaluation report" in rendered
