from rest_framework import serializers
from .models import BlogModel, BlogCategoryModel
from account.models import User
from product.serializers import SubCategorySerializer

class AuthorSerializer(serializers.ModelSerializer):
    full_name = serializers.SerializerMethodField()
    class Meta:
        model = User
        fields = ['full_name', 'profile_photo']
    def get_full_name(self, obj):
        if obj.first_name and obj.last_name:
            return obj.first_name + ' ' + obj.last_name
        else:
            return 'ادمین وبسایت'

class BlogCategorySerilizer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField()
    class Meta:
        model = BlogCategoryModel
        fields = ['id', 'name']

    def get_name(self, obj):
        return obj.title
    


class BlogSerilizer(serializers.ModelSerializer):
    category = BlogCategorySerilizer()
    author = AuthorSerializer()
    class Meta:
        model = BlogModel
        exclude = ('is_published',)




class AllBlogSerilizer(serializers.ModelSerializer):
    author = AuthorSerializer()
    category = BlogCategorySerilizer()
    class Meta:
        model = BlogModel
        exclude = ('is_published', 'content',)


class AllBlogCategorySerilizer(serializers.ModelSerializer):
    class Meta:
        model = BlogCategoryModel
        fields = '__all__'

