"""Tests para diff_entities."""

import sys
import os

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


def test_entity_anadida():
    before = [{"id": "1", "name": "Alice"}]
    after = [{"id": "1", "name": "Alice"}, {"id": "2", "name": "Bob"}]
    result = diff_entities(before, after)
    assert len(result["added"]) == 1
    assert result["added"][0]["id"] == "2"
    assert result["removed"] == []
    assert result["modified"] == []
    assert result["unchanged"] == 1
    assert "1 added" in result["summary"]


def test_entity_eliminada():
    before = [{"id": "1", "name": "Alice"}, {"id": "2", "name": "Bob"}]
    after = [{"id": "1", "name": "Alice"}]
    result = diff_entities(before, after)
    assert result["added"] == []
    assert len(result["removed"]) == 1
    assert result["removed"][0]["id"] == "2"
    assert result["unchanged"] == 1
    assert "1 removed" in result["summary"]


def test_entity_modificada_con_detalle_de_campos():
    before = [{"id": "1", "name": "Alice", "status": "active"}]
    after = [{"id": "1", "name": "Alice", "status": "inactive"}]
    result = diff_entities(before, after)
    assert result["added"] == []
    assert result["removed"] == []
    assert len(result["modified"]) == 1
    mod = result["modified"][0]
    assert mod["key"] == "1"
    assert "status" in mod["changes"]
    assert mod["changes"]["status"]["old"] == "active"
    assert mod["changes"]["status"]["new"] == "inactive"
    assert result["unchanged"] == 0


def test_entities_identicas_unchanged():
    before = [{"id": "1", "name": "Alice"}, {"id": "2", "name": "Bob"}]
    after = [{"id": "1", "name": "Alice"}, {"id": "2", "name": "Bob"}]
    result = diff_entities(before, after)
    assert result["added"] == []
    assert result["removed"] == []
    assert result["modified"] == []
    assert result["unchanged"] == 2
    assert "2 unchanged" in result["summary"]


def test_ignore_fields_funciona():
    before = [{"id": "1", "name": "Alice", "updated_at": "2024-01-01", "created_at": "2023-01-01"}]
    after = [{"id": "1", "name": "Alice", "updated_at": "2024-06-01", "created_at": "2023-01-01"}]
    result = diff_entities(before, after)
    # updated_at se ignora por defecto -> unchanged
    assert result["unchanged"] == 1
    assert result["modified"] == []

    # Si no ignoramos updated_at, debe detectar el cambio
    result2 = diff_entities(before, after, ignore_fields=[])
    assert len(result2["modified"]) == 1
    assert "updated_at" in result2["modified"][0]["changes"]


def test_compare_fields_filtra_correctamente():
    before = [{"id": "1", "name": "Alice", "status": "active", "score": 10}]
    after = [{"id": "1", "name": "Bob", "status": "inactive", "score": 10}]
    # Solo comparar score -> no hay cambio en score, unchanged
    result = diff_entities(before, after, compare_fields=["score"])
    assert result["unchanged"] == 1
    assert result["modified"] == []

    # Solo comparar name -> detecta cambio
    result2 = diff_entities(before, after, compare_fields=["name"])
    assert len(result2["modified"]) == 1
    assert "name" in result2["modified"][0]["changes"]
    assert "status" not in result2["modified"][0]["changes"]


def test_lista_vacia_vs_lista_con_datos():
    before = []
    after = [{"id": "1", "name": "Alice"}, {"id": "2", "name": "Bob"}]
    result = diff_entities(before, after)
    assert len(result["added"]) == 2
    assert result["removed"] == []
    assert result["unchanged"] == 0

    # Invertido
    result2 = diff_entities(after, before)
    assert result2["added"] == []
    assert len(result2["removed"]) == 2
    assert result2["unchanged"] == 0


if __name__ == "__main__":
    test_entity_anadida()
    test_entity_eliminada()
    test_entity_modificada_con_detalle_de_campos()
    test_entities_identicas_unchanged()
    test_ignore_fields_funciona()
    test_compare_fields_filtra_correctamente()
    test_lista_vacia_vs_lista_con_datos()
    print("All tests passed.")
