"""FC-3f: /api/artists/directory API tests."""

import pytest

from backend.app.models import Artist, ImageRecord, Source

pytestmark = pytest.mark.integration


@pytest.fixture
async def seeded(db):
    alice = Artist(name="alice-api", slug="alice-api", is_subscription=True)
    bob = Artist(name="bob-api", slug="bob-api", is_subscription=False)
    db.add(alice)
    db.add(bob)
    await db.flush()
    db.add(Source(
        artist_id=alice.id, platform="patreon",
        url="https://p/alice-api", enabled=True,
    ))
    db.add(ImageRecord(
        path="/images/api/alice.jpg",
        sha256=("a" * 60) + "0001",
        size_bytes=10, mime="image/jpeg", width=10, height=10,
        origin="downloaded", artist_id=alice.id,
    ))
    await db.commit()
    return alice, bob


@pytest.mark.asyncio
async def test_directory_returns_cards_and_cursor_keys(client, seeded):
    resp = await client.get("/api/artists/directory")
    assert resp.status_code == 200
    body = await resp.get_json()
    assert set(body.keys()) == {"cards", "next_cursor"}
    assert isinstance(body["cards"], list)


@pytest.mark.asyncio
async def test_directory_card_shape(client, seeded):
    resp = await client.get("/api/artists/directory?q=alice-api")
    body = await resp.get_json()
    card = next(c for c in body["cards"] if c["name"] == "alice-api")
    assert set(card.keys()) == {
        "id", "name", "slug", "is_subscription", "image_count",
        "unseen_count", "preview_thumbnails",
    }
    assert card["is_subscription"] is True
    assert card["image_count"] == 1
    assert len(card["preview_thumbnails"]) == 1


@pytest.mark.asyncio
async def test_directory_rejects_malformed_cursor(client):
    resp = await client.get("/api/artists/directory?cursor=garbage!!!")
    assert resp.status_code == 400
    body = await resp.get_json()
    assert body["error"] == "invalid_cursor"


@pytest.mark.asyncio
async def test_directory_rejects_unknown_platform(client):
    resp = await client.get("/api/artists/directory?platform=myspace")
    assert resp.status_code == 400
    body = await resp.get_json()
    assert body["error"] == "unknown_platform"


@pytest.mark.asyncio
async def test_directory_rejects_limit_out_of_range(client):
    resp = await client.get("/api/artists/directory?limit=0")
    assert resp.status_code == 400
    resp = await client.get("/api/artists/directory?limit=500")
    assert resp.status_code == 400


@pytest.mark.asyncio
async def test_directory_q_propagates(client, seeded):
    resp = await client.get("/api/artists/directory?q=alice-api")
    body = await resp.get_json()
    names = [c["name"] for c in body["cards"]]
    assert "alice-api" in names
    assert "bob-api" not in names


@pytest.mark.asyncio
async def test_directory_platform_propagates(client, seeded):
    resp = await client.get("/api/artists/directory?platform=patreon")
    body = await resp.get_json()
    names = [c["name"] for c in body["cards"]]
    assert "alice-api" in names
    assert "bob-api" not in names  # no patreon source
