import pytest

from backend.app.models import Tag, TagKind

pytestmark = pytest.mark.integration


@pytest.mark.asyncio
async def test_directory_endpoint(client, db):
    db.add(Tag(name="api_tag", kind=TagKind.general))
    await db.flush()
    await db.commit()
    resp = await client.get("/api/tags/directory?limit=10")
    assert resp.status_code == 200
    body = await resp.get_json()
    assert "cards" in body and "next_cursor" in body


@pytest.mark.asyncio
async def test_directory_bad_limit(client):
    resp = await client.get("/api/tags/directory?limit=nan")
    assert resp.status_code == 400


@pytest.mark.asyncio
async def test_directory_invalid_kind_is_400_not_500(client):
    # 'copyright' is not a TagKind member; the route must reject it
    # cleanly (400) instead of letting the enum cast 500.
    resp = await client.get("/api/tags/directory?kind=copyright")
    assert resp.status_code == 400


@pytest.mark.asyncio
async def test_directory_valid_kind_ok(client, db):
    db.add(Tag(name="a_fandom", kind=TagKind.fandom))
    await db.flush()
    await db.commit()
    resp = await client.get("/api/tags/directory?kind=fandom&limit=10")
    assert resp.status_code == 200
    body = await resp.get_json()
    assert any(c["name"] == "a_fandom" for c in body["cards"])
