from rest_framework import generics

from review import filters
from review import models
from review import serializers
from review.serializers.back import ReviewBackSerializer
from utils.methods import get_permission_classes
from utils.permissions import (
    IsReviewManager, IsRestaurantInspector, IsWineryWineInspector,
    IsArtisanInspector, IsProducerFoodInspector, IsDistilleryLiquorInspector,
)


class PermissionMixinView:
    """Permission mixin view."""
    permission_classes = get_permission_classes(
        IsReviewManager, IsRestaurantInspector, IsWineryWineInspector,
        IsArtisanInspector, IsProducerFoodInspector, IsDistilleryLiquorInspector,
    )


class ReviewMixinView:
    """Review mixin."""
    queryset = models.Review.objects.with_base_related()


class ReviewListView(PermissionMixinView, ReviewMixinView, generics.ListCreateAPIView):
    """Review list create view.

    status values:

    TO_INVESTIGATE = 0
    TO_REVIEW = 1
    READY = 2
    """
    serializer_class = ReviewBackSerializer
    filterset_class = filters.ReviewFilter


class ReviewRUDView(PermissionMixinView, ReviewMixinView, generics.RetrieveUpdateDestroyAPIView):
    """Review RUD view.

    status values:

    TO_INVESTIGATE = 0
    TO_REVIEW = 1
    READY = 2
    """
    serializer_class = ReviewBackSerializer
    lookup_field = 'id'


class InquiriesListView(PermissionMixinView, generics.ListCreateAPIView):
    """Inquiries list create view."""

    serializer_class = serializers.InquiriesBaseSerializer
    queryset = models.Inquiries.objects.all()

    def get_queryset(self):
        review_id = self.kwargs.get('review_id')
        if review_id:
            return super().get_queryset().filter(review_id=review_id)
        return super().get_queryset()


class InquiriesRUDView(PermissionMixinView, generics.RetrieveUpdateDestroyAPIView):
    """Inquiries RUD view."""
    serializer_class = serializers.InquiriesBaseSerializer
    queryset = models.Inquiries.objects.all()
    lookup_field = 'id'


class GridItemsListView(PermissionMixinView, generics.ListCreateAPIView):
    """GridItems list create view."""
    serializer_class = serializers.GridItemsBaseSerializer
    queryset = models.GridItems.objects.all()

    def get_queryset(self):
        inquiry_id = self.kwargs.get('inquiry_id')
        if inquiry_id:
            return super().get_queryset().filter(inquiry_id=inquiry_id)
        return super().get_queryset()


class GridItemsRUDView(PermissionMixinView, generics.RetrieveUpdateDestroyAPIView):
    """GridItems RUD view."""
    serializer_class = serializers.GridItemsBaseSerializer
    queryset = models.GridItems.objects.all()
    lookup_field = 'id'
