"""
Tests for API error handling consistency.

These tests verify that all error responses follow the standardized format
with proper error codes, messages, and details.
"""

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


class ErrorResponseFormatTestCase(TestCase):
    """Tests for standardized error response format."""

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

    def test_404_error_format(self):
        """Test that 404 errors follow standardized format."""
        response = self.client.get("/api/v1/parks/nonexistent-slug/")

        if response.status_code == status.HTTP_404_NOT_FOUND:
            data = response.json()
            # Should have error information
            self.assertTrue(
                "error" in data or "detail" in data or "status" in data, "404 response should contain error information"
            )

    def test_400_error_format(self):
        """Test that 400 validation errors follow standardized format."""
        response = self.client.get("/api/v1/rides/hybrid/", {"offset": "invalid"})

        if response.status_code == status.HTTP_400_BAD_REQUEST:
            data = response.json()
            # Should have error information
            self.assertTrue(
                "error" in data or "status" in data or "detail" in data, "400 response should contain error information"
            )

    def test_500_error_format(self):
        """Test that 500 errors follow standardized format."""
        # This is harder to test directly, but we can verify the handler exists
        pass


class ErrorCodeConsistencyTestCase(TestCase):
    """Tests for consistent error codes."""

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

    def test_validation_error_code(self):
        """Test that validation errors use consistent error codes."""
        response = self.client.get("/api/v1/rides/hybrid/", {"offset": "invalid"})

        if response.status_code == status.HTTP_400_BAD_REQUEST:
            data = response.json()
            if "error" in data and isinstance(data["error"], dict):
                self.assertIn("code", data["error"])
                self.assertEqual(data["error"]["code"], "VALIDATION_ERROR")


class AuthenticationErrorTestCase(TestCase):
    """Tests for authentication error handling."""

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

    def test_unauthorized_error_format(self):
        """Test that unauthorized errors are properly formatted."""
        # Try to access protected endpoint without auth
        response = self.client.get("/api/v1/accounts/profile/")

        if response.status_code == status.HTTP_401_UNAUTHORIZED:
            data = response.json()
            # Should have error information
            self.assertTrue("error" in data or "detail" in data, "401 response should contain error information")

    def test_forbidden_error_format(self):
        """Test that forbidden errors are properly formatted."""
        # This would need authentication to test properly
        pass


class ExceptionHandlerTestCase(TestCase):
    """Tests for the custom exception handler."""

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

    def test_custom_exception_handler_is_configured(self):
        """Test that custom exception handler is configured."""
        from django.conf import settings

        exception_handler = settings.REST_FRAMEWORK.get("EXCEPTION_HANDLER")
        self.assertEqual(exception_handler, "apps.core.api.exceptions.custom_exception_handler")

    def test_throttled_error_format(self):
        """Test that throttled errors are properly formatted."""
        # This would need many rapid requests to trigger throttling
        pass
