"""Tests para merge_entity_attributes."""

import sys
import os

sys.path.insert(0, os.path.dirname(__file__))
from merge_entity_attributes import merge_entity_attributes


def test_atributos_complementarios():
    """Atributos complementarios (A tiene full_name, B tiene nationality) -> ambos."""
    a = {"full_name": "Alice Smith"}
    b = {"nationality": "US"}
    result = merge_entity_attributes([a, b])
    assert result["full_name"] == "Alice Smith"
    assert result["nationality"] == "US"


def test_atributos_conflictivos_risk_score_max():
    """Atributos conflictivos en risk_score -> max."""
    a = {"risk_score": 3.5}
    b = {"risk_score": 7.2}
    result = merge_entity_attributes([a, b])
    assert result["risk_score"] == 7.2


def test_atributos_first_seen_conflictivos_min():
    """Atributos first_seen conflictivos -> min."""
    a = {"first_seen": "2023-01-01"}
    b = {"first_seen": "2022-05-15"}
    result = merge_entity_attributes([a, b])
    assert result["first_seen"] == "2022-05-15"


def test_todos_null():
    """Todos null -> null."""
    a = {"score": None}
    b = {"score": None}
    result = merge_entity_attributes([a, b])
    assert result["score"] is None


def test_listas_union_sin_duplicados():
    """Listas -> union sin duplicados."""
    a = {"name_servers": ["ns1.example.com", "ns2.example.com"]}
    b = {"name_servers": ["ns2.example.com", "ns3.example.com"]}
    result = merge_entity_attributes([a, b])
    assert set(result["name_servers"]) == {"ns1.example.com", "ns2.example.com", "ns3.example.com"}
    assert len(result["name_servers"]) == 3


def test_boolean_or():
    """Boolean verified -> True si alguno es True."""
    a = {"verified": False}
    b = {"verified": True}
    result = merge_entity_attributes([a, b])
    assert result["verified"] is True


def test_string_mas_largo():
    """String conflictivo -> usar el mas largo."""
    a = {"description": "Short desc"}
    b = {"description": "A much longer and more detailed description"}
    result = merge_entity_attributes([a, b])
    assert result["description"] == "A much longer and more detailed description"


def test_valores_iguales():
    """Valores iguales -> usar ese valor."""
    a = {"alias": "Alice"}
    b = {"alias": "Alice"}
    result = merge_entity_attributes([a, b])
    assert result["alias"] == "Alice"


def test_un_solo_candidato():
    """Un solo candidato -> retorna sus atributos tal cual."""
    a = {"risk_score": 5.0, "verified": True}
    result = merge_entity_attributes([a])
    assert result == a


def test_lista_vacia():
    """Lista vacia -> retorna dict vacio."""
    result = merge_entity_attributes([])
    assert result == {}


def test_last_seen_max():
    """last_seen conflictivo -> max (mas reciente)."""
    a = {"last_seen": "2023-12-01"}
    b = {"last_seen": "2024-03-15"}
    result = merge_entity_attributes([a, b])
    assert result["last_seen"] == "2024-03-15"


def test_un_valor_non_null():
    """Un candidato tiene null, otro tiene valor -> usar el valor."""
    a = {"cvss": None}
    b = {"cvss": 8.5}
    result = merge_entity_attributes([a, b])
    assert result["cvss"] == 8.5
