from rest_framework import serializers
from core.apps.eggs.models.all_history import AllHistory


class AllHistorySerializer(serializers.ModelSerializer):
    action = serializers.SerializerMethodField()

    class Meta:
        model = AllHistory
        fields = [
            "id",
            "object_id",
            "action",
            "timestamp",
            "created_by",
            "created_who",
            "reason",
        ]

    def get_action(self, obj):
        return obj.action

    def to_representation(self, instance):
        representation = super().to_representation(instance)
        if instance.action != "created":
            return {}
        return representation
