from typing import Any

from fastapi.testclient import TestClient


def base_logframe_draft() -> dict[str, Any]:
    return {
        "proposal_mode": "evaluation_rfq",
        "indicators": [
            {
                "indicator_id": "IND_001",
                "name": "Deliverable milestone: Inception Report",
                "result_level": "output",
                "baseline": "0 deliverables",
                "target": "1 deliverable",
                "frequency": "bi-weekly",
                "formula": "Count of required deliverables completed and accepted against the agreed evaluation work plan",
                "definition": "Tracks whether the inception package is completed with reviewer-ready documentation.",
                "justification": "Maps the deliverable schedule into an evaluation RFQ management indicator.",
                "means_of_verification": "Accepted deliverable package and QA checklist",
                "owner": "Evaluation team lead and operations coordinator",
            }
        ],
    }


def base_toc_draft(*, source_path: str | None = None) -> dict[str, Any]:
    attachment = {
        "attachment": "Registration certificate",
        "required_for": "Organization information and legal-status package",
        "owner": "Operations / compliance",
        "status": "ready",
        "notes": "Attach current registration evidence in PDF format.",
    }
    if source_path is not None:
        attachment["source_path"] = source_path

    return {
        "proposal_mode": "evaluation_rfq",
        "rfq_profile": "katch_final_assessment",
        "toc": {
            "proposal_mode": "evaluation_rfq",
            "rfq_profile": "katch_final_assessment",
            "brief": "Final assessment technical response",
            "organization_information": ["Registered NGO with M&E and field delivery capacity."],
            "evaluation_purpose": ["Assess performance, outcomes, and learning questions."],
            "technical_approach_summary": ["Mixed-method final assessment with field verification."],
            "methodology_components": [
                {
                    "method": "Outcome harvesting",
                    "purpose": "Capture outcome evidence",
                    "evidence": "Stakeholder interviews",
                }
            ],
            "submission_package_checklist": [
                {
                    "artifact": "Technical proposal narrative",
                    "owner": "Proposal manager",
                    "status": "ready",
                    "notes": "Final reviewer-ready narrative package",
                }
            ],
            "attachment_manifest": [attachment],
            "compliance_matrix": [
                {
                    "requirement": "Organization information and legal status package",
                    "response_section": "Organization Information",
                    "evidence": "Registration certificate and audited financials",
                    "status": "ready",
                    "notes": "Attached in annex package",
                }
            ],
        },
    }


def katch_submission_kit_toc_draft() -> dict[str, Any]:
    toc_draft = base_toc_draft()
    toc_draft["toc"].update(
        {
            "brief": "KATCH final assessment technical proposal backbone",
            "organization_information": (
                "Legal Name: Example Evaluation Group. Authorized Contact: Jane Doe, Evaluation Director."
            ),
            "background_context": "KATCH is a performance evaluation assignment in Kazakhstan.",
            "evaluation_purpose": "Assess relevance, effectiveness, efficiency, sustainability, and outcomes of KATCH.",
            "evaluation_questions": ["How relevant was KATCH to beneficiary and stakeholder priorities?"],
            "technical_approach_summary": (
                "Theory-based, RBM-aligned mixed-method evaluation with decision-ready reporting."
            ),
            "methodology_components": [
                {
                    "method": "Outcome Harvesting",
                    "purpose": "Capture intended and unintended outcomes.",
                    "respondent_group": "Partners and stakeholders",
                    "evidence_source": "Validated interviews and project records",
                }
            ],
            "sampling_plan": "Purposive sampling across sampled southern Kazakhstan locations.",
            "ethical_considerations": ["Informed consent", "Confidentiality and anonymization"],
            "deliverables_schedule_table": [
                {
                    "deliverable": "Draft Inception Report",
                    "due_window": "Jul 14, 2025",
                    "owner": "Team Lead",
                    "dependencies": ["Kick-off", "Desk review"],
                    "review_gate": "Client review",
                }
            ],
            "key_personnel": [
                {
                    "name": "Proposed Team Lead",
                    "role": "Team Lead / Evaluation Specialist",
                    "qualifications": "Senior evaluation lead",
                    "level_of_effort": "Lead oversight across inception, fieldwork, and reporting",
                    "cv_status": "ready",
                }
            ],
            "technical_experience_summary": (
                "Relevant performance evaluations in migration, protection, and systems strengthening."
            ),
            "deliverables": [
                {
                    "deliverable": "Final Evaluation Report",
                    "timing": "Sep 30, 2025",
                    "purpose": "Final evidence-backed assessment and recommendations",
                }
            ],
            "cost_structure": [
                {
                    "cost_bucket": "Team Leader professional fee",
                    "basis": "person-day",
                    "estimate": "TBD",
                    "notes": "Final commercial rate to be inserted",
                }
            ],
            "financial_summary": (
                "The budget is based on the proposed level of effort and field coordination requirements."
            ),
            "pricing_assumptions": ["Firm fixed price, inclusive of taxes"],
            "evaluation_questions_matrix": [
                {
                    "evaluation_question": "How relevant was KATCH to stakeholder priorities?",
                    "key_methods": ["Outcome Harvesting"],
                    "evidence_sources": ["Project records"],
                    "reporting_use": "Supports relevance findings and recommendations",
                }
            ],
        }
    )
    return toc_draft


def export_both_zip(client: TestClient, toc_draft: dict[str, Any]):
    return client.post(
        "/export",
        json={
            "donor_id": "un_agencies",
            "toc_draft": toc_draft,
            "logframe_draft": base_logframe_draft(),
            "format": "both",
        },
    )
