"""
Base view classes with common patterns.

This module provides base view classes that implement common patterns
such as automatic query optimization with select_related and prefetch_related.
"""

from django.db.models import QuerySet
from django.views.generic import DetailView, ListView


class OptimizedListView(ListView):
    """
    ListView with automatic query optimization.

    Automatically applies select_related and prefetch_related based on
    class attributes, reducing the need for boilerplate code in get_queryset.

    Attributes:
        select_related_fields: List of fields to pass to select_related()
        prefetch_related_fields: List of fields to pass to prefetch_related()

    Example:
        class RideListView(OptimizedListView):
            model = Ride
            select_related_fields = ['park', 'manufacturer']
            prefetch_related_fields = ['photos']
    """

    select_related_fields: list[str] = []
    prefetch_related_fields: list[str] = []

    def get_queryset(self) -> QuerySet:
        """Get queryset with optimizations applied."""
        queryset = super().get_queryset()

        if self.select_related_fields:
            queryset = queryset.select_related(*self.select_related_fields)

        if self.prefetch_related_fields:
            queryset = queryset.prefetch_related(*self.prefetch_related_fields)

        return queryset


class OptimizedDetailView(DetailView):
    """
    DetailView with automatic query optimization.

    Automatically applies select_related and prefetch_related based on
    class attributes, reducing the need for boilerplate code in get_queryset.

    Attributes:
        select_related_fields: List of fields to pass to select_related()
        prefetch_related_fields: List of fields to pass to prefetch_related()

    Example:
        class RideDetailView(OptimizedDetailView):
            model = Ride
            select_related_fields = ['park', 'park__location', 'manufacturer']
            prefetch_related_fields = ['photos', 'coaster_stats']
    """

    select_related_fields: list[str] = []
    prefetch_related_fields: list[str] = []

    def get_queryset(self) -> QuerySet:
        """Get queryset with optimizations applied."""
        queryset = super().get_queryset()

        if self.select_related_fields:
            queryset = queryset.select_related(*self.select_related_fields)

        if self.prefetch_related_fields:
            queryset = queryset.prefetch_related(*self.prefetch_related_fields)

        return queryset
