"""
Tests for rides admin interfaces.

These tests verify the functionality of ride, model, stats, company,
review, and ranking 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.rides.admin import (
    CompanyAdmin,
    RankingSnapshotAdmin,
    RideAdmin,
    RideModelAdmin,
    RidePairComparisonAdmin,
    RideRankingAdmin,
    RideReviewAdmin,
    RollerCoasterStatsAdmin,
)
from apps.rides.models.company import Company
from apps.rides.models.rankings import RankingSnapshot, RidePairComparison, RideRanking
from apps.rides.models.reviews import RideReview
from apps.rides.models.rides import Ride, RideModel, RollerCoasterStats

User = get_user_model()


class TestRideAdmin(TestCase):
    """Tests for RideAdmin class."""

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

    def test_list_display_fields(self):
        """Verify all required fields are in list_display."""
        required_fields = [
            "name",
            "park_link",
            "category_badge",
            "manufacturer_link",
            "status_badge",
        ]
        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 "park" in self.admin.list_select_related
        assert "manufacturer" in self.admin.list_select_related
        assert "designer" in self.admin.list_select_related
        assert "ride_model" in self.admin.list_select_related

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

    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
        assert "category" in self.admin.export_fields

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

        actions = self.admin.get_actions(request)
        assert "bulk_set_operating" in actions
        assert "bulk_set_closed" in actions
        assert "bulk_set_sbno" in actions


class TestRideModelAdmin(TestCase):
    """Tests for RideModelAdmin class."""

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

    def test_list_select_related(self):
        """Verify select_related for manufacturer."""
        assert "manufacturer" 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 TestRollerCoasterStatsAdmin(TestCase):
    """Tests for RollerCoasterStatsAdmin class."""

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

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


class TestRideReviewAdmin(TestCase):
    """Tests for RideReviewAdmin class."""

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

    def test_list_select_related(self):
        """Verify select_related for ride, park, and user."""
        assert "ride" in self.admin.list_select_related
        assert "ride__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
        assert "flag_for_review" in actions


class TestRideRankingAdmin(TestCase):
    """Tests for RideRankingAdmin class."""

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

    def test_readonly_permissions(self):
        """Verify read-only permissions are set."""
        request = self.factory.get("/admin/")
        request.user = User(is_superuser=False)

        assert self.admin.has_add_permission(request) is False
        assert self.admin.has_change_permission(request) is False

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


class TestRidePairComparisonAdmin(TestCase):
    """Tests for RidePairComparisonAdmin class."""

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

    def test_readonly_permissions(self):
        """Verify read-only permissions are set."""
        request = self.factory.get("/admin/")
        request.user = User(is_superuser=False)

        assert self.admin.has_add_permission(request) is False
        assert self.admin.has_change_permission(request) is False

    def test_list_select_related(self):
        """Verify select_related for both rides."""
        assert "ride_a" in self.admin.list_select_related
        assert "ride_b" in self.admin.list_select_related


class TestRankingSnapshotAdmin(TestCase):
    """Tests for RankingSnapshotAdmin class."""

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

    def test_readonly_permissions(self):
        """Verify read-only permissions are set."""
        request = self.factory.get("/admin/")
        request.user = User(is_superuser=False)

        assert self.admin.has_add_permission(request) is False
        assert self.admin.has_change_permission(request) is False


class TestCompanyAdmin(TestCase):
    """Tests for rides 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 manufactured rides."""
        assert "manufactured_rides" in self.admin.list_prefetch_related
        assert "designed_rides" in self.admin.list_prefetch_related
