"""serialize_tag — the shared canonical tag-dict serializer."""

from types import SimpleNamespace

from backend.app.models import TagKind
from backend.app.services.tag_query import serialize_tag


def test_serialize_tag_with_enum_kind():
    # ORM/column rows carry kind as a TagKind enum.
    row = SimpleNamespace(
        id=1, name="Sasuke Uchiha", kind=TagKind.character,
        fandom_id=5, fandom_name="Naruto", is_system=False,
    )
    assert serialize_tag(row) == {
        "id": 1, "name": "Sasuke Uchiha", "kind": "character",
        "fandom_id": 5, "fandom_name": "Naruto", "is_system": False,
        # Applied-tag context defaults for rows without image scope (m139).
        "source": None, "confirmed": False,
    }


def test_serialize_tag_with_string_kind_and_no_fandom():
    # autocomplete hits already store kind as a plain string. A row from a
    # select that predates the is_system column serializes as non-system.
    row = SimpleNamespace(
        id=2, name="solo", kind="general", fandom_id=None, fandom_name=None,
    )
    assert serialize_tag(row) == {
        "id": 2, "name": "solo", "kind": "general",
        "fandom_id": None, "fandom_name": None, "is_system": False,
        "source": None, "confirmed": False,
    }


def test_serialize_tag_system_row():
    row = SimpleNamespace(
        id=3, name="wip", kind=TagKind.general,
        fandom_id=None, fandom_name=None, is_system=True,
    )
    assert serialize_tag(row)["is_system"] is True
