from rest_framework import serializers
from .models import Orders, OrderProducts,Ads, Discount, Coupons, OrderDelivery, SomeOneElse
from django.urls import reverse
from products.models import ProductImage, Products, CustomProductImage
from products.serializers import CustomProductSerializer



class SomeOneElseDataSerializer(serializers.ModelSerializer):
    class Meta:
        model = SomeOneElse
        fields = ["name", "phone_number"]


class OrderProductSerializer(serializers.ModelSerializer):
    sku_name = serializers.CharField(source="sku.sku_name")
    sku_quantity = serializers.CharField(source="sku.sku_quantity")
    sku_unit = serializers.CharField(source="sku.sku_unit")
    veg_or_non_veg = serializers.CharField(source="sku.product.veg_or_non_veg_status")

    class Meta:
        model = OrderProducts
        fields = [
            "sku",
            "quantity",
            "price",
            "sku_name",
            "veg_or_non_veg",
            "sku_quantity",
            "sku_unit",
        ]


class OrderSerializer(serializers.ModelSerializer):
    order_data = OrderProductSerializer(many=True, read_only=True)
    shop_name = serializers.CharField(source="store_uuid.unit_name")
    shop_location = serializers.CharField(source="store_uuid.unit_location")
    shop_contact = serializers.CharField(source="store_uuid.contact_no")
    address_type = serializers.CharField(source="drop_address.address_type", allow_null=True,required=False)
    address_1 = serializers.CharField(source="drop_address.house_number_or_name", allow_null=True, required=False)
    address_2 = serializers.CharField(source="drop_address.land_mark", allow_null=True, required=False)
    some_one_else_order = SomeOneElseDataSerializer(read_only=True)
    class Meta:
        model = Orders
        fields = [
            "uuid",
            "order_ID",
            "order_type",
            "order_status",
            "sub_total",
            "taxes_and_charges",
            "delivery_charges",
            "grand_total",
            "order_data",
            "created_date",
            "shop_name",
            "shop_location",
            "shop_contact",
            "delivery_slot_date",
            "delivery_slot_time",
            "address_type",
            "address_1",
            "address_2",
            "description",
            "message",
            "some_one_else_order"
        ]
    def to_representation(self, instance):
        representation = super().to_representation(instance)
        if representation.get('some_one_else_order') is None:
            representation['some_one_else_order'] = {}  # return empty object instead of null
        return representation

class CourierDetailsSerializer(serializers.ModelSerializer):
    class Meta:
        model = OrderDelivery
        fields = ["courier_service_name", "package_number", "expected_date_of_delivery", "tracking_link"]


class OrderSerializer2(serializers.ModelSerializer):
    order_data = OrderProductSerializer(many=True, read_only=True)
    pu_name = serializers.CharField(source="pu_uuid.pu_name")
    pu_location = serializers.CharField(source="pu_uuid.pu_location")
    pu_contact = serializers.CharField(source="pu_uuid.contact_no")
    address_type = serializers.CharField(source="drop_address.address_type", allow_null=True,required=False)
    address_1 = serializers.CharField(source="drop_address.house_number_or_name", allow_null=True, required=False)
    address_2 = serializers.CharField(source="drop_address.land_mark", allow_null=True, required=False)
    some_one_else_order = SomeOneElseDataSerializer(read_only=True)
    class Meta:
        model = Orders
        fields = [
            "uuid",
            "order_ID",
            "order_type",
            "order_status",
            "sub_total",
            "taxes_and_charges",
            "delivery_charges",
            "grand_total",
            "order_data",
            "created_date",
            "pu_name",
            "pu_location",
            "pu_contact",
            "delivery_slot_date",
            "delivery_slot_time",
            "address_type",
            "address_1",
            "address_2",
            "some_one_else_order"
        ]
    def to_representation(self, instance):
        representation = super().to_representation(instance)
        if representation.get('some_one_else_order') is None:
            representation['some_one_else_order'] = {}  # return empty object instead of null
        return representation

class CustomOrderSerializer(serializers.ModelSerializer):
    shop_name = serializers.CharField(source="store_uuid.unit_name")
    shop_location = serializers.CharField(source="store_uuid.unit_location")
    shop_contact = serializers.CharField(source="store_uuid.contact_no")
    address_type = serializers.CharField(source="drop_address.address_type", allow_null=True,required=False)
    address_1 = serializers.CharField(source="drop_address.house_number_or_name", allow_null=True, required=False)
    address_2 = serializers.CharField(source="drop_address.land_mark", allow_null=True, required=False)
    custom_product = CustomProductSerializer(read_only = True)
    some_one_else_order = SomeOneElseDataSerializer(read_only=True)
    class Meta:
        model = Orders
        fields = [
            "uuid",
            "order_ID",
            "order_type",
            "order_status",
            "sub_total",
            "taxes_and_charges",
            "delivery_charges",
            "grand_total",
            "created_date",
            "shop_name",
            "shop_location",
            "shop_contact",
            "delivery_slot_date",
            "delivery_slot_time",
            "address_type",
            "address_1",
            "address_2",
            "custom_product",
            "description",
            "message",
            "some_one_else_order"
        ]
    def to_representation(self, instance):
        representation = super().to_representation(instance)
        if representation.get('some_one_else_order') is None:
            representation['some_one_else_order'] = {}  # return empty object instead of null
        return representation

    # def get_product_image(self, obj):
    #     product_image_custom = CustomProductImage.objects.filter(
    #         custom_product=obj.custom_product
    #     ).first()
    #     if product_image_custom:
    #         return product_image_custom.image.url
    #     return None




class PastOrderListSerializer(serializers.ModelSerializer):
    order_ID = serializers.CharField()
    delivery_slot_date = serializers.DateField()
    delivery_slot_time = serializers.TimeField(required=False, allow_null=True)
    grand_total = serializers.FloatField()
    order_status = serializers.CharField()
    order_type = serializers.CharField()
    product_names = serializers.SerializerMethodField()
    product_image = serializers.SerializerMethodField()
    address_type = serializers.CharField(source="drop_address.address_type", allow_null=True,required=False)

    class Meta:
        model = Orders
        fields = [
            "uuid",
            "order_ID",
            "delivery_slot_date",
            "delivery_slot_time",
            "grand_total",
            "order_status",
            "order_type",
            "product_names",
            "product_image",
            "address_type",
            "description"
        ]

    def get_product_names(self, obj):
        if obj.order_type == "Custom Orders":
            return [obj.custom_product.item_name]
        return [order_product.product_name for order_product in obj.order_data.all()]

    def get_product_image(self, obj):
        if obj.order_type == "Custom Orders":
            product_image_custom = CustomProductImage.objects.filter(
                custom_product=obj.custom_product
            ).first()
            if product_image_custom:
                return product_image_custom.image.url
        for order_product in obj.order_data.all():
            product_image = ProductImage.objects.filter(
                product=order_product.sku.product
            ).first()
            if product_image:
                return product_image.image.url
        return None

    def to_representation(self, instance):
        data = super().to_representation(instance)

        if not data.get("delivery_slot_time"):
            data["delivery_slot_time"] = "Not Provided"

        return data

class ProductSerializer(serializers.ModelSerializer):
    class Meta:
        model = Products
        fields = ["id", "item_name", "item_code"]

class CouponSerializer(serializers.ModelSerializer):
    class Meta:
        model = Coupons
        fields = ["id", "CouponName", "CouponCode"]

class DiscountSerializer(serializers.ModelSerializer):
    class Meta:
        model = Discount
        fields = ["id", "DiscountName", "DiscountCode"]



class AdSerializer(serializers.ModelSerializer):
    AdTitle = serializers.CharField()
    AdDescription = serializers.CharField()
    AdPlacement = serializers.CharField()
    AdType = serializers.CharField()
    StandardImage = serializers.ImageField()
    BannerImage = serializers.ImageField()
    Discount = DiscountSerializer()
    Coupon = CouponSerializer()
    Product = ProductSerializer()

    class Meta:
        model = Ads
        fields = [
            "AdTitle",
            "AdDescription",
            "AdPlacement",
            "AdType",
            "StandardImage",
            "BannerImage",
            "Discount",
            "Coupon",
            "Product"
        ]
class DiscountDetailSerializer(serializers.ModelSerializer):
    class Meta:
        model = Discount
        fields = ["id", "DiscountName", "DiscountCode", "DiscountDescription", "DiscountOn", "ApplicableCategory", "ApplicableSubCategory", "ApplicableProduct", "ApplicableSku", "DiscountPercentage", "StandardImage", "BannerImage"]


class OrderBillDetailsSerializer(serializers.ModelSerializer):
    class Meta:
        model = Orders
        fields = ["sub_total","taxes_and_charges","delivery_charges","grand_total", "total_savings", "coupon_savings", "discount"]



