"""Tests para filter_relations_by_entity_types."""

from __future__ import annotations

import os
import sys

sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", ".."))

from core.filter_relations_by_entity_types import filter_relations_by_entity_types

NAME_TO_TYPE = {
    "carlos torres": "person",
    "bbva": "organization",
    "madrid": "location",
    "santander": "organization",
    "ana": "person",
}

ALLOWED = {
    "president_of": (["person"], ["organization"]),
    "located_in": (["organization", "person"], ["location"]),
}


def test_pares_validos_se_incluyen_en_kept():
    """pares validos se incluyen en kept"""
    relations = {"president_of": [("Carlos Torres", "BBVA")]}
    kept, dropped = filter_relations_by_entity_types(relations, NAME_TO_TYPE, ALLOWED)
    assert len(kept) == 1
    assert kept[0]["from"] == "Carlos Torres"
    assert kept[0]["to"] == "BBVA"
    assert len(dropped) == 0


def test_pares_con_tipos_incompatibles_van_a_dropped():
    """pares con tipos incompatibles van a dropped"""
    # Madrid es location, no person -> no puede presidir nada
    relations = {"president_of": [("Madrid", "Santander")]}
    kept, dropped = filter_relations_by_entity_types(relations, NAME_TO_TYPE, ALLOWED)
    assert len(kept) == 0
    assert len(dropped) == 1
    assert dropped[0]["head_type"] == "location"


def test_rel_type_no_en_allowed_se_acepta_siempre():
    """rel_type no en allowed se acepta siempre"""
    relations = {"unknown_rel": [("Carlos Torres", "Madrid")]}
    kept, dropped = filter_relations_by_entity_types(relations, NAME_TO_TYPE, ALLOWED)
    assert len(kept) == 1
    assert len(dropped) == 0


def test_entidad_no_encontrada_en_name_to_type_va_a_dropped():
    """entidad no encontrada en name_to_type va a dropped"""
    # "Desconocido" no esta en name_to_type -> head_type es None -> dropped
    relations = {"president_of": [("Desconocido", "BBVA")]}
    kept, dropped = filter_relations_by_entity_types(relations, NAME_TO_TYPE, ALLOWED)
    assert len(dropped) == 1
    assert dropped[0]["head_type"] is None
