from rest_framework import serializers
# from product.models import Category, Product, Images, Tags
from settings.models import Setting

"""
title
content
categories
keywords
brim
price
tags
gallery
images
thumb
video
slug
created_at
updated_at
is_active
is_front
"""

"""
class CateSerializer(serializers.ModelSerializer):
    class Meta:
        model = Category
        fields = ['title', 'parent', 'is_active', 'created_at', 'order', 'slug', 'images', 'keywords', 'description']


class GalSerializer(serializers.ModelSerializer):
    class Meta:
        model = Images
        fields = ['title', 'images']


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


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)

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


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

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

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

"""


class SettingSerializer(serializers.ModelSerializer):
    b_logo = serializers.SerializerMethodField()
    w_logo = serializers.SerializerMethodField()

    class Meta:
        model = Setting
        fields = ['title', 'meta_title', 'meta_description', 'phone', 'url', 'email', 'facebook', 'x',
                  'instagram', 'whatsapp', 'slogan', 'w_logo', 'b_logo', 'created_at',
                  'updated_at', 'is_active']

    def get_w_logo(self, obj):
        if obj.w_logo:
            request = self.context.get('request')
            if request:
                return request.build_absolute_uri(obj.w_logo.url)
            else:
                # Fallback olarak manuel URL oluşturma
                return f"http://127.0.0.1:8000{obj.w_logo.url}"
        return None
    def get_b_logo(self, obj):
        if obj.b_logo:
            request = self.context.get('request')
            if request:
                return request.build_absolute_uri(obj.b_logo.url)
            else:
                # Fallback olarak manuel URL oluşturma
                return f"http://127.0.0.1:8000{obj.b_logo.url}"
        return None
