from rest_framework import mixins, permissions, viewsets
from rest_framework.decorators import action
from rest_framework.response import Response

from . import models, serializers


class ProductPublicViewSet(viewsets.ReadOnlyModelViewSet):
    """Public storefront products."""

    queryset = models.Product.objects.filter(is_active=True).select_related("category")
    serializer_class = serializers.ProductSerializer
    permission_classes = [permissions.AllowAny]
    lookup_field = "slug"


class ProductAdminViewSet(viewsets.ModelViewSet):
    queryset = models.Product.objects.select_related("category").all()
    serializer_class = serializers.ProductSerializer
    permission_classes = [permissions.IsAuthenticated]


class ProductCategoryPublicViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = models.ProductCategory.objects.filter(is_active=True)
    serializer_class = serializers.ProductCategorySerializer
    permission_classes = [permissions.AllowAny]
    lookup_field = "slug"


class ProductCategoryAdminViewSet(viewsets.ModelViewSet):
    queryset = models.ProductCategory.objects.all()
    serializer_class = serializers.ProductCategorySerializer
    permission_classes = [permissions.IsAuthenticated]


class DeliveryZonePublicViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = models.DeliveryZone.objects.filter(is_active=True)
    serializer_class = serializers.DeliveryZoneSerializer
    permission_classes = [permissions.AllowAny]


class DeliveryZoneAdminViewSet(viewsets.ModelViewSet):
    queryset = models.DeliveryZone.objects.all()
    serializer_class = serializers.DeliveryZoneSerializer
    permission_classes = [permissions.IsAuthenticated]


class DeliveryOverrideCodeAdminViewSet(viewsets.ModelViewSet):
    queryset = models.DeliveryOverrideCode.objects.all()
    serializer_class = serializers.DeliveryOverrideCodeSerializer
    permission_classes = [permissions.IsAuthenticated]
    lookup_field = "code"

    @action(methods=["post"], detail=True, permission_classes=[permissions.IsAuthenticated])
    def activate(self, request, code=None):
        override_code = self.get_object()
        override_code.is_active = True
        override_code.save(update_fields=["is_active"])
        return Response(self.get_serializer(override_code).data)

    @action(methods=["post"], detail=True, permission_classes=[permissions.IsAuthenticated])
    def deactivate(self, request, code=None):
        override_code = self.get_object()
        override_code.is_active = False
        override_code.save(update_fields=["is_active"])
        return Response(self.get_serializer(override_code).data)


class OrderPublicViewSet(mixins.CreateModelMixin, viewsets.GenericViewSet):
    queryset = models.Order.objects.all()
    serializer_class = serializers.OrderSerializer
    permission_classes = [permissions.AllowAny]


class OrderAdminViewSet(viewsets.ModelViewSet):
    queryset = (
        models.Order.objects.select_related("delivery_override_code")
        .prefetch_related("items")
        .all()
    )
    serializer_class = serializers.OrderAdminSerializer
    permission_classes = [permissions.IsAuthenticated]
