from rest_framework.fields import FloatField, JSONField, SerializerMethodField
from rest_framework.serializers import ModelSerializer, Serializer

from payments.models import Transaction


class DepositSerializer(Serializer):  # type: ignore [type-arg]
    amount = FloatField(required=True)


class TransactionSerializer(ModelSerializer):  # type: ignore [type-arg]
    class Meta:
        model = Transaction
        fields = "__all__"


class TransactionProcessSerializer(ModelSerializer):  # type: ignore [type-arg]
    process = JSONField(required=True)
    order_hr_id = SerializerMethodField(read_only=True, required=False)
    order_uuid = SerializerMethodField(read_only=True, required=False)

    def get_order_hr_id(self, obj: Transaction) -> str | None:
        return obj.order.human_readable_id if obj.order else None

    def get_order_uuid(self, obj: Transaction) -> str | None:
        return str(obj.order.uuid) if obj.order else None

    class Meta:
        model = Transaction
        fields = ("process", "order_hr_id", "order_uuid")
        read_only_fields = ("process", "order_hr_id", "order_uuid")
