"""
Tests for API response format consistency.

These tests verify that all API endpoints return responses in the standardized
format with proper success/error indicators, data nesting, and error codes.
"""

from django.test import TestCase
from rest_framework import status
from rest_framework.test import APIClient


class ResponseFormatTestCase(TestCase):
    """Tests for standardized API response format."""

    def setUp(self):
        """Set up test client."""
        self.client = APIClient()

    def test_success_response_has_success_field(self):
        """Test that success responses include success: true field."""
        response = self.client.get("/api/v1/parks/hybrid/")

        if response.status_code == status.HTTP_200_OK:
            data = response.json()
            self.assertIn("success", data)
            self.assertTrue(data["success"])

    def test_success_response_has_data_field(self):
        """Test that success responses include data field."""
        response = self.client.get("/api/v1/parks/hybrid/")

        if response.status_code == status.HTTP_200_OK:
            data = response.json()
            self.assertIn("data", data)

    def test_error_response_format(self):
        """Test that error responses follow standardized format."""
        # Request a non-existent resource
        response = self.client.get("/api/v1/parks/non-existent-park-slug/")

        if response.status_code == status.HTTP_404_NOT_FOUND:
            data = response.json()
            # Should have either 'error' or 'status' key for error responses
            self.assertTrue(
                "error" in data or "status" in data or "detail" in data,
                "Error response should contain error information",
            )

    def test_validation_error_format(self):
        """Test that validation errors include field-specific details."""
        # This test would need authentication but we can test the format
        pass


class HybridEndpointResponseTestCase(TestCase):
    """Tests for hybrid endpoint response format."""

    def setUp(self):
        """Set up test client."""
        self.client = APIClient()

    def test_parks_hybrid_response_format(self):
        """Test parks hybrid endpoint response structure."""
        response = self.client.get("/api/v1/parks/hybrid/")

        if response.status_code == status.HTTP_200_OK:
            data = response.json()
            self.assertIn("success", data)
            self.assertIn("data", data)

            if data.get("data"):
                result = data["data"]
                self.assertIn("parks", result)
                self.assertIn("total_count", result)
                self.assertIn("strategy", result)
                self.assertIn("has_more", result)

    def test_rides_hybrid_response_format(self):
        """Test rides hybrid endpoint response structure."""
        response = self.client.get("/api/v1/rides/hybrid/")

        if response.status_code == status.HTTP_200_OK:
            data = response.json()
            self.assertIn("success", data)
            self.assertIn("data", data)

            if data.get("data"):
                result = data["data"]
                self.assertIn("rides", result)
                self.assertIn("total_count", result)
                self.assertIn("strategy", result)
                self.assertIn("has_more", result)


class FilterMetadataResponseTestCase(TestCase):
    """Tests for filter metadata endpoint response format."""

    def setUp(self):
        """Set up test client."""
        self.client = APIClient()

    def test_parks_filter_metadata_response_format(self):
        """Test parks filter metadata endpoint response structure."""
        response = self.client.get("/api/v1/parks/filter-metadata/")

        if response.status_code == status.HTTP_200_OK:
            data = response.json()
            self.assertIn("success", data)
            self.assertIn("data", data)

    def test_rides_filter_metadata_response_format(self):
        """Test rides filter metadata endpoint response structure."""
        response = self.client.get("/api/v1/rides/filter-metadata/")

        if response.status_code == status.HTTP_200_OK:
            data = response.json()
            self.assertIn("success", data)
            self.assertIn("data", data)
