"""Integration tests for admin global categories endpoints."""

import pytest
from httpx import AsyncClient


class TestAdminGlobalCategories:

    @pytest.mark.asyncio
    async def test_list_global_categories(
        self, client: AsyncClient, admin_auth_headers: dict
    ):
        """List global categories."""
        response = await client.get("/api/v1/admin/categories/global", headers=admin_auth_headers)
        assert response.status_code == 200
        assert isinstance(response.json(), list)

    @pytest.mark.asyncio
    async def test_create_global_category(
        self, client: AsyncClient, admin_auth_headers: dict
    ):
        """Create a global category."""
        response = await client.post(
            "/api/v1/admin/categories/global",
            json={"name": "Test Category", "slug": "test-category"},
            headers=admin_auth_headers,
        )
        assert response.status_code == 201
        data = response.json()
        assert data["name"] == "Test Category"
        assert data["slug"] == "test-category"
        assert data["account_id"] is None

    @pytest.mark.asyncio
    async def test_update_global_category(
        self, client: AsyncClient, admin_auth_headers: dict
    ):
        """Update a global category."""
        create_resp = await client.post(
            "/api/v1/admin/categories/global",
            json={"name": "Old Name", "slug": "old-name"},
            headers=admin_auth_headers,
        )
        cat_id = create_resp.json()["id"]

        response = await client.put(
            f"/api/v1/admin/categories/global/{cat_id}",
            json={"name": "New Name"},
            headers=admin_auth_headers,
        )
        assert response.status_code == 200
        assert response.json()["name"] == "New Name"

    @pytest.mark.asyncio
    async def test_delete_global_category(
        self, client: AsyncClient, admin_auth_headers: dict
    ):
        """Delete (archive) a global category."""
        create_resp = await client.post(
            "/api/v1/admin/categories/global",
            json={"name": "To Delete", "slug": "to-delete"},
            headers=admin_auth_headers,
        )
        cat_id = create_resp.json()["id"]

        response = await client.delete(
            f"/api/v1/admin/categories/global/{cat_id}",
            headers=admin_auth_headers,
        )
        assert response.status_code == 204

    @pytest.mark.asyncio
    async def test_duplicate_slug_fails(
        self, client: AsyncClient, admin_auth_headers: dict
    ):
        """Duplicate slug returns 409."""
        await client.post(
            "/api/v1/admin/categories/global",
            json={"name": "First", "slug": "dupe-slug"},
            headers=admin_auth_headers,
        )
        response = await client.post(
            "/api/v1/admin/categories/global",
            json={"name": "Second", "slug": "dupe-slug"},
            headers=admin_auth_headers,
        )
        assert response.status_code == 409

    @pytest.mark.asyncio
    async def test_non_admin_cannot_access(
        self, client: AsyncClient, auth_headers: dict
    ):
        """Non-admin gets 403."""
        response = await client.get("/api/v1/admin/categories/global", headers=auth_headers)
        assert response.status_code == 403
