import pytest

pytestmark = pytest.mark.integration


@pytest.mark.asyncio
async def test_autocomplete_empty(client):
    resp = await client.get("/api/tags/autocomplete?q=")
    assert resp.status_code == 200
    assert await resp.get_json() == []


@pytest.mark.asyncio
async def test_create_tag(client):
    resp = await client.post("/api/tags", json={"name": "Bob", "kind": "artist"})
    assert resp.status_code == 201
    body = await resp.get_json()
    assert body["name"] == "Bob"
    assert body["kind"] == "artist"


@pytest.mark.asyncio
async def test_create_tag_rejects_invalid_kind(client):
    resp = await client.post("/api/tags", json={"name": "Bob", "kind": "notakind"})
    assert resp.status_code == 400


@pytest.mark.asyncio
async def test_create_character_without_fandom_ok(client):
    resp = await client.post("/api/tags", json={"name": "Alice", "kind": "character"})
    assert resp.status_code == 201


@pytest.mark.asyncio
async def test_create_character_with_bad_fandom_id(client):
    artist_resp = await client.post("/api/tags", json={"name": "X", "kind": "artist"})
    artist_id = (await artist_resp.get_json())["id"]
    resp = await client.post(
        "/api/tags", json={"name": "Y", "kind": "character", "fandom_id": artist_id}
    )
    assert resp.status_code == 400


@pytest.mark.asyncio
async def test_create_tag_missing_name_400(client):
    """name is still required; only `kind` became optional."""
    resp = await client.post("/api/tags", json={})
    assert resp.status_code == 400
    resp = await client.post("/api/tags", json={"kind": "artist"})
    assert resp.status_code == 400


@pytest.mark.asyncio
async def test_create_tag_name_only_defaults_to_general(client):
    """IR-style: name without kind and without `kind:` prefix → general.
    #701: operator-entered names are Title-Cased at the create endpoint."""
    resp = await client.post("/api/tags", json={"name": "sunset"})
    assert resp.status_code == 201
    body = await resp.get_json()
    assert body["name"] == "Sunset"
    assert body["kind"] == "general"


@pytest.mark.asyncio
async def test_create_tag_with_kind_prefix(client):
    """IR-style: `name="character:Saber"` without explicit kind → parsed as character."""
    resp = await client.post("/api/tags", json={"name": "character:Saber"})
    assert resp.status_code == 201
    body = await resp.get_json()
    assert body["name"] == "Saber"
    assert body["kind"] == "character"


@pytest.mark.asyncio
async def test_explicit_kind_overrides_prefix_parsing(client):
    """If caller passes explicit kind, don't re-parse the name —
    colon and prefix stay literal. #701: first letter capitalized, tail
    preserved (acronym-safe), so the 'S' in Saber survives."""
    resp = await client.post(
        "/api/tags", json={"name": "character:Saber", "kind": "general"}
    )
    assert resp.status_code == 201
    body = await resp.get_json()
    assert body["name"] == "Character:Saber"
    assert body["kind"] == "general"


@pytest.mark.asyncio
async def test_unknown_prefix_kept_literal(client):
    """`http:example` — `http` not in KNOWN_KINDS → kind=general, literal name.
    #701: Title-Cased as one word (no internal whitespace to split on)."""
    resp = await client.post("/api/tags", json={"name": "http://example.com"})
    assert resp.status_code == 201
    body = await resp.get_json()
    assert body["name"] == "Http://example.com"
    assert body["kind"] == "general"
