"""
Contract Tests: Services API

Endpoint: /productos/api/v1/services/
App: productos

Returns available veterinary services filtered by pet type and location.
Critical for vet assignment automation.
"""

from ..base import ContractTestCase
from ..endpoints import Endpoints
from ..helpers import sample_pet_owner, unique_email, SAMPLE_CAT, SAMPLE_DOG


class TestServicesList(ContractTestCase):
    """GET /productos/api/v1/services/"""

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

        self.assert_status(response, 200)

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

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

    def test_services_have_required_fields(self):
        """Each service should have id and name"""
        response = self.get(Endpoints.SERVICES, params={"page_size": 10})

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

        if len(services) > 0:
            service = services[0]
            self.assert_has_fields(service, "id", "name")

    def test_accepts_pet_id_filter(self):
        """Services endpoint accepts pet_id parameter"""
        response = self.get(Endpoints.SERVICES, params={"pet_id": 1})

        # Should not error (even if pet doesn't exist, endpoint should handle gracefully)
        self.assertIn(response.status_code, [200, 404])


class TestServicesFiltering(ContractTestCase):
    """GET /productos/api/v1/services/ with filters"""

    def _create_owner_with_cat(self):
        """Helper to create owner and cat"""
        owner_data = sample_pet_owner(unique_email("service_owner"))
        owner_response = self.post(Endpoints.PET_OWNERS, owner_data)
        owner_id = owner_response.data["id"]

        pet_data = {**SAMPLE_CAT, "owner": owner_id}
        pet_response = self.post(Endpoints.PETS, pet_data)
        return pet_response.data["id"]

    def _create_owner_with_dog(self):
        """Helper to create owner and dog"""
        owner_data = sample_pet_owner(unique_email("service_owner"))
        owner_response = self.post(Endpoints.PET_OWNERS, owner_data)
        owner_id = owner_response.data["id"]

        pet_data = {**SAMPLE_DOG, "owner": owner_id}
        pet_response = self.post(Endpoints.PETS, pet_data)
        return pet_response.data["id"]

    def test_filter_services_by_cat(self):
        """
        Services filtered by cat pet_id returns appropriate services.

        Request (from production turnero):
            GET /productos/api/v1/services/?pet_id=123

        Response structure validates services available for CAT type.
        """
        cat_id = self._create_owner_with_cat()
        response = self.get(Endpoints.SERVICES, params={"pet_id": cat_id, "page_size": 10})

        # Should return services or handle gracefully
        self.assertIn(response.status_code, [200, 404])
        if response.status_code == 200:
            data = response.data
            services = data["results"] if isinstance(data, dict) and "results" in data else data
            self.assertIsInstance(services, list)

    def test_filter_services_by_dog(self):
        """
        Services filtered by dog pet_id returns appropriate services.

        Different pet types may have different service availability.
        """
        dog_id = self._create_owner_with_dog()
        response = self.get(Endpoints.SERVICES, params={"pet_id": dog_id, "page_size": 10})

        self.assertIn(response.status_code, [200, 404])
        if response.status_code == 200:
            data = response.data
            services = data["results"] if isinstance(data, dict) and "results" in data else data
            self.assertIsInstance(services, list)

    def test_services_without_pet_returns_all(self):
        """
        Services without pet filter returns all available services.

        Used for initial service browsing before pet selection.
        """
        response = self.get(Endpoints.SERVICES, params={"page_size": 10})

        self.assert_status(response, 200)
        data = response.data
        services = data["results"] if isinstance(data, dict) and "results" in data else data
        self.assertIsInstance(services, list)
