"""
Tests for parks admin interfaces.

These tests verify the functionality of park, area, company, location,
and review admin classes including query optimization and custom actions.
"""

from django.contrib.admin.sites import AdminSite
from django.contrib.auth import get_user_model
from django.test import RequestFactory, TestCase

from apps.parks.admin import (
    CompanyAdmin,
    ParkAdmin,
    ParkAreaAdmin,
    ParkLocationAdmin,
    ParkReviewAdmin,
)
from apps.parks.models import Company, Park, ParkArea, ParkLocation, ParkReview

User = get_user_model()


class TestParkAdmin(TestCase):
    """Tests for ParkAdmin class."""

    def setUp(self):
        self.factory = RequestFactory()
        self.site = AdminSite()
        self.admin = ParkAdmin(model=Park, admin_site=self.site)

    def test_list_display_fields(self):
        """Verify all required fields are in list_display."""
        required_fields = [
            "name",
            "formatted_location",
            "status_badge",
            "operator_link",
            "ride_count",
            "average_rating",
            "created_at",
        ]
        for field in required_fields:
            assert field in self.admin.list_display

    def test_list_select_related(self):
        """Verify select_related is configured for ForeignKeys."""
        assert "operator" in self.admin.list_select_related
        assert "property_owner" in self.admin.list_select_related
        assert "location" in self.admin.list_select_related

    def test_list_prefetch_related(self):
        """Verify prefetch_related is configured for reverse relations."""
        assert "areas" in self.admin.list_prefetch_related
        assert "rides" in self.admin.list_prefetch_related

    def test_search_fields_include_relations(self):
        """Verify search includes related object fields."""
        assert "location__city" in self.admin.search_fields
        assert "operator__name" in self.admin.search_fields

    def test_export_fields_configured(self):
        """Verify export fields are configured."""
        assert hasattr(self.admin, "export_fields")
        assert "id" in self.admin.export_fields
        assert "name" in self.admin.export_fields

    def test_actions_registered(self):
        """Verify custom actions are registered."""
        request = self.factory.get("/admin/")
        request.user = User(is_superuser=True)

        actions = self.admin.get_actions(request)
        assert "bulk_activate" in actions
        assert "bulk_deactivate" in actions
        assert "export_to_csv" in actions


class TestParkAreaAdmin(TestCase):
    """Tests for ParkAreaAdmin class."""

    def setUp(self):
        self.factory = RequestFactory()
        self.site = AdminSite()
        self.admin = ParkAreaAdmin(model=ParkArea, admin_site=self.site)

    def test_list_select_related(self):
        """Verify select_related for park."""
        assert "park" in self.admin.list_select_related

    def test_list_prefetch_related(self):
        """Verify prefetch_related for rides."""
        assert "rides" in self.admin.list_prefetch_related


class TestParkLocationAdmin(TestCase):
    """Tests for ParkLocationAdmin class."""

    def setUp(self):
        self.factory = RequestFactory()
        self.site = AdminSite()
        self.admin = ParkLocationAdmin(model=ParkLocation, admin_site=self.site)

    def test_list_select_related(self):
        """Verify select_related for park."""
        assert "park" in self.admin.list_select_related

    def test_readonly_coordinates(self):
        """Verify coordinate fields are readonly."""
        assert "latitude" in self.admin.readonly_fields
        assert "longitude" in self.admin.readonly_fields


class TestCompanyAdmin(TestCase):
    """Tests for CompanyAdmin class."""

    def setUp(self):
        self.factory = RequestFactory()
        self.site = AdminSite()
        self.admin = CompanyAdmin(model=Company, admin_site=self.site)

    def test_list_prefetch_related(self):
        """Verify prefetch_related for related parks."""
        assert "operated_parks" in self.admin.list_prefetch_related
        assert "owned_parks" in self.admin.list_prefetch_related


class TestParkReviewAdmin(TestCase):
    """Tests for ParkReviewAdmin class."""

    def setUp(self):
        self.factory = RequestFactory()
        self.site = AdminSite()
        self.admin = ParkReviewAdmin(model=ParkReview, admin_site=self.site)

    def test_list_select_related(self):
        """Verify select_related for user and park."""
        assert "park" in self.admin.list_select_related
        assert "user" in self.admin.list_select_related
        assert "moderated_by" in self.admin.list_select_related

    def test_moderation_actions_registered(self):
        """Verify moderation actions are registered."""
        request = self.factory.get("/admin/")
        request.user = User(is_superuser=True)

        actions = self.admin.get_actions(request)
        assert "bulk_approve" in actions
        assert "bulk_reject" in actions

    def test_readonly_moderation_fields(self):
        """Verify moderation fields are readonly."""
        assert "moderated_by" in self.admin.readonly_fields
        assert "moderated_at" in self.admin.readonly_fields
