from .models import *
from rest_framework import serializers

class ProductSerializer(serializers.ModelSerializer):
    class Meta:
        model = ProductModel
        fields = "__all__"
class ProductChatSerializer(serializers.ModelSerializer):
    class Meta:
        model = ProductModel
        fields = ['name', 'description', 'price', 'in_stock', 'discount', ]

class CommentSerializer(serializers.ModelSerializer):
    class Meta:
        model = CommentModel
        fields = "__all__"
        read_only_fields = ('show', 'product')


class CategorySerializer(serializers.ModelSerializer):
    children = serializers.SerializerMethodField()

    class Meta:
        model = CategoryModel
        fields = ['id', 'name', 'slug', 'icon', 'meta_title', 'meta_description', 'parent', 'children']

    def get_children(self, obj):
        children = obj.children.all()
        if children.exists():
            return CategorySerializer(children, many=True).data
        return []