from __future__ import annotations

import pytest

from grantflow.api.demo_presets import (
    list_generate_legacy_preset_summaries,
    list_generate_legacy_preset_details,
    list_ingest_preset_details,
    list_ingest_preset_summaries,
    load_generate_legacy_preset,
    load_ingest_preset,
)


def test_list_generate_legacy_preset_summaries_contains_expected_keys():
    rows = list_generate_legacy_preset_summaries()
    assert isinstance(rows, list) and rows
    keys = {str(item.get("preset_key") or "") for item in rows if isinstance(item, dict)}
    assert "usaid_gov_ai_kazakhstan" in keys
    assert "eu_digital_governance_moldova" in keys
    assert "worldbank_public_sector_uzbekistan" in keys


def test_load_generate_legacy_preset_returns_expected_structure():
    payload = load_generate_legacy_preset("usaid_gov_ai_kazakhstan")
    assert payload["preset_key"] == "usaid_gov_ai_kazakhstan"
    assert payload["donor_id"] == "usaid"
    generate_payload = payload.get("generate_payload")
    assert isinstance(generate_payload, dict)
    input_context = generate_payload.get("input_context")
    assert isinstance(input_context, dict)
    assert str(input_context.get("project") or "").strip() != ""
    assert str(input_context.get("country") or "").strip().lower() == "kazakhstan"


def test_load_generate_legacy_preset_raises_for_unknown_key():
    with pytest.raises(ValueError, match="Unknown preset_key"):
        load_generate_legacy_preset("missing-generate-preset")


def test_list_generate_legacy_preset_details_contains_payloads():
    rows = list_generate_legacy_preset_details()
    assert isinstance(rows, list) and rows
    first = rows[0]
    assert isinstance(first.get("generate_payload"), dict)


def test_list_ingest_preset_summaries_contains_expected_keys():
    rows = list_ingest_preset_summaries()
    assert isinstance(rows, list) and rows
    keys = {str(item.get("preset_key") or "") for item in rows if isinstance(item, dict)}
    assert "usaid_gov_ai_kazakhstan" in keys
    assert "eu_digital_governance_moldova" in keys
    assert "worldbank_public_sector_uzbekistan" in keys


def test_load_ingest_preset_returns_expected_structure():
    payload = load_ingest_preset("eu_digital_governance_moldova")
    assert payload["preset_key"] == "eu_digital_governance_moldova"
    assert payload["donor_id"] == "eu"
    assert isinstance(payload.get("metadata"), dict)
    assert isinstance(payload.get("checklist_items"), list)
    assert isinstance(payload.get("recommended_docs"), list)


def test_load_ingest_preset_raises_for_unknown_key():
    with pytest.raises(ValueError, match="Unknown preset_key"):
        load_ingest_preset("missing-preset")


def test_list_ingest_preset_details_contains_payloads():
    rows = list_ingest_preset_details()
    assert isinstance(rows, list) and rows
    first = rows[0]
    assert isinstance(first.get("metadata"), dict)
    assert isinstance(first.get("checklist_items"), list)


# --- New donor presets ---


@pytest.mark.parametrize(
    "preset_key,donor_id,country",
    [
        ("fcdo_governance_education_nigeria", "fcdo", "nigeria"),
        ("afd_climate_urban_senegal", "afd", "senegal"),
        ("jica_infrastructure_water_vietnam", "jica", "vietnam"),
        ("adb_climate_infrastructure_bangladesh", "adb", "bangladesh"),
    ],
)
def test_new_donor_generate_presets_structure(preset_key, donor_id, country):
    payload = load_generate_legacy_preset(preset_key)
    assert payload["preset_key"] == preset_key
    assert payload["donor_id"] == donor_id
    ctx = payload["generate_payload"]["input_context"]
    assert str(ctx.get("country", "")).lower() == country
    assert str(ctx.get("project", "")).strip() != ""
    assert isinstance(ctx.get("key_activities"), list)
    assert len(ctx["key_activities"]) >= 3


@pytest.mark.parametrize(
    "preset_key,donor_id",
    [
        ("fcdo_governance_education_nigeria", "fcdo"),
        ("afd_climate_urban_senegal", "afd"),
        ("jica_infrastructure_water_vietnam", "jica"),
        ("adb_climate_infrastructure_bangladesh", "adb"),
    ],
)
def test_new_donor_ingest_presets_structure(preset_key, donor_id):
    payload = load_ingest_preset(preset_key)
    assert payload["preset_key"] == preset_key
    assert payload["donor_id"] == donor_id
    assert isinstance(payload.get("metadata"), dict)
    assert isinstance(payload.get("checklist_items"), list)
    assert len(payload["checklist_items"]) >= 3
    assert isinstance(payload.get("recommended_docs"), list)


def test_all_new_donors_present_in_generate_summary():
    keys = {p["preset_key"] for p in list_generate_legacy_preset_summaries()}
    for expected in [
        "fcdo_governance_education_nigeria",
        "afd_climate_urban_senegal",
        "jica_infrastructure_water_vietnam",
        "adb_climate_infrastructure_bangladesh",
    ]:
        assert expected in keys


def test_all_new_donors_present_in_ingest_summary():
    keys = {p["preset_key"] for p in list_ingest_preset_summaries()}
    for expected in [
        "fcdo_governance_education_nigeria",
        "afd_climate_urban_senegal",
        "jica_infrastructure_water_vietnam",
        "adb_climate_infrastructure_bangladesh",
    ]:
        assert expected in keys
