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


class QuotaSerializer(serializers.ModelSerializer):
    class Meta:
        model = product_models.Quota
        fields = '__all__'
        depth = 0

    def to_representation(self, instance):
        representation = super().to_representation(instance)
        if isinstance(instance, product_models.Quota):
            representation['incentive_plan'] = QuotaIncentiveAssignmentSerializer(
                instance.incentive_assignments.all(),
                many=True
            ).data

            representation['attribute_values'] = product_serializers.AttributeValueSerializer(
                instance.attribute_values.all(),
                many=True
            ).data

            representation['brokers'] = QuotaBrokerValueSerializer(
                instance.broker_values.all(),
                many=True
            ).data

            representation['livestock_allocations'] = QuotaLiveStockAllocationSerializer(
                instance.livestock_allocations.all(),
                many=True
            ).data

            representation['livestock_limitations'] = QuotaLiveStockAgeLimitationSerializer(
                instance.livestock_age_limitations.all(),
                many=True
            ).data

        return representation

    def update(self, instance, validated_data):
        """ Custom Update """

        instance.quota_id = validated_data.get('quota_id', instance.quota_id)
        instance.quota_code = validated_data.get('quota_code', instance.quota_code)
        instance.quota_weight = validated_data.get('quota_weight', instance.quota_weight)
        instance.remaining_weight = validated_data.get('remaining_weight', instance.remaining_weight)
        instance.quota_distributed = validated_data.get('quota_distributed', instance.quota_distributed)
        instance.quota_balance = validated_data.get('quota_balance', instance.quota_balance)
        instance.product = validated_data.get('product', instance.product)
        instance.sale_type = validated_data.get('sale_type', instance.sale_type)
        instance.sale_unit = validated_data.get('sale_unit', instance.sale_type)
        instance.month_choices = validated_data.get('month_choices', instance.month_choices)
        instance.sale_license = validated_data.get('sale_license', instance.sale_license)
        instance.group = validated_data.get('group', instance.group)
        instance.has_distribution_limit = validated_data.get('has_distribution_limit', instance.has_distribution_limit)
        instance.distribution_mode = validated_data.get('distribution_mode', instance.distribution_mode)
        instance.base_price_factory = validated_data.get('base_price_factory', instance.base_price_factory)
        instance.base_price_cooperative = validated_data.get('base_price_cooperative', instance.base_price_cooperative)
        instance.final_price = validated_data.get('final_price', instance.final_price)
        instance.is_closed = validated_data.get('is_closed', instance.is_closed)
        instance.closed_at = validated_data.get('closed_at', instance.closed_at)
        instance.save()

        instance.assigned_organizations.clear()
        instance.assigned_organizations.add(
            *(validated_data.get('assigned_organizations', instance.assigned_organizations))
        )

        return instance


class QuotaIncentiveAssignmentSerializer(serializers.ModelSerializer):
    class Meta:
        model = product_models.QuotaIncentiveAssignment
        fields = '__all__'

    def update(self, instance, validated_data):
        """ Custom Update """

        instance.quota = validated_data.get('quota', instance.quota)
        instance.incentive_plan = validated_data.get('incentive_plan', instance.incentive_plan)
        instance.heavy_value = validated_data.get('heavy_value', instance.heavy_value)
        instance.light_value = validated_data.get('light_value', instance.light_value)
        instance.save()

        return instance


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

    def update(self, instance, validated_data):
        """ Custom Update """

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

        return instance


class QuotaLiveStockAllocationSerializer(serializers.ModelSerializer):
    class Meta:
        model = product_models.QuotaLivestockAllocation
        fields = '__all__'
        extra_kwargs = {
            'livestock_group': {
                'required': False
            },
            'livestock_type': {
                'required': False
            },
            'livestock_subtype': {
                'required': False
            }
        }

    def update(self, instance, validated_data):
        """ Custom Update """

        instance.quota = validated_data.get('quota', instance.quota)
        instance.livestock_group = validated_data.get('livestock_group', instance.livestock_group)
        instance.livestock_type = validated_data.get('livestock_type', instance.livestock_type)
        instance.livestock_subtype = validated_data.get('livestock_subtype', instance.livestock_subtype)
        instance.save()

        return instance


class QuotaLiveStockAgeLimitationSerializer(serializers.ModelSerializer):
    class Meta:
        model = product_models.QuotaLiveStockAgeLimitation
        fields = '__all__'

    def update(self, instance, validated_data):
        """ Custom Update """

        instance.quota = validated_data.get('quota', instance.quota)
        instance.livestock_type = validated_data.get('livestock_type', instance.livestock_type)
        instance.livestock_subtype = validated_data.get('livestock_subtype', instance.livestock_subtype)
        instance.age_month = validated_data.get('age_month', instance.age_month)
        instance.save()

        return instance
