from rest_framework import serializers

from blog.models import Category, Post, Tags


class CateSerializer(serializers.ModelSerializer):
    parent = serializers.StringRelatedField()  # ID yerine __str__ metodundaki değeri döndürür

    class Meta:
        model = Category
        fields = ['title', 'parent', 'is_active', 'created_at', 'order', 'slug', 'image', 'keywords', 'description']


class TagSerializer(serializers.ModelSerializer):
    class Meta:
        model = Tags
        fields = ['tag', 'slug']


class PostSerializer(serializers.ModelSerializer):
    categories = CateSerializer(read_only=True, many=True)
    # Tags için sadece tag ismini döndürmek daha temiz olabilir, ama mevcut yapıyı koruyalım
    # Eğer sadece isim listesi istenirse: tags = serializers.SlugRelatedField(many=True, read_only=True, slug_field='tag')
    tags = TagSerializer(read_only=True, many=True)

    class Meta:
        model = Post
        fields = ['title', 'content', 'categories', 'keywords', 'tags', 'image', 'thumb', 'video',
                  'slug', 'created_at', 'updated_at', 'is_active', 'is_front']
        # fields = '__all__'


class PostSYalinerializer(serializers.ModelSerializer):
    class Meta:
        model = Post
        fields = ['slug', ]
        # fields = '__all__'


class CategorySerializer(serializers.ModelSerializer):

    posts = PostSYalinerializer(source='c_categories', read_only=True, many=True)
    child = serializers.SerializerMethodField()

    class Meta:
        model = Category
        fields = ['title', 'parent', 'is_active', 'created_at', 'order', 'slug', 'image', 'keywords', 'description',
                  'posts', 'child']

    def get_child(self, obj):
        serializer = self.__class__(obj.child.all(), many=True, context=self.context)
        return serializer.data


class CategoryPostSerializer(serializers.ModelSerializer):

    posts = serializers.SerializerMethodField()
    child = serializers.SerializerMethodField()

    class Meta:
        model = Category
        fields = ['title', 'parent', 'is_active', 'created_at', 'order', 'slug', 'image', 'keywords', 'description',
                  'posts', 'child']

    def get_posts(self, obj):
        # Pagination context'ini al
        paginator = self.context.get('paginator')
        request = self.context.get('request')

        posts = obj.c_categories.all()

        if paginator and request:
            # Pagination uygula
            paginated_posts = paginator.paginate_queryset(posts, request)
            serializer = PostSerializer(paginated_posts, many=True, context=self.context)
            return {
                'results': serializer.data,
                'count': posts.count(),
                'next': paginator.get_next_link(),
                'previous': paginator.get_previous_link(),
            }
        else:
            # Pagination yoksa normal döndür
            serializer = PostSerializer(posts, many=True, context=self.context)
            return serializer.data

    def get_child(self, obj):
        serializer = self.__class__(obj.child.all(), many=True, context=self.context)
        return serializer.data