"""
Courier History serializer
"""

from rest_framework import serializers

from core.apps.eggs import models


class CourierHistorySerializer(serializers.ModelSerializer):
    class Meta:
        model = models.CourierHistory
        fields = (
            "id",
            "courier_id",
            "group_id",
            "get_eggs",
            "return_eggs",
            "broken_eggs",
            "date",
        )

    def update(self, instance, validated_data):
        return_eggs = validated_data.get("return_eggs", 0)
        broken_eggs = validated_data.get("broken_eggs", 0)

        instance.return_eggs += return_eggs
        instance.broken_eggs += broken_eggs
        instance.save()

        return instance
