from __future__ import annotations

from copy import deepcopy
from typing import Any

_DEMO_GENERATE_PRESETS_LEGACY: dict[str, dict[str, Any]] = {
    "usaid_gov_ai_kazakhstan": {
        "donor_id": "usaid",
        "title": "AI civil service (KZ)",
        "generate_payload": {
            "donor_id": "usaid",
            "input_context": {
                "project": "Responsible AI Skills for Civil Service Modernization",
                "country": "Kazakhstan",
                "region": "National with pilot cohorts in Astana and Almaty",
                "timeframe": "2026-2027 (24 months)",
                "problem": (
                    "Civil servants have uneven practical skills in safe, ethical, and effective AI use for "
                    "public administration."
                ),
                "target_population": (
                    "Mid-level and senior civil servants in policy, service delivery, and digital transformation units."
                ),
                "expected_change": (
                    "Agencies improve AI readiness, adopt governance guidance, and demonstrate early workflow "
                    "efficiency gains."
                ),
                "key_activities": [
                    "Needs assessment and baseline competency mapping",
                    "Responsible AI curriculum design for public administration",
                    "Cohort-based training and training-of-trainers",
                    "Applied labs for policy and service workflows",
                    "SOP and governance guidance drafting support",
                ],
            },
            "llm_mode": True,
            "hitl_enabled": True,
            "architect_rag_enabled": True,
            "strict_preflight": False,
        },
    },
    "eu_digital_governance_moldova": {
        "donor_id": "eu",
        "title": "Digital governance (MD)",
        "generate_payload": {
            "donor_id": "eu",
            "input_context": {
                "project": "Digital Governance Service Quality and Administrative Capacity",
                "country": "Moldova",
                "region": "National and selected municipalities",
                "timeframe": "2026-2028 (30 months)",
                "problem": "Public institutions face uneven digital service management capacity and inconsistent service quality.",
                "target_population": (
                    "Civil servants and municipal service managers in digital transformation and service delivery units."
                ),
                "expected_change": "Institutions adopt stronger service quality procedures and improve processing efficiency.",
                "key_activities": [
                    "Institutional workflow assessments",
                    "Training on service design and process improvement",
                    "Coaching for agency and municipal teams",
                    "Support for SOPs and service quality dashboards",
                ],
            },
            "llm_mode": True,
            "hitl_enabled": True,
            "architect_rag_enabled": True,
            "strict_preflight": False,
        },
    },
    "worldbank_public_sector_uzbekistan": {
        "donor_id": "worldbank",
        "title": "Public sector performance (UZ)",
        "generate_payload": {
            "donor_id": "worldbank",
            "input_context": {
                "project": "Public Sector Performance and Service Delivery Capacity Strengthening",
                "country": "Uzbekistan",
                "region": "National ministries and selected subnational administrations",
                "timeframe": "2026-2028 (36 months)",
                "problem": (
                    "Public agencies have uneven capabilities in performance management and evidence-based decision-making."
                ),
                "target_population": "Government managers and civil servants in reform, performance, and service delivery functions.",
                "expected_change": (
                    "Participating institutions adopt stronger performance management practices and improve selected services."
                ),
                "key_activities": [
                    "Institutional diagnostics and process mapping",
                    "Capacity development for performance management and data use",
                    "Technical assistance for service improvement plans",
                    "Process optimization pilots and adaptive reviews",
                ],
            },
            "llm_mode": True,
            "hitl_enabled": True,
            "architect_rag_enabled": True,
            "strict_preflight": False,
        },
    },
    "giz_sme_resilience_ukraine": {
        "donor_id": "giz",
        "title": "SME resilience (UA)",
        "generate_payload": {
            "donor_id": "giz",
            "input_context": {
                "project": "SME Resilience and Local Jobs",
                "country": "Ukraine",
                "region": "Selected municipalities and SME support ecosystems",
                "timeframe": "2026-2028 (30 months)",
                "problem": (
                    "Small and medium enterprises face disrupted operations, weak continuity planning, "
                    "and uneven access to practical resilience support."
                ),
                "target_population": (
                    "SMEs, local business support providers, and municipal economic development partners."
                ),
                "expected_change": (
                    "Participating SMEs adopt stronger resilience practices, stabilize operations, and retain jobs."
                ),
                "key_activities": [
                    "SME resilience diagnostics and prioritization",
                    "Business continuity coaching and peer learning",
                    "Support for local partner implementation routines",
                    "Resilience action plans and follow-up implementation reviews",
                ],
            },
            "llm_mode": True,
            "hitl_enabled": True,
            "architect_rag_enabled": True,
            "strict_preflight": False,
        },
    },
    "state_department_media_georgia": {
        "donor_id": "state_department",
        "title": "Media resilience (GE)",
        "generate_payload": {
            "donor_id": "state_department",
            "input_context": {
                "project": "Independent Media Resilience",
                "country": "Georgia",
                "region": "National and selected local media ecosystems",
                "timeframe": "2026-2028 (24 months)",
                "problem": (
                    "Independent media organizations face political pressure, information-space threats, "
                    "and uneven capacity to manage operational and safeguarding risks."
                ),
                "target_population": (
                    "Independent media outlets, editors, journalists, and partner civil society support organizations."
                ),
                "expected_change": (
                    "Media partners improve resilience practices, protect editorial operations, and respond more "
                    "consistently to information integrity risks."
                ),
                "key_activities": [
                    "Partner risk and resilience assessments",
                    "Editorial and organizational resilience coaching",
                    "Safeguarding and contingency planning support",
                    "Peer exchange and monitoring of information integrity responses",
                ],
            },
            "llm_mode": True,
            "hitl_enabled": True,
            "architect_rag_enabled": True,
            "strict_preflight": False,
        },
    },
    "un_agencies_education_nepal": {
        "donor_id": "un_agencies",
        "title": "Education recovery (NP)",
        "generate_payload": {
            "donor_id": "un_agencies",
            "input_context": {
                "project": "Inclusive Education Recovery",
                "country": "Nepal",
                "region": "Selected provinces and vulnerable school communities",
                "timeframe": "2026-2028 (24 months)",
                "problem": (
                    "Learners in crisis-affected communities face uneven access to inclusive education recovery "
                    "support and inconsistent service delivery across local institutions."
                ),
                "target_population": (
                    "Children, teachers, school leaders, and partner education institutions in vulnerable communities."
                ),
                "expected_change": (
                    "Partner institutions deliver more reliable inclusive education recovery support and improve "
                    "continuity for affected learners."
                ),
                "key_activities": [
                    "Partner education needs assessments",
                    "Teacher and school support packages",
                    "Inclusive education recovery planning with local institutions",
                    "Field monitoring and partner verification reviews",
                ],
            },
            "llm_mode": True,
            "hitl_enabled": True,
            "architect_rag_enabled": True,
            "strict_preflight": False,
        },
    },
    "fcdo_governance_education_nigeria": {
        "donor_id": "fcdo",
        "title": "Education governance (NG)",
        "generate_payload": {
            "donor_id": "fcdo",
            "input_context": {
                "project": "Basic Education Quality and Governance in Northern Nigeria",
                "country": "Nigeria",
                "region": "Kano, Kaduna, and Sokoto states",
                "timeframe": "2026-2029 (36 months)",
                "problem": (
                    "Out-of-school rates remain high in northern Nigeria due to weak school governance, "
                    "low community accountability, and inadequate teacher support systems."
                ),
                "target_population": (
                    "Primary school children (with equity focus on girls and children with disabilities), "
                    "teachers, head teachers, and state education authorities."
                ),
                "expected_change": (
                    "State education systems adopt stronger accountability mechanisms, teacher support routines, "
                    "and community engagement that improve learning access and retention."
                ),
                "key_activities": [
                    "State-level education governance and accountability diagnostics",
                    "School management committee strengthening and community engagement",
                    "Teacher coaching and instructional support system improvement",
                    "Girls' access and safeguarding integration into school management",
                    "Logframe and VfM monitoring with OECD DAC criteria tracking",
                ],
            },
            "llm_mode": True,
            "hitl_enabled": True,
            "architect_rag_enabled": True,
            "strict_preflight": False,
        },
    },
    "afd_climate_urban_senegal": {
        "donor_id": "afd",
        "title": "Climate urban WASH (SN)",
        "generate_payload": {
            "donor_id": "afd",
            "input_context": {
                "project": "Climate-Resilient Urban Water and Sanitation in Greater Dakar",
                "country": "Senegal",
                "region": "Dakar metropolitan area and peri-urban communes",
                "timeframe": "2026-2030 (48 months)",
                "problem": (
                    "Rapid urbanization and climate variability threaten water supply reliability and sanitation "
                    "coverage in Dakar's peri-urban areas, with disproportionate impacts on women and low-income households."
                ),
                "target_population": (
                    "Peri-urban households, women heads of household, ONAS and SDE service staff, "
                    "and municipal authorities in target communes."
                ),
                "expected_change": (
                    "Target communes improve climate-resilient water access and sanitation coverage, "
                    "with gender-equitable service delivery and stronger utility management capacity."
                ),
                "key_activities": [
                    "Climate risk and infrastructure vulnerability assessment",
                    "Water network rehabilitation and climate-proofing investments",
                    "Sanitation facility construction with gender-sensitive design",
                    "Community hygiene promotion and behaviour change communication",
                    "Utility capacity building and gender action plan implementation",
                ],
            },
            "llm_mode": True,
            "hitl_enabled": True,
            "architect_rag_enabled": True,
            "strict_preflight": False,
        },
    },
    "jica_infrastructure_water_vietnam": {
        "donor_id": "jica",
        "title": "Rural WASH infrastructure (VN)",
        "generate_payload": {
            "donor_id": "jica",
            "input_context": {
                "project": "Rural Water Supply and Sanitation Infrastructure Improvement in the Mekong Delta",
                "country": "Vietnam",
                "region": "Mekong Delta provinces (An Giang, Dong Thap, Kien Giang)",
                "timeframe": "2026-2030 (48 months)",
                "problem": (
                    "Rural communities in the Mekong Delta face deteriorating water supply infrastructure, "
                    "increasing salinity intrusion, and low access to adequate sanitation facilities."
                ),
                "target_population": (
                    "Rural households in target provinces, provincial and district water supply management centres, "
                    "and commune-level water user groups."
                ),
                "expected_change": (
                    "Target communities access reliable, safe, and climate-resilient water supply and improved "
                    "sanitation, with strengthened local management capacity."
                ),
                "key_activities": [
                    "Infrastructure baseline survey and detailed design",
                    "Water supply system rehabilitation and salinity-resistant upgrades",
                    "Sanitation facility construction and hygiene promotion",
                    "Counterpart capacity building for O&M and water quality monitoring",
                    "Technology transfer and Japanese expertise integration",
                ],
            },
            "llm_mode": True,
            "hitl_enabled": True,
            "architect_rag_enabled": True,
            "strict_preflight": False,
        },
    },
    "adb_climate_infrastructure_bangladesh": {
        "donor_id": "adb",
        "title": "Coastal climate resilience (BD)",
        "generate_payload": {
            "donor_id": "adb",
            "input_context": {
                "project": "Coastal Polder Rehabilitation and Climate Resilience Project",
                "country": "Bangladesh",
                "region": "Coastal polders in Khulna, Bagerhat, and Satkhira districts",
                "timeframe": "2026-2031 (60 months)",
                "problem": (
                    "Coastal embankments and drainage infrastructure in southwestern Bangladesh are severely "
                    "degraded, exposing 2 million people to flood and cyclone risks intensified by sea-level rise."
                ),
                "target_population": (
                    "Coastal households in target polders, women farmers and fish pond operators, "
                    "Bangladesh Water Development Board (BWDB), and community water management committees."
                ),
                "expected_change": (
                    "Rehabilitated coastal infrastructure provides sustained flood and cyclone protection, "
                    "community water management systems are functional, and women participate equitably in "
                    "governance and economic recovery."
                ),
                "key_activities": [
                    "Polder embankment and sluice gate rehabilitation to climate-resilient standards",
                    "Community-based water management committee establishment and capacity building",
                    "Early warning system integration and disaster response planning",
                    "Gender action plan implementation with women's economic inclusion activities",
                    "Environmental management plan and resettlement framework compliance",
                ],
            },
            "llm_mode": True,
            "hitl_enabled": True,
            "architect_rag_enabled": True,
            "strict_preflight": False,
        },
    },
    "un_agencies_katch_evaluation_kyrgyzstan": {
        "donor_id": "un_agencies",
        "title": "KATCH evaluation RFQ",
        "generate_payload": {
            "donor_id": "un_agencies",
            "input_context": {
                "proposal_mode": "evaluation_rfq",
                "rfq_profile": "katch_final_assessment",
                "project": "KATCH Project Performance Evaluation",
                "country": "Kyrgyzstan",
                "region": "Selected project locations and stakeholder groups",
                "background": (
                    "The assignment requests an external project performance evaluation with mixed methods, "
                    "stakeholder validation, and formal reporting deliverables."
                ),
                "evaluation_purpose": (
                    "Assess project performance, capture outcome-level change, and generate practical "
                    "recommendations for follow-on programming."
                ),
                "problem": (
                    "The client needs a reviewable technical proposal that translates RFQ instructions into a "
                    "coherent evaluation design, work plan, and reporting package."
                ),
                "methods": [
                    "Outcome Harvesting",
                    "Social Media Analysis",
                    "Key Informant Interviews",
                    "Focus Group Discussions",
                    "Survey of Beneficiaries",
                ],
                "deliverables": [
                    "Draft Inception Report with Evaluation Design and Work Plan",
                    "Final Inception Report with Evaluation Design and Work Plan",
                    "Bi-Weekly Updates",
                    "Report on field survey data collection",
                    "Virtual Event/Workshop",
                    "Draft Evaluation Report",
                    "Stand-alone Brief",
                    "Final Evaluation Report",
                ],
                "evaluation_questions": [
                    "What outcome-level changes can be substantiated through the project evidence base?",
                    "Which implementation factors most influenced performance and beneficiary reach?",
                    "What actionable recommendations should guide future programming and partner decisions?",
                ],
            },
            "llm_mode": True,
            "hitl_enabled": True,
            "architect_rag_enabled": True,
            "strict_preflight": False,
        },
    },
}

_DEMO_INGEST_PRESETS: dict[str, dict[str, Any]] = {
    "usaid_gov_ai_kazakhstan": {
        "donor_id": "usaid",
        "title": "AI civil service (KZ)",
        "metadata": {
            "source_type": "donor_guidance",
            "sector": "governance",
            "theme": "responsible_ai_public_sector",
            "country_focus": "Kazakhstan",
            "doc_family": "donor_policy",
        },
        "checklist_items": [
            {"id": "donor_policy", "label": "USAID donor policy / ADS guidance", "source_type": "donor_guidance"},
            {
                "id": "responsible_ai_guidance",
                "label": "Responsible AI / digital governance guidance",
                "source_type": "reference_guidance",
            },
            {
                "id": "country_context",
                "label": "Kazakhstan public administration / digital government context",
                "source_type": "country_context",
            },
            {
                "id": "competency_framework",
                "label": "Civil service competency / training framework",
                "source_type": "training_framework",
            },
        ],
        "recommended_docs": [
            "USAID ADS / policy guidance relevant to digital transformation, governance, or capacity strengthening",
            "Responsible AI / digital governance guidance approved for your organization",
            "Kazakhstan public administration or digital government policy/context documents",
            "Civil service training standards / competency frameworks (if available)",
        ],
    },
    "eu_digital_governance_moldova": {
        "donor_id": "eu",
        "title": "Digital governance (MD)",
        "metadata": {
            "source_type": "donor_guidance",
            "sector": "governance",
            "theme": "digital_service_delivery",
            "country_focus": "Moldova",
            "doc_family": "donor_results_guidance",
        },
        "checklist_items": [
            {
                "id": "donor_results_guidance",
                "label": "EU intervention logic / results framework guidance",
                "source_type": "donor_guidance",
            },
            {
                "id": "digital_governance_guidance",
                "label": "EU digital governance / service delivery references",
                "source_type": "reference_guidance",
            },
            {
                "id": "country_context",
                "label": "Moldova digitization policy / service standards",
                "source_type": "country_context",
            },
            {
                "id": "municipal_process_guidance",
                "label": "Municipal service process / quality guidance",
                "source_type": "implementation_reference",
            },
        ],
        "recommended_docs": [
            "EU intervention logic / results framework guidance relevant to governance and public administration reform",
            "EU digital governance or service delivery reform policy references",
            "Moldova public service digitization strategies / standards",
            "Municipal service quality standards or process management guidance",
        ],
    },
    "worldbank_public_sector_uzbekistan": {
        "donor_id": "worldbank",
        "title": "Public sector performance (UZ)",
        "metadata": {
            "source_type": "donor_guidance",
            "sector": "public_sector_reform",
            "theme": "performance_management_service_delivery",
            "country_focus": "Uzbekistan",
            "doc_family": "donor_results_guidance",
        },
        "checklist_items": [
            {"id": "donor_results_guidance", "label": "World Bank RF / M&E guidance", "source_type": "donor_guidance"},
            {
                "id": "project_reference_docs",
                "label": "World Bank public sector modernization project references",
                "source_type": "reference_guidance",
            },
            {
                "id": "country_context",
                "label": "Uzbekistan public administration reform context",
                "source_type": "country_context",
            },
            {
                "id": "agency_process_docs",
                "label": "Agency service standards / process maps",
                "source_type": "implementation_reference",
            },
        ],
        "recommended_docs": [
            "World Bank results framework / M&E guidance relevant to governance or public sector reform",
            "World Bank public sector modernization / service delivery project documents",
            "Uzbekistan public administration reform strategies / performance frameworks",
            "Agency service standards, process maps, or reform guidance used for pilots",
        ],
    },
    "giz_sme_resilience_ukraine": {
        "donor_id": "giz",
        "title": "SME resilience (UA)",
        "metadata": {
            "source_type": "donor_guidance",
            "sector": "private_sector_development",
            "theme": "sme_resilience_and_jobs",
            "country_focus": "Ukraine",
            "doc_family": "donor_results_guidance",
        },
        "checklist_items": [
            {
                "id": "donor_results_guidance",
                "label": "GIZ results and sustainability guidance",
                "source_type": "donor_guidance",
            },
            {
                "id": "sme_resilience_guidance",
                "label": "SME resilience / continuity support references",
                "source_type": "reference_guidance",
            },
            {
                "id": "country_context",
                "label": "Ukraine SME recovery / economic resilience context",
                "source_type": "country_context",
            },
            {
                "id": "partner_delivery_docs",
                "label": "Partner implementation and review references",
                "source_type": "implementation_reference",
            },
        ],
        "recommended_docs": [
            "GIZ programme guidance on results, implementation, and sustainability",
            "SME resilience or business continuity support references used by implementing partners",
            "Ukraine SME recovery or local jobs context references",
            "Partner implementation review templates or delivery monitoring notes",
        ],
    },
    "state_department_media_georgia": {
        "donor_id": "state_department",
        "title": "Media resilience (GE)",
        "metadata": {
            "source_type": "donor_guidance",
            "sector": "media_and_information_integrity",
            "theme": "independent_media_resilience",
            "country_focus": "Georgia",
            "doc_family": "donor_results_guidance",
        },
        "checklist_items": [
            {
                "id": "donor_results_guidance",
                "label": "State Department program logic / DRG guidance",
                "source_type": "donor_guidance",
            },
            {
                "id": "media_resilience_guidance",
                "label": "Media resilience / information integrity references",
                "source_type": "reference_guidance",
            },
            {
                "id": "country_context",
                "label": "Georgia media environment / civic space context",
                "source_type": "country_context",
            },
            {
                "id": "partner_risk_docs",
                "label": "Partner safeguarding / contingency planning references",
                "source_type": "implementation_reference",
            },
        ],
        "recommended_docs": [
            "State Department DRG / public affairs guidance relevant to media resilience",
            "Information integrity or independent media support references",
            "Georgia media environment / civic space context references",
            "Partner safeguarding, contingency planning, or risk review templates",
        ],
    },
    "fcdo_governance_education_nigeria": {
        "donor_id": "fcdo",
        "title": "Education governance (NG)",
        "metadata": {
            "source_type": "donor_guidance",
            "sector": "education_governance",
            "theme": "basic_education_accountability",
            "country_focus": "Nigeria",
            "doc_family": "donor_results_guidance",
        },
        "checklist_items": [
            {
                "id": "donor_results_guidance",
                "label": "FCDO Smart Rules / logframe guidance",
                "source_type": "donor_guidance",
            },
            {
                "id": "vfm_guidance",
                "label": "FCDO Value for Money (4Es) references",
                "source_type": "reference_guidance",
            },
            {
                "id": "country_context",
                "label": "Nigeria education / governance context",
                "source_type": "country_context",
            },
            {
                "id": "mel_safeguarding_docs",
                "label": "FCDO MEL standards and safeguarding references",
                "source_type": "mel_safeguarding_guidance",
            },
        ],
        "recommended_docs": [
            "FCDO Smart Rules and logframe guidance relevant to education or governance",
            "FCDO Value for Money (4Es) guidance and OECD DAC criteria references",
            "Nigeria education system or governance context documents",
            "FCDO MEL, safeguarding, and PSEAH policy references",
        ],
    },
    "afd_climate_urban_senegal": {
        "donor_id": "afd",
        "title": "Climate urban WASH (SN)",
        "metadata": {
            "source_type": "donor_guidance",
            "sector": "urban_wash",
            "theme": "climate_resilient_water_sanitation",
            "country_focus": "Senegal",
            "doc_family": "donor_results_guidance",
        },
        "checklist_items": [
            {
                "id": "donor_results_guidance",
                "label": "AFD cadre logique / results framework guidance",
                "source_type": "donor_guidance",
            },
            {
                "id": "climate_finance_guidance",
                "label": "AFD climate marker and co-benefit documentation",
                "source_type": "reference_guidance",
            },
            {
                "id": "country_context",
                "label": "Senegal urban / climate development context",
                "source_type": "country_context",
            },
            {
                "id": "sustainability_docs",
                "label": "AFD sustainability and gender standards references",
                "source_type": "sustainability_climate_finance",
            },
        ],
        "recommended_docs": [
            "AFD cadre logique and results framework guidance",
            "AFD climate co-benefit marker and Rio Markers documentation",
            "Senegal water sector or urban development context references",
            "AFD gender marker and sustainability standards references",
        ],
    },
    "jica_infrastructure_water_vietnam": {
        "donor_id": "jica",
        "title": "Rural WASH infrastructure (VN)",
        "metadata": {
            "source_type": "donor_guidance",
            "sector": "water_sanitation",
            "theme": "rural_water_supply_infrastructure",
            "country_focus": "Vietnam",
            "doc_family": "donor_results_guidance",
        },
        "checklist_items": [
            {
                "id": "donor_results_guidance",
                "label": "JICA PCM / PDM methodology guidance",
                "source_type": "donor_guidance",
            },
            {
                "id": "infrastructure_reference",
                "label": "JICA water infrastructure Vietnam reference",
                "source_type": "implementation_reference",
            },
            {
                "id": "country_context",
                "label": "Vietnam infrastructure / Mekong Delta development context",
                "source_type": "country_context",
            },
            {
                "id": "quality_sustainability_docs",
                "label": "JICA quality standards and sustainability references",
                "source_type": "quality_sustainability_guidance",
            },
        ],
        "recommended_docs": [
            "JICA PCM and PDM methodology guidance",
            "JICA water supply or rural infrastructure project references for Vietnam",
            "Vietnam Mekong Delta development or water resources context documents",
            "JICA quality standards, O&M sustainability, and technology transfer references",
        ],
    },
    "adb_climate_infrastructure_bangladesh": {
        "donor_id": "adb",
        "title": "Coastal climate resilience (BD)",
        "metadata": {
            "source_type": "donor_guidance",
            "sector": "climate_infrastructure",
            "theme": "coastal_polder_rehabilitation",
            "country_focus": "Bangladesh",
            "doc_family": "donor_results_guidance",
        },
        "checklist_items": [
            {
                "id": "donor_results_guidance",
                "label": "ADB DMF / Strategy 2030 results guidance",
                "source_type": "donor_guidance",
            },
            {
                "id": "climate_infrastructure_reference",
                "label": "ADB Bangladesh climate infrastructure reference",
                "source_type": "implementation_reference",
            },
            {
                "id": "country_context",
                "label": "Bangladesh climate and development context",
                "source_type": "country_context",
            },
            {
                "id": "safeguard_gender_docs",
                "label": "ADB safeguard policy and EGM gender standards",
                "source_type": "safeguard_gender_standards",
            },
        ],
        "recommended_docs": [
            "ADB DMF guidance and Strategy 2030 Operational Priorities documentation",
            "ADB Bangladesh climate infrastructure or polder rehabilitation project references",
            "Bangladesh Delta Plan 2100 or climate vulnerability context documents",
            "ADB Safeguard Policy Statement and EGM gender action plan guidance",
        ],
    },
    "un_agencies_education_nepal": {
        "donor_id": "un_agencies",
        "title": "Education recovery (NP)",
        "metadata": {
            "source_type": "donor_guidance",
            "sector": "education",
            "theme": "inclusive_education_recovery",
            "country_focus": "Nepal",
            "doc_family": "donor_results_guidance",
        },
        "checklist_items": [
            {
                "id": "donor_results_guidance",
                "label": "UN results framework / reporting guidance",
                "source_type": "donor_guidance",
            },
            {
                "id": "education_recovery_guidance",
                "label": "Inclusive education recovery references",
                "source_type": "reference_guidance",
            },
            {
                "id": "country_context",
                "label": "Nepal education recovery / access context",
                "source_type": "country_context",
            },
            {
                "id": "partner_verification_docs",
                "label": "Partner monitoring / field verification references",
                "source_type": "implementation_reference",
            },
        ],
        "recommended_docs": [
            "UN results framework / reporting guidance relevant to education recovery",
            "Inclusive education recovery references used by implementing partners",
            "Nepal education access or recovery context references",
            "Partner monitoring, field verification, or sector-review templates",
        ],
    },
}


def list_generate_legacy_preset_summaries() -> list[dict[str, Any]]:
    rows: list[dict[str, Any]] = []
    for preset_key in sorted(_DEMO_GENERATE_PRESETS_LEGACY.keys()):
        preset = _DEMO_GENERATE_PRESETS_LEGACY[preset_key]
        rows.append(
            {
                "preset_key": preset_key,
                "donor_id": str(preset.get("donor_id") or "").strip().lower() or None,
                "title": str(preset.get("title") or "").strip() or None,
            }
        )
    return rows


def load_generate_legacy_preset(preset_key: str) -> dict[str, Any]:
    token = str(preset_key or "").strip()
    if not token:
        raise ValueError("Missing preset_key")
    if token not in _DEMO_GENERATE_PRESETS_LEGACY:
        known = ", ".join(sorted(_DEMO_GENERATE_PRESETS_LEGACY.keys()))
        raise ValueError(f"Unknown preset_key '{token}'. Available: {known}")
    payload = deepcopy(_DEMO_GENERATE_PRESETS_LEGACY[token])
    payload["preset_key"] = token
    return payload


def list_generate_legacy_preset_details() -> list[dict[str, Any]]:
    rows: list[dict[str, Any]] = []
    for item in list_generate_legacy_preset_summaries():
        key = str(item.get("preset_key") or "").strip()
        if not key:
            continue
        rows.append(load_generate_legacy_preset(key))
    return rows


def list_ingest_preset_summaries() -> list[dict[str, Any]]:
    rows: list[dict[str, Any]] = []
    for preset_key in sorted(_DEMO_INGEST_PRESETS.keys()):
        preset = _DEMO_INGEST_PRESETS[preset_key]
        rows.append(
            {
                "preset_key": preset_key,
                "donor_id": str(preset.get("donor_id") or "").strip().lower() or None,
                "title": str(preset.get("title") or "").strip() or None,
            }
        )
    return rows


def load_ingest_preset(preset_key: str) -> dict[str, Any]:
    token = str(preset_key or "").strip()
    if not token:
        raise ValueError("Missing preset_key")
    if token not in _DEMO_INGEST_PRESETS:
        known = ", ".join(sorted(_DEMO_INGEST_PRESETS.keys()))
        raise ValueError(f"Unknown preset_key '{token}'. Available: {known}")
    payload = deepcopy(_DEMO_INGEST_PRESETS[token])
    payload["preset_key"] = token
    return payload


def list_ingest_preset_details() -> list[dict[str, Any]]:
    rows: list[dict[str, Any]] = []
    for item in list_ingest_preset_summaries():
        key = str(item.get("preset_key") or "").strip()
        if not key:
            continue
        rows.append(load_ingest_preset(key))
    return rows
