from apps.authentication.api.v1.serializers.serializer import (
    UserSerializer,
    OrganizationSerializer,
    ProvinceSerializer,
    CitySerializer
)
from rest_framework import serializers
from apps.herd.models import Herd


class HerdSerializer(serializers.ModelSerializer):
    """ Herd Serializer """
    class Meta:
        model = Herd
        fields = '__all__'

    def to_representation(self, instance):
        """ Customize serializer output """
        representation = super().to_representation(instance)
        if isinstance(instance, Herd):
            representation['owner'] = UserSerializer(instance.owner).data
            representation['cooperative'] = OrganizationSerializer(instance.cooperative).data
            representation['province'] = ProvinceSerializer(instance.province).data
            representation['city'] = CitySerializer(instance.city).data
            representation['contractor'] = OrganizationSerializer(instance.contractor).data

        return representation
