"""Tests para build_guide_prompt."""

from build_guide_prompt import build_guide_prompt


def test_con_ubicacion_completa_pois_y_query():
    location = {
        "display_name": "Puerta del Sol, Madrid",
        "street": "Puerta del Sol",
        "neighbourhood": "Sol",
        "city": "Madrid",
        "country": "España",
    }
    pois = [
        {
            "name": "Museo del Prado",
            "category": "museum",
            "score": 1.0,
            "matched_interests": ["arte"],
            "lat": 40.41,
            "lon": -3.70,
            "tags": {},
        },
        {
            "name": "Jardines del Retiro",
            "category": "park",
            "score": 0.8,
            "matched_interests": ["naturaleza"],
            "lat": 40.41,
            "lon": -3.68,
            "tags": {},
        },
    ]
    interests = [
        {"name": "arte", "keywords": ["museo", "pintura"], "weight": 1.0},
        {"name": "naturaleza", "keywords": ["parque", "jardín"], "weight": 0.8},
    ]
    user_query = "¿Qué hay interesante por aquí?"

    messages = build_guide_prompt(location, pois, interests, user_query)

    assert len(messages) == 2
    assert messages[0]["role"] == "system"
    assert messages[1]["role"] == "user"

    system = messages[0]["content"]
    assert "Madrid" in system
    assert "arte" in system
    assert "naturaleza" in system

    user = messages[1]["content"]
    assert "Puerta del Sol, Madrid" in user
    assert "Sol" in user
    assert "Museo del Prado" in user
    assert "museum" in user
    assert "arte" in user
    assert "Jardines del Retiro" in user
    assert "¿Qué hay interesante por aquí?" in user


def test_sin_pois_solo_ubicacion():
    location = {
        "display_name": "Calle Mayor, Salamanca",
        "street": "Calle Mayor",
        "neighbourhood": "Centro Histórico",
        "city": "Salamanca",
        "country": "España",
    }
    pois = []
    interests = [{"name": "historia", "keywords": ["catedral"], "weight": 1.0}]
    user_query = "¿Algo interesante cerca?"

    messages = build_guide_prompt(location, pois, interests, user_query)

    assert len(messages) == 2
    user = messages[1]["content"]
    assert "Salamanca" in user
    assert "Lugares cercanos" not in user
    assert "¿Algo interesante cerca?" in user


def test_sin_query_usa_default():
    location = {
        "display_name": "Plaza Mayor, Sevilla",
        "city": "Sevilla",
        "neighbourhood": "Casco Antiguo",
        "country": "España",
    }
    pois = [
        {
            "name": "Catedral de Sevilla",
            "category": "cathedral",
            "score": 0.95,
            "matched_interests": ["historia", "arquitectura"],
            "lat": 37.38,
            "lon": -5.99,
            "tags": {},
        }
    ]
    interests = [
        {"name": "historia", "keywords": ["catedral"], "weight": 1.0},
        {"name": "arquitectura", "keywords": ["edificio"], "weight": 0.9},
    ]

    messages = build_guide_prompt(location, pois, interests)

    assert len(messages) == 2
    user = messages[1]["content"]
    assert "¿Qué me puedes contar de esta zona?" in user
    assert "Catedral de Sevilla" in user
    assert "historia" in user
    assert "arquitectura" in user


def test_max_5_pois():
    location = {"display_name": "Barcelona", "city": "Barcelona"}
    pois = [
        {"name": f"Lugar {i}", "category": "poi", "score": 1.0 - i * 0.1, "matched_interests": ["cultura"], "lat": 0, "lon": 0, "tags": {}}
        for i in range(10)
    ]
    interests = [{"name": "cultura", "keywords": [], "weight": 1.0}]

    messages = build_guide_prompt(location, pois, interests)

    user = messages[1]["content"]
    # Solo los primeros 5 POIs deben aparecer
    for i in range(5):
        assert f"Lugar {i}" in user
    for i in range(5, 10):
        assert f"Lugar {i}" not in user


def test_lang_aparece_en_system():
    location = {"display_name": "Paris", "city": "Paris"}
    pois = []
    interests = [{"name": "art", "keywords": [], "weight": 1.0}]

    messages = build_guide_prompt(location, pois, interests, lang="fr")

    system = messages[0]["content"]
    assert "fr" in system


def test_location_sin_neighbourhood():
    location = {
        "display_name": "Gran Via, Madrid",
        "city": "Madrid",
    }
    pois = []
    interests = []

    messages = build_guide_prompt(location, pois, interests)

    user = messages[1]["content"]
    assert "Gran Via, Madrid" in user
    # No debe romper si no hay neighbourhood
    assert len(messages) == 2
