"""Payments withdrawals domain — sync and async mixins."""

from __future__ import annotations

from decimal import Decimal
from typing import Any

from ...exceptions import api_error_handler, async_api_error_handler
from ..._api.generated.payments.payments__api__payments.models import (
    PaginatedWithdrawalListList,
    WithdrawalCancelResponse,
    WithdrawalCancelResponseRequest,
    WithdrawalCreateRequest,
    WithdrawalCreateResponse,
    WithdrawalDetail,
)


class _WithdrawalsMixin:
    """Sync withdrawals methods."""

    _api: Any

    @api_error_handler
    def create_withdrawal(
        self,
        *,
        amount_usd: float | Decimal | str,
        currency_code: str,
        wallet_address: str,
        project_slug: str,
        customer_id: str,
    ) -> WithdrawalCreateResponse:
        """Create a withdrawal request."""
        request = WithdrawalCreateRequest(
            amount_usd=str(amount_usd),
            currency_code=currency_code.upper(),
            wallet_address=wallet_address,
            project_slug=project_slug,
            customer_id=customer_id,
        )
        return self._api.withdrawals_create_create(request)

    @api_error_handler
    def list_withdrawals(
        self,
        *,
        page: int = 1,
        page_size: int = 20,
        status: str | None = None,
    ) -> PaginatedWithdrawalListList:
        """List withdrawal requests."""
        return self._api.withdrawals_list(page=page, page_size=page_size)

    @api_error_handler
    def get_withdrawal(self, withdrawal_id: str) -> WithdrawalDetail:
        """Get withdrawal details."""
        return self._api.withdrawals_retrieve(withdrawal_id)

    @api_error_handler
    def cancel_withdrawal(self, withdrawal_id: str) -> WithdrawalCancelResponse:
        """Cancel a pending/approved withdrawal."""
        request = WithdrawalCancelResponseRequest(success=True, message="cancel")
        return self._api.withdrawals_cancel_create(withdrawal_id, request)


class _AsyncWithdrawalsMixin:
    """Async withdrawals methods."""

    _api: Any

    @async_api_error_handler
    async def create_withdrawal(
        self,
        *,
        amount_usd: float | Decimal | str,
        currency_code: str,
        wallet_address: str,
        project_slug: str,
        customer_id: str,
    ) -> WithdrawalCreateResponse:
        """Create a withdrawal request."""
        request = WithdrawalCreateRequest(
            amount_usd=str(amount_usd),
            currency_code=currency_code.upper(),
            wallet_address=wallet_address,
            project_slug=project_slug,
            customer_id=customer_id,
        )
        return await self._api.withdrawals_create_create(request)

    @async_api_error_handler
    async def list_withdrawals(
        self,
        *,
        page: int = 1,
        page_size: int = 20,
        status: str | None = None,
    ) -> PaginatedWithdrawalListList:
        """List withdrawal requests."""
        return await self._api.withdrawals_list(page=page, page_size=page_size)

    @async_api_error_handler
    async def get_withdrawal(self, withdrawal_id: str) -> WithdrawalDetail:
        """Get withdrawal details."""
        return await self._api.withdrawals_retrieve(withdrawal_id)

    @async_api_error_handler
    async def cancel_withdrawal(self, withdrawal_id: str) -> WithdrawalCancelResponse:
        """Cancel a pending/approved withdrawal."""
        request = WithdrawalCancelResponseRequest(success=True, message="cancel")
        return await self._api.withdrawals_cancel_create(withdrawal_id, request)
