import graphene
from graphene import Mutation
from rest_framework.exceptions import PermissionDenied

from engine.core.utils.messages import permission_denied_message
from engine.payments.graphene.object_types import TransactionType
from engine.payments.models import Transaction


class Deposit(Mutation):
    class Arguments:
        amount = graphene.Float(required=True)

    transaction = graphene.Field(TransactionType)

    def mutate(self, info, amount):
        if info.context.user.is_authenticated:
            transaction = Transaction.objects.create(
                balance=info.context.user.payments_balance,
                amount=amount,
                currency="EUR",
            )
            # noinspection PyTypeChecker
            return Deposit(transaction=transaction)  # ty: ignore[unknown-argument]
        else:
            raise PermissionDenied(permission_denied_message)
