from apps.authentication.api.v1.serializers import serializer as auth_serializers
from rest_framework import serializers
from apps.tag import models as tag_models


class TagSerializer(serializers.ModelSerializer):
    """ Tag Model Serializer """

    class Meta:
        model = tag_models.Tag
        fields = [
            'id',
            'country_code',
            'static_code',
            'ownership_code',
            'species_code',
            'serial',
            'tag_code',
            'organization',
            'status',
        ]
        extra_kwargs = {
            'serial': {
                'required': False
            }
        }

    def update(self, instance, validated_data):
        """ update tag information """

        instance.country_code = validated_data.get(
            'country_code', instance.country_code
        )
        instance.static_code = validated_data.get(
            'static_code', instance.static_code
        )
        instance.ownership_code = validated_data.get(
            'ownership_code', instance.ownership_code
        )
        instance.species_code = validated_data.get(
            'species_code', instance.species_code
        )
        instance.serial = validated_data.get(
            'serial', instance.serial
        )
        instance.tag_code = validated_data.get(
            'tag_code', instance.tag_code
        )
        if validated_data.get('organization'):
            instance.organization = tag_models.auth_models.Organization.objects.get(
                id=validated_data.get('organization', instance.organization).id
            )
        instance.status = validated_data.get('status', instance.status)
        instance.save()
        return instance

    def to_representation(self, instance):
        """ Customize output of serializer """
        representation = super().to_representation(instance)
        if isinstance(instance, tag_models.Tag):
            representation['organization'] = auth_serializers.OrganizationSerializer(instance.organization).data
        return representation


class TagAssignmentSerializer(serializers.ModelSerializer):
    """ assigned tag serializer """

    class Meta:
        model = tag_models.TagAssignment
        fields = [
            'id',
            'organization',
            'document',
            'serial',
        ]

    def to_representation(self, instance):
        """ custom output for serializer """
        representation = super().to_representation(instance)
        representation['serial'] = instance.serial
        representation['document'] = instance.document
        representation['organization'] = auth_serializers.OrganizationSerializer(
            instance.organization
        ).data
        representation['allocated_tags'] = AllocatedTagsSerializer(
            instance.assigned_allocated_tags.all(), many=True
        ).data
        return representation


class AllocatedTagsSerializer(serializers.ModelSerializer):
    """ allocated tags serializer """

    class Meta:
        model = tag_models.AllocatedTags
        fields = [
            'id',
            'tag',
            'assignment',
            'status'
        ]

    def to_representation(self, instance):
        """ custom output for serializer """
        representation = super().to_representation(instance)
        representation['tag'] = TagSerializer(instance.tag).data
        representation['status'] = instance.status

        return representation
