from rest_framework.generics import ListAPIView, RetrieveAPIView
from rest_framework.pagination import PageNumberPagination

from blog.models import Post, Category
from blog.serializers import PostSerializer, CategorySerializer, CategoryPostSerializer
from core.utils.Permission import ReadOnly


class StandardResultsSetPagination(PageNumberPagination):
    page_size = 10
    page_size_query_param = 'page_size'
    max_page_size = 100


# Create your views here.
class CategoryList(ListAPIView):
    permission_classes = [ReadOnly]
    queryset = Category.objects.order_by('order').filter(is_active=True, parent__isnull=True).all()
    # serializer_class = ParentSerializer
    serializer_class = CategorySerializer


class CategoryDetail(RetrieveAPIView):
    permission_classes = [ReadOnly]
    queryset = Category.objects.order_by('order').filter(is_active=True).all()
    serializer_class = CategoryPostSerializer
    lookup_field = 'slug'  # Slug ile arama yapılacak

    def get_serializer_context(self):
        context = super().get_serializer_context()
        context['paginator'] = StandardResultsSetPagination()
        return context


# Create your views here.
class PostList(ListAPIView):
    permission_classes = [ReadOnly]
    queryset = Post.objects.all()
    serializer_class = PostSerializer
    pagination_class = StandardResultsSetPagination


class PostDetail(RetrieveAPIView):
    permission_classes = [ReadOnly]
    queryset = Post.objects.all()
    serializer_class = PostSerializer
    lookup_field = 'slug'  # Slug ile arama yapılacak
