import pytest

from backend.app.models import TagKind
from backend.app.services.ml.aliases import AliasService
from backend.app.services.tag_service import TagService

pytestmark = pytest.mark.integration


@pytest.mark.asyncio
async def test_create_and_resolve(db):
    tags = TagService(db)
    canonical = await tags.find_or_create("Sasuke Uchiha", TagKind.character)
    aliases = AliasService(db)
    await aliases.create("uchiha_sasuke", "character", canonical.id)

    resolved = await aliases.resolve("uchiha_sasuke", "character")
    assert resolved is not None
    assert resolved.id == canonical.id


@pytest.mark.asyncio
async def test_resolve_returns_none_when_no_alias(db):
    aliases = AliasService(db)
    assert await aliases.resolve("nonexistent", "general") is None


@pytest.mark.asyncio
async def test_category_scoped(db):
    tags = TagService(db)
    char = await tags.find_or_create("Naruto", TagKind.character)
    copy = await tags.find_or_create("Naruto", TagKind.fandom)
    aliases = AliasService(db)
    await aliases.create("naruto", "character", char.id)
    await aliases.create("naruto", "copyright", copy.id)

    assert (await aliases.resolve("naruto", "character")).id == char.id
    assert (await aliases.resolve("naruto", "copyright")).id == copy.id


@pytest.mark.asyncio
async def test_create_idempotent(db):
    tags = TagService(db)
    t = await tags.find_or_create("X", TagKind.general)
    aliases = AliasService(db)
    await aliases.create("x_alias", "general", t.id)
    await aliases.create("x_alias", "general", t.id)  # no error
    assert (await aliases.resolve("x_alias", "general")).id == t.id


@pytest.mark.asyncio
async def test_resolve_many(db):
    tags = TagService(db)
    a = await tags.find_or_create("Alice", TagKind.character)
    b = await tags.find_or_create("Bob", TagKind.artist)
    aliases = AliasService(db)
    await aliases.create("alice_x", "character", a.id)
    await aliases.create("bob_x", "artist", b.id)

    out = await aliases.resolve_many(
        [("alice_x", "character"), ("bob_x", "artist"), ("unknown", "general")]
    )
    assert out[("alice_x", "character")].id == a.id
    assert out[("bob_x", "artist")].id == b.id
    assert ("unknown", "general") not in out


@pytest.mark.asyncio
async def test_remove(db):
    tags = TagService(db)
    t = await tags.find_or_create("Y", TagKind.general)
    aliases = AliasService(db)
    await aliases.create("y_alias", "general", t.id)
    await aliases.remove("y_alias", "general")
    assert await aliases.resolve("y_alias", "general") is None


@pytest.mark.asyncio
async def test_list_all(db):
    tags = TagService(db)
    t = await tags.find_or_create("Z", TagKind.general)
    aliases = AliasService(db)
    await aliases.create("z1", "general", t.id)
    rows = await aliases.list_all()
    assert any(r.alias_string == "z1" and r.canonical_tag_name == "Z" for r in rows)


@pytest.mark.asyncio
async def test_list_for_tag(db):
    tags = TagService(db)
    keep = await tags.find_or_create("Keeper", TagKind.general)
    other = await tags.find_or_create("Other", TagKind.general)
    aliases = AliasService(db)
    await aliases.create("k1", "general", keep.id)
    await aliases.create("k2", "general", keep.id)
    await aliases.create("o1", "general", other.id)

    rows = await aliases.list_for_tag(keep.id)
    assert {r.alias_string for r in rows} == {"k1", "k2"}
    assert all(r.canonical_tag_id == keep.id for r in rows)
