"""
Contract Tests: Service Requests API

Endpoint: /solicitudes/service-requests/
App: solicitudes

Creates and manages service requests (appointment bookings).
"""

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


class TestServiceRequestList(ContractTestCase):
    """GET /solicitudes/service-requests/"""

    def test_list_returns_200(self):
        """GET should return list of service requests (with pagination)"""
        response = self.get(Endpoints.SERVICE_REQUESTS, params={"page_size": 1})

        self.assert_status(response, 200)

    def test_returns_list(self):
        """GET should return a list (possibly paginated)"""
        response = self.get(Endpoints.SERVICE_REQUESTS, params={"page_size": 10})

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


class TestServiceRequestFields(ContractTestCase):
    """Field validation for service requests"""

    def test_has_state_field(self):
        """Service requests should have a state/status field"""
        response = self.get(Endpoints.SERVICE_REQUESTS, params={"page_size": 1})

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

        if len(requests_list) > 0:
            req = requests_list[0]
            has_state = "state" in req or "status" in req
            self.assertTrue(has_state, "Service request should have state/status field")


class TestServiceRequestCreate(ContractTestCase):
    """POST /solicitudes/service-requests/"""

    def test_create_requires_fields(self):
        """Creating service request with empty data should fail"""
        response = self.post(Endpoints.SERVICE_REQUESTS, {})

        # Should return 400 with validation errors
        self.assert_status(response, 400)
