import pytest
from httpx import AsyncClient

pytestmark = pytest.mark.asyncio


async def test_get_categories_seeded_defaults(client: AsyncClient):
    # Retrieve categories and verify the default list is present (since lifespan seeds them)
    response = await client.get("/api/v1/categories")
    assert response.status_code == 200
    categories = response.json()
    assert len(categories) >= 8

    names = [c["name"] for c in categories]
    assert "Food" in names
    assert "Shopping" in names
    assert "Travel" in names
    assert "Other" in names


async def test_create_and_duplicate_category(client: AsyncClient):
    # 1. Create a new category
    response = await client.post("/api/v1/categories", json={"name": "Medical"})
    assert response.status_code == 201
    data = response.json()
    assert "id" in data
    assert data["name"] == "Medical"

    # 2. Try creating the same category (duplicate, case-insensitive check)
    dup_response = await client.post("/api/v1/categories", json={"name": "medical"})
    assert dup_response.status_code == 400
    assert "already exists" in dup_response.json()["detail"]
