import pytest

pytestmark = pytest.mark.integration


async def _series(client, name="Vol"):
    r = await client.post("/api/tags", json={"name": name, "kind": "series"})
    return (await r.get_json())["id"]


@pytest.mark.asyncio
async def test_pages_404_for_non_series(client):
    r = await client.post("/api/tags", json={"name": "g", "kind": "general"})
    gid = (await r.get_json())["id"]
    resp = await client.get(f"/api/series/{gid}/pages")
    assert resp.status_code == 404


@pytest.mark.asyncio
async def test_pages_empty_shape(client):
    sid = await _series(client, "Empty")
    resp = await client.get(f"/api/series/{sid}/pages")
    assert resp.status_code == 200
    body = await resp.get_json()
    assert body["series"]["id"] == sid
    assert body["pages"] == []


@pytest.mark.asyncio
async def test_add_requires_ids(client):
    sid = await _series(client, "NoIds")
    resp = await client.post(f"/api/series/{sid}/pages", json={})
    assert resp.status_code == 400


@pytest.mark.asyncio
async def test_add_over_cap_400(client):
    sid = await _series(client, "Cap")
    resp = await client.post(
        f"/api/series/{sid}/pages",
        json={"image_ids": list(range(1, 502))},
    )
    assert resp.status_code == 400


@pytest.mark.asyncio
async def test_set_page_number_requires_image_id(client):
    sid = await _series(client, "Num")
    resp = await client.post(
        f"/api/series/{sid}/pages/number", json={"page_number": 5}
    )
    assert resp.status_code == 400


@pytest.mark.asyncio
async def test_cover_requires_image_id(client):
    sid = await _series(client, "Cov")
    resp = await client.post(f"/api/series/{sid}/cover", json={})
    assert resp.status_code == 400
