"""Views for app favorites."""
from rest_framework import generics
from establishment.models import Establishment
from establishment.filters import EstablishmentFilter
from establishment.serializers import EstablishmentBaseSerializer, EstablishmentSimilarSerializer
from news.filters import NewsListFilterSet
from news.models import News
from news.serializers import NewsBaseSerializer, NewsListSerializer
from product.models import Product
from product.serializers import ProductBaseSerializer
from product.filters import ProductFilterSet
from .models import Favorites


class FavoritesBaseView(generics.GenericAPIView):
    """Base view for Favorites."""

    def get_queryset(self):
        """Override get_queryset method."""
        return Favorites.objects.by_user(self.request.user)


class FavoritesEstablishmentListView(generics.ListAPIView):
    """List views for establishments in favorites."""

    serializer_class = EstablishmentSimilarSerializer
    filter_class = EstablishmentFilter

    def get_queryset(self):
        """Override get_queryset method"""
        return Establishment.objects.filter(favorites__user=self.request.user) \
            .order_by('-favorites').with_base_related() \
            .with_certain_tag_category_related('category', 'restaurant_category') \
            .with_certain_tag_category_related('cuisine', 'restaurant_cuisine') \
            .with_certain_tag_category_related('shop_category', 'artisan_category')


class FavoritesProductListView(generics.ListAPIView):
    """List views for products in favorites."""

    serializer_class = ProductBaseSerializer
    filter_class = ProductFilterSet

    def get_queryset(self):
        """Override get_queryset method"""
        return Product.objects.filter(favorites__user=self.request.user) \
            .order_by('-favorites')


class FavoritesNewsListView(generics.ListAPIView):
    """List views for news in favorites."""

    serializer_class = NewsListSerializer
    filter_class = NewsListFilterSet

    def get_queryset(self):
        """Override get_queryset method"""
        return News.objects.filter(favorites__user=self.request.user).order_by('-favorites')
