from rest_framework import generics, permissions

from review import filters
from review import models
from review import serializers
from utils.permissions import IsReviewerManager, IsRestaurantReviewer
from review.serializers.back import ReviewBackSerializer


class ReviewLstView(generics.ListCreateAPIView):
    """Review list create view.

    status values:

    TO_INVESTIGATE = 0
    TO_REVIEW = 1
    READY = 2
    """
    serializer_class = ReviewBackSerializer
    queryset = models.Review.objects.all()
    permission_classes = [permissions.IsAuthenticatedOrReadOnly, ]
    filterset_class = filters.ReviewFilter


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

    status values:

    TO_INVESTIGATE = 0
    TO_REVIEW = 1
    READY = 2
    """
    serializer_class = ReviewBackSerializer
    queryset = models.Review.objects.all()
    permission_classes = [permissions.IsAdminUser | IsReviewerManager | IsRestaurantReviewer]
    lookup_field = 'id'


class InquiriesLstView(generics.ListCreateAPIView):
    """Inquiries list create view."""

    serializer_class = serializers.InquiriesBaseSerializer
    queryset = models.Inquiries.objects.all()
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)

    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(generics.RetrieveUpdateDestroyAPIView):
    """Inquiries RUD view."""
    serializer_class = serializers.InquiriesBaseSerializer
    queryset = models.Inquiries.objects.all()
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
    lookup_field = 'id'


class GridItemsLstView(generics.ListCreateAPIView):
    """GridItems list create view."""
    serializer_class = serializers.GridItemsBaseSerializer
    queryset = models.GridItems.objects.all()
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)

    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(generics.RetrieveUpdateDestroyAPIView):
    """GridItems RUD view."""
    serializer_class = serializers.GridItemsBaseSerializer
    queryset = models.GridItems.objects.all()
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
    lookup_field = 'id'
