from rest_framework import serializers
from apps.product import models as product_models
from apps.authorization.api.v1 import serializers as authorize_serializers
from apps.authentication.api.v1.serializers.serializer import OrganizationSerializer


class ProductCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = product_models.ProductCategory
        fields = '__all__'


class ProductSerializer(serializers.ModelSerializer):
    """ Serializer of product """

    class Meta:
        model = product_models.Product
        fields = '__all__'

    def to_representation(self, instance):
        """ Custom output of product serializer """

        representation = super().to_representation(instance)
        if instance.category:
            representation['category'] = {
                'id': instance.category.id,
                'name': instance.category.name
            }

        return representation


class AttributeSerializer(serializers.ModelSerializer):
    """ serialize attributes of reference product """

    class Meta:
        model = product_models.Attribute
        fields = '__all__'

    def to_representation(self, instance):
        representation = super().to_representation(instance)
        if instance.product:
            representation['product'] = {
                'id': instance.product.id,
                'name': instance.product.name
            }

        if instance.type:
            representation['type'] = {
                'id': instance.type.id,
                'unit': instance.type.unit
            }
        return representation


class AttributeValueSerializer(serializers.ModelSerializer):
    """ serialize attribute values for child products """

    class Meta:
        model = product_models.AttributeValue
        fields = '__all__'

    def update(self, instance, validated_data):

        instance.quota = validated_data.get('quota', instance.quota)
        instance.attribute = validated_data.get('attribute', instance.attribute)
        instance.value = validated_data.get('value', instance.value)
        instance.save()

        return instance


class BrokerSerializer(serializers.ModelSerializer):
    """ serialize product broker """

    class Meta:
        model = product_models.Broker
        fields = '__all__'
        depth = 0

    def to_representation(self, instance):
        representation = super().to_representation(instance)
        if instance.organization:
            representation['organization'] = OrganizationSerializer(
                instance.organization
            ).data

        if instance.product:
            representation['product'] = ProductSerializer(
                instance.product
            ).data

        if instance.calculation_strategy:
            representation['calculation_strategy'] = {
                'id': instance.calculation_strategy.id,
                'unit': instance.calculation_strategy.unit
            }

        return representation


class SaleUnitSerializer(serializers.ModelSerializer):
    """ serialize unit of products for sale """

    class Meta:
        model = product_models.SaleUnit
        fields = '__all__'

    def to_representation(self, instance):
        representation = super().to_representation(instance)
        if instance.product:
            representation['product'] = {
                'id': instance.product.id,
                'name': instance.product.name
            }
        return representation


class IncentivePlanSerializer(serializers.ModelSerializer):  # noqa
    class Meta:
        model = product_models.IncentivePlan
        fields = '__all__'
