from rest_framework import serializers
from product.models import Category, Product, Images, Tags, ProductTree


class CateSerializer(serializers.ModelSerializer):
    images = serializers.SerializerMethodField()

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

    def get_images(self, obj):
        if obj.images:
            return obj.images.url
        return None


class GalSerializer(serializers.ModelSerializer):
    images = serializers.SerializerMethodField()

    class Meta:
        model = Images
        fields = ['title', 'images']

    def get_images(self, obj):
        if obj.images:
            return obj.images.url
        return None


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


class ProductTreeSerializer(serializers.ModelSerializer):
    images = serializers.SerializerMethodField()
    categories = CateSerializer(read_only=True, many=True)

    class Meta:
        model = ProductTree
        fields = ['id', 'title', 'button', 'content', 'categories', 'price', 'images', 'created_at', 'updated_at', 'is_active', 'is_front']

    def get_images(self, obj):
        if obj.images:
            return obj.images.url
        return None


class ProductSerializer(serializers.ModelSerializer):
    categories = CateSerializer(read_only=True, many=True)
    gallery = GalSerializer(read_only=True, many=True)
    tags = TagSerializer(read_only=True, many=True)
    images = serializers.SerializerMethodField()
    thumb = serializers.SerializerMethodField()
    
    # RateableMixin'den gelen property'leri buraya ekliyoruz
    average_rating = serializers.FloatField(read_only=True)
    rating_count = serializers.IntegerField(read_only=True)

    class Meta:
        model = Product
        fields = ['id', 'title', 'content', 'categories', 'keywords', 'brim', 'tags', 'gallery', 'images', 'thumb', 'video',
                  'slug', 'created_at', 'updated_at', 'is_active', 'is_front', 'price', 'average_rating', 'rating_count']

    def get_images(self, obj):
        if obj.images:
            return obj.images.url
        return None

    def get_thumb(self, obj):
        try:
            if obj.thumb:
                return obj.thumb.url
        except Exception:
            # Thumbnail oluşturulamadıysa veya bir hata olursa ana resmi dene
            if obj.images:
                return obj.images.url
        return None


class CategorySerializer(serializers.ModelSerializer):
    categories = ProductSerializer(read_only=True, many=True)
    child = serializers.SerializerMethodField()
    images = serializers.SerializerMethodField()

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

    def get_images(self, obj):
        if obj.images:
            return obj.images.url
        return None

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