"""
Contract Tests: Categories API

Endpoint: /productos/api/v1/categories/
App: productos

Returns service categories filtered by location availability.
Categories without available services in location should be hidden.
"""

from ..base import ContractTestCase
from ..endpoints import Endpoints


class TestCategoriesList(ContractTestCase):
    """GET /productos/api/v1/categories/"""

    def test_list_returns_200(self):
        """GET categories returns 200"""
        response = self.get(Endpoints.CATEGORIES, params={"page_size": 10})

        self.assert_status(response, 200)

    def test_returns_list(self):
        """GET categories returns a list"""
        response = self.get(Endpoints.CATEGORIES, params={"page_size": 10})

        self.assert_status(response, 200)
        data = response.data
        # Handle paginated or non-paginated response
        categories = data["results"] if isinstance(data, dict) and "results" in data else data
        self.assertIsInstance(categories, list)

    def test_categories_have_required_fields(self):
        """
        Each category should have id, name, and description.

        Request (from production turnero):
            GET /productos/api/v1/categories/

        Response (200):
            [
                {
                    "id": 1,
                    "name": "Consulta General",
                    "description": "Consultas veterinarias generales"
                },
                {
                    "id": 2,
                    "name": "Vacunación",
                    "description": "Servicios de vacunación"
                }
            ]
        """
        response = self.get(Endpoints.CATEGORIES, params={"page_size": 10})

        data = response.data
        categories = data["results"] if isinstance(data, dict) and "results" in data else data

        if len(categories) > 0:
            category = categories[0]
            self.assert_has_fields(category, "id", "name", "description")

    def test_only_active_categories_returned(self):
        """
        Only active categories are returned in the list.

        Business rule: Inactive categories should not be visible to users.
        """
        response = self.get(Endpoints.CATEGORIES, params={"page_size": 50})

        data = response.data
        categories = data["results"] if isinstance(data, dict) and "results" in data else data

        # All categories should be active (no 'active': False in response)
        # This is enforced at queryset level in CategoryViewSet
        self.assertIsInstance(categories, list)


class TestCategoryRetrieve(ContractTestCase):
    """GET /productos/api/v1/categories/{id}/"""

    def test_get_category_by_id_returns_200(self):
        """
        GET category by ID returns category details.

        First fetch list to get a valid ID, then retrieve that category.
        """
        # Get first category
        list_response = self.get(Endpoints.CATEGORIES, params={"page_size": 1})
        if list_response.status_code != 200:
            self.skipTest("No categories available for testing")

        data = list_response.data
        categories = data["results"] if isinstance(data, dict) and "results" in data else data

        if len(categories) == 0:
            self.skipTest("No categories available for testing")

        category_id = categories[0]["id"]

        # Test detail endpoint
        response = self.get(f"{Endpoints.CATEGORIES}{category_id}/")

        self.assert_status(response, 200)
        self.assertEqual(response.data["id"], category_id)

    def test_nonexistent_category_returns_404(self):
        """GET non-existent category returns 404"""
        response = self.get(f"{Endpoints.CATEGORIES}999999/")

        self.assert_status(response, 404)
