"""Tests for equipment router (T03)."""
import pytest


@pytest.mark.asyncio
async def test_list_equipment(client, seeded_equipment):
    resp = await client.get("/api/equipment?page=1&page_size=10")
    assert resp.status_code == 200
    data = resp.json()
    assert "items" in data
    assert "total" in data
    assert "page" in data
    assert "page_size" in data
    assert "total_pages" in data
    assert data["total"] == 3
    assert len(data["items"]) == 3


@pytest.mark.asyncio
async def test_search_equipment(client, seeded_equipment):
    resp = await client.get("/api/equipment?search=K2")
    assert resp.status_code == 200
    data = resp.json()
    assert data["total"] == 1
    assert "K2" in data["items"][0]["name"]


@pytest.mark.asyncio
async def test_filter_category(client, seeded_equipment):
    resp = await client.get("/api/equipment?category=Lautsprecher")
    assert resp.status_code == 200
    data = resp.json()
    assert data["total"] == 2
    for item in data["items"]:
        assert item["category"] == "Lautsprecher"


@pytest.mark.asyncio
async def test_sort_name_asc(client, seeded_equipment):
    resp = await client.get("/api/equipment?sort=name_asc")
    assert resp.status_code == 200
    data = resp.json()
    names = [item["name"] for item in data["items"]]
    assert names == sorted(names)


@pytest.mark.asyncio
async def test_sort_name_desc(client, seeded_equipment):
    resp = await client.get("/api/equipment?sort=name_desc")
    assert resp.status_code == 200
    data = resp.json()
    names = [item["name"] for item in data["items"]]
    assert names == sorted(names, reverse=True)


@pytest.mark.asyncio
async def test_categories(client, seeded_equipment):
    resp = await client.get("/api/equipment/categories")
    assert resp.status_code == 200
    cats = resp.json()
    assert "Lautsprecher" in cats
    assert "Subwoofer" in cats


@pytest.mark.asyncio
async def test_equipment_detail(client, seeded_equipment):
    resp = await client.get(f"/api/equipment/{seeded_equipment[0].id}")
    assert resp.status_code == 200
    data = resp.json()
    assert data["name"] == "L-Acoustics K2"
    assert data["brand"] == "L-Acoustics"


@pytest.mark.asyncio
async def test_equipment_not_found(client, seeded_equipment):
    resp = await client.get("/api/equipment/999999")
    assert resp.status_code == 404
