# This file was auto-generated by Fern from our API Definition.

import typing

from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.request_options import RequestOptions
from ..types.ach_validation import AchValidation
from ..types.entrypointfield import Entrypointfield
from ..types.force_customer_creation import ForceCustomerCreation
from ..types.idempotency_key import IdempotencyKey
from ..types.payabli_api_response_paymethod_delete import PayabliApiResponsePaymethodDelete
from ..types.payor_data_request import PayorDataRequest
from ..types.source import Source
from ..types.subdomain import Subdomain
from .raw_client import AsyncRawTokenStorageClient, RawTokenStorageClient
from .types.add_method_response import AddMethodResponse
from .types.create_anonymous import CreateAnonymous
from .types.get_method_response import GetMethodResponse
from .types.request_token_storage_payment_method import RequestTokenStoragePaymentMethod
from .types.temporary import Temporary

# this is used as the default value for optional parameters
OMIT = typing.cast(typing.Any, ...)


class TokenStorageClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawTokenStorageClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> RawTokenStorageClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        RawTokenStorageClient
        """
        return self._raw_client

    def add_method(
        self,
        *,
        create_anonymous: CreateAnonymous,
        temporary: Temporary,
        ach_validation: typing.Optional[AchValidation] = None,
        force_customer_creation: typing.Optional[ForceCustomerCreation] = None,
        idempotency_key: typing.Optional[IdempotencyKey] = None,
        customer_data: typing.Optional[PayorDataRequest] = OMIT,
        entry_point: typing.Optional[Entrypointfield] = OMIT,
        fallback_auth: typing.Optional[bool] = OMIT,
        fallback_auth_amount: typing.Optional[int] = OMIT,
        method_description: typing.Optional[str] = OMIT,
        payment_method: typing.Optional[RequestTokenStoragePaymentMethod] = OMIT,
        source: typing.Optional[Source] = OMIT,
        subdomain: typing.Optional[Subdomain] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AddMethodResponse:
        """
        Saves a payment method for reuse. This call exchanges sensitive payment information for a token that can be used to process future transactions. The `ReferenceId` value in the response is the `storedMethodId` to use with transactions.

        Parameters
        ----------
        create_anonymous : CreateAnonymous

        temporary : Temporary

        ach_validation : typing.Optional[AchValidation]

        force_customer_creation : typing.Optional[ForceCustomerCreation]

        idempotency_key : typing.Optional[IdempotencyKey]

        customer_data : typing.Optional[PayorDataRequest]
            Object describing the Customer/Payor owner of payment method. Required for POST requests. Which fields are required depends on the paypoint's custom identifier settings.

        entry_point : typing.Optional[Entrypointfield]
            Entrypoint identifier. Required for POST requests.

        fallback_auth : typing.Optional[bool]
            When `true`, if tokenization fails, Payabli will attempt an authorization transaction to request a permanent token for the card. If the authorization is successful, the card will be tokenized and the authorization will be voided automatically.

        fallback_auth_amount : typing.Optional[int]
            The amount for the `fallbackAuth` transaction. Defaults to one dollar (`100`).

        method_description : typing.Optional[str]
            Custom description for stored payment method.

        payment_method : typing.Optional[RequestTokenStoragePaymentMethod]
            Information about the payment method for the transaction.

        source : typing.Optional[Source]
            Custom identifier to indicate the source for the request

        subdomain : typing.Optional[Subdomain]

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AddMethodResponse
            Success

        Examples
        --------
        from payabli import PayorDataRequest, payabli
        from payabli.token_storage import TokenizeCard

        client = payabli(
            api_key="YOUR_API_KEY",
        )
        client.token_storage.add_method(
            customer_data=PayorDataRequest(
                customer_id=4440,
            ),
            entry_point="f743aed24a",
            fallback_auth=True,
            payment_method=TokenizeCard(
                cardcvv="123",
                cardexp="02/25",
                card_holder="John Doe",
                cardnumber="4111111111111111",
                cardzip="12345",
                method="card",
            ),
        )
        """
        _response = self._raw_client.add_method(
            create_anonymous=create_anonymous,
            temporary=temporary,
            ach_validation=ach_validation,
            force_customer_creation=force_customer_creation,
            idempotency_key=idempotency_key,
            customer_data=customer_data,
            entry_point=entry_point,
            fallback_auth=fallback_auth,
            fallback_auth_amount=fallback_auth_amount,
            method_description=method_description,
            payment_method=payment_method,
            source=source,
            subdomain=subdomain,
            request_options=request_options,
        )
        return _response.data

    def get_method(
        self,
        method_id: str,
        *,
        card_expiration_format: typing.Optional[int] = None,
        include_temporary: typing.Optional[bool] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> GetMethodResponse:
        """
        Retrieves details for a saved payment method.

        Parameters
        ----------
        method_id : str
            The saved payment method ID.

        card_expiration_format : typing.Optional[int]
            Format for card expiration dates in the response.

            Accepted values:

            - 0: default, no formatting. Expiration dates are returned in the format they're saved in.

            - 1: MMYY

            - 2: MM/YY

        include_temporary : typing.Optional[bool]
            When `true`, the request will include temporary tokens in the search and return details for a matching temporary token. The default behavior searches only for permanent tokens.

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        GetMethodResponse
            Success

        Examples
        --------
        from payabli import payabli

        client = payabli(
            api_key="YOUR_API_KEY",
        )
        client.token_storage.get_method(
            method_id="32-8877drt00045632-678",
            card_expiration_format=1,
            include_temporary=False,
        )
        """
        _response = self._raw_client.get_method(
            method_id,
            card_expiration_format=card_expiration_format,
            include_temporary=include_temporary,
            request_options=request_options,
        )
        return _response.data

    def remove_method(
        self, method_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> PayabliApiResponsePaymethodDelete:
        """
        Deletes a saved payment method.

        Parameters
        ----------
        method_id : str
            The saved payment method ID.

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        PayabliApiResponsePaymethodDelete
            Success

        Examples
        --------
        from payabli import payabli

        client = payabli(
            api_key="YOUR_API_KEY",
        )
        client.token_storage.remove_method(
            method_id="32-8877drt00045632-678",
        )
        """
        _response = self._raw_client.remove_method(method_id, request_options=request_options)
        return _response.data

    def update_method(
        self,
        method_id: str,
        *,
        ach_validation: typing.Optional[AchValidation] = None,
        customer_data: typing.Optional[PayorDataRequest] = OMIT,
        entry_point: typing.Optional[Entrypointfield] = OMIT,
        fallback_auth: typing.Optional[bool] = OMIT,
        fallback_auth_amount: typing.Optional[int] = OMIT,
        method_description: typing.Optional[str] = OMIT,
        payment_method: typing.Optional[RequestTokenStoragePaymentMethod] = OMIT,
        source: typing.Optional[Source] = OMIT,
        subdomain: typing.Optional[Subdomain] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> PayabliApiResponsePaymethodDelete:
        """
        Updates a saved payment method.

        Parameters
        ----------
        method_id : str
            The saved payment method ID.

        ach_validation : typing.Optional[AchValidation]

        customer_data : typing.Optional[PayorDataRequest]
            Object describing the Customer/Payor owner of payment method. Required for POST requests. Which fields are required depends on the paypoint's custom identifier settings.

        entry_point : typing.Optional[Entrypointfield]
            Entrypoint identifier. Required for POST requests.

        fallback_auth : typing.Optional[bool]
            When `true`, if tokenization fails, Payabli will attempt an authorization transaction to request a permanent token for the card. If the authorization is successful, the card will be tokenized and the authorization will be voided automatically.

        fallback_auth_amount : typing.Optional[int]
            The amount for the `fallbackAuth` transaction. Defaults to one dollar (`100`).

        method_description : typing.Optional[str]
            Custom description for stored payment method.

        payment_method : typing.Optional[RequestTokenStoragePaymentMethod]
            Information about the payment method for the transaction.

        source : typing.Optional[Source]
            Custom identifier to indicate the source for the request

        subdomain : typing.Optional[Subdomain]

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        PayabliApiResponsePaymethodDelete
            Success

        Examples
        --------
        from payabli import PayorDataRequest, payabli
        from payabli.token_storage import TokenizeCard

        client = payabli(
            api_key="YOUR_API_KEY",
        )
        client.token_storage.update_method(
            method_id="32-8877drt00045632-678",
            customer_data=PayorDataRequest(
                customer_id=4440,
            ),
            entry_point="f743aed24a",
            fallback_auth=True,
            payment_method=TokenizeCard(
                cardcvv="123",
                cardexp="02/25",
                card_holder="John Doe",
                cardnumber="4111111111111111",
                cardzip="12345",
                method="card",
            ),
        )
        """
        _response = self._raw_client.update_method(
            method_id,
            ach_validation=ach_validation,
            customer_data=customer_data,
            entry_point=entry_point,
            fallback_auth=fallback_auth,
            fallback_auth_amount=fallback_auth_amount,
            method_description=method_description,
            payment_method=payment_method,
            source=source,
            subdomain=subdomain,
            request_options=request_options,
        )
        return _response.data


class AsyncTokenStorageClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawTokenStorageClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> AsyncRawTokenStorageClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        AsyncRawTokenStorageClient
        """
        return self._raw_client

    async def add_method(
        self,
        *,
        create_anonymous: CreateAnonymous,
        temporary: Temporary,
        ach_validation: typing.Optional[AchValidation] = None,
        force_customer_creation: typing.Optional[ForceCustomerCreation] = None,
        idempotency_key: typing.Optional[IdempotencyKey] = None,
        customer_data: typing.Optional[PayorDataRequest] = OMIT,
        entry_point: typing.Optional[Entrypointfield] = OMIT,
        fallback_auth: typing.Optional[bool] = OMIT,
        fallback_auth_amount: typing.Optional[int] = OMIT,
        method_description: typing.Optional[str] = OMIT,
        payment_method: typing.Optional[RequestTokenStoragePaymentMethod] = OMIT,
        source: typing.Optional[Source] = OMIT,
        subdomain: typing.Optional[Subdomain] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AddMethodResponse:
        """
        Saves a payment method for reuse. This call exchanges sensitive payment information for a token that can be used to process future transactions. The `ReferenceId` value in the response is the `storedMethodId` to use with transactions.

        Parameters
        ----------
        create_anonymous : CreateAnonymous

        temporary : Temporary

        ach_validation : typing.Optional[AchValidation]

        force_customer_creation : typing.Optional[ForceCustomerCreation]

        idempotency_key : typing.Optional[IdempotencyKey]

        customer_data : typing.Optional[PayorDataRequest]
            Object describing the Customer/Payor owner of payment method. Required for POST requests. Which fields are required depends on the paypoint's custom identifier settings.

        entry_point : typing.Optional[Entrypointfield]
            Entrypoint identifier. Required for POST requests.

        fallback_auth : typing.Optional[bool]
            When `true`, if tokenization fails, Payabli will attempt an authorization transaction to request a permanent token for the card. If the authorization is successful, the card will be tokenized and the authorization will be voided automatically.

        fallback_auth_amount : typing.Optional[int]
            The amount for the `fallbackAuth` transaction. Defaults to one dollar (`100`).

        method_description : typing.Optional[str]
            Custom description for stored payment method.

        payment_method : typing.Optional[RequestTokenStoragePaymentMethod]
            Information about the payment method for the transaction.

        source : typing.Optional[Source]
            Custom identifier to indicate the source for the request

        subdomain : typing.Optional[Subdomain]

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AddMethodResponse
            Success

        Examples
        --------
        import asyncio

        from payabli import Asyncpayabli, PayorDataRequest
        from payabli.token_storage import TokenizeCard

        client = Asyncpayabli(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.token_storage.add_method(
                customer_data=PayorDataRequest(
                    customer_id=4440,
                ),
                entry_point="f743aed24a",
                fallback_auth=True,
                payment_method=TokenizeCard(
                    cardcvv="123",
                    cardexp="02/25",
                    card_holder="John Doe",
                    cardnumber="4111111111111111",
                    cardzip="12345",
                    method="card",
                ),
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.add_method(
            create_anonymous=create_anonymous,
            temporary=temporary,
            ach_validation=ach_validation,
            force_customer_creation=force_customer_creation,
            idempotency_key=idempotency_key,
            customer_data=customer_data,
            entry_point=entry_point,
            fallback_auth=fallback_auth,
            fallback_auth_amount=fallback_auth_amount,
            method_description=method_description,
            payment_method=payment_method,
            source=source,
            subdomain=subdomain,
            request_options=request_options,
        )
        return _response.data

    async def get_method(
        self,
        method_id: str,
        *,
        card_expiration_format: typing.Optional[int] = None,
        include_temporary: typing.Optional[bool] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> GetMethodResponse:
        """
        Retrieves details for a saved payment method.

        Parameters
        ----------
        method_id : str
            The saved payment method ID.

        card_expiration_format : typing.Optional[int]
            Format for card expiration dates in the response.

            Accepted values:

            - 0: default, no formatting. Expiration dates are returned in the format they're saved in.

            - 1: MMYY

            - 2: MM/YY

        include_temporary : typing.Optional[bool]
            When `true`, the request will include temporary tokens in the search and return details for a matching temporary token. The default behavior searches only for permanent tokens.

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        GetMethodResponse
            Success

        Examples
        --------
        import asyncio

        from payabli import Asyncpayabli

        client = Asyncpayabli(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.token_storage.get_method(
                method_id="32-8877drt00045632-678",
                card_expiration_format=1,
                include_temporary=False,
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.get_method(
            method_id,
            card_expiration_format=card_expiration_format,
            include_temporary=include_temporary,
            request_options=request_options,
        )
        return _response.data

    async def remove_method(
        self, method_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> PayabliApiResponsePaymethodDelete:
        """
        Deletes a saved payment method.

        Parameters
        ----------
        method_id : str
            The saved payment method ID.

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        PayabliApiResponsePaymethodDelete
            Success

        Examples
        --------
        import asyncio

        from payabli import Asyncpayabli

        client = Asyncpayabli(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.token_storage.remove_method(
                method_id="32-8877drt00045632-678",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.remove_method(method_id, request_options=request_options)
        return _response.data

    async def update_method(
        self,
        method_id: str,
        *,
        ach_validation: typing.Optional[AchValidation] = None,
        customer_data: typing.Optional[PayorDataRequest] = OMIT,
        entry_point: typing.Optional[Entrypointfield] = OMIT,
        fallback_auth: typing.Optional[bool] = OMIT,
        fallback_auth_amount: typing.Optional[int] = OMIT,
        method_description: typing.Optional[str] = OMIT,
        payment_method: typing.Optional[RequestTokenStoragePaymentMethod] = OMIT,
        source: typing.Optional[Source] = OMIT,
        subdomain: typing.Optional[Subdomain] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> PayabliApiResponsePaymethodDelete:
        """
        Updates a saved payment method.

        Parameters
        ----------
        method_id : str
            The saved payment method ID.

        ach_validation : typing.Optional[AchValidation]

        customer_data : typing.Optional[PayorDataRequest]
            Object describing the Customer/Payor owner of payment method. Required for POST requests. Which fields are required depends on the paypoint's custom identifier settings.

        entry_point : typing.Optional[Entrypointfield]
            Entrypoint identifier. Required for POST requests.

        fallback_auth : typing.Optional[bool]
            When `true`, if tokenization fails, Payabli will attempt an authorization transaction to request a permanent token for the card. If the authorization is successful, the card will be tokenized and the authorization will be voided automatically.

        fallback_auth_amount : typing.Optional[int]
            The amount for the `fallbackAuth` transaction. Defaults to one dollar (`100`).

        method_description : typing.Optional[str]
            Custom description for stored payment method.

        payment_method : typing.Optional[RequestTokenStoragePaymentMethod]
            Information about the payment method for the transaction.

        source : typing.Optional[Source]
            Custom identifier to indicate the source for the request

        subdomain : typing.Optional[Subdomain]

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        PayabliApiResponsePaymethodDelete
            Success

        Examples
        --------
        import asyncio

        from payabli import Asyncpayabli, PayorDataRequest
        from payabli.token_storage import TokenizeCard

        client = Asyncpayabli(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.token_storage.update_method(
                method_id="32-8877drt00045632-678",
                customer_data=PayorDataRequest(
                    customer_id=4440,
                ),
                entry_point="f743aed24a",
                fallback_auth=True,
                payment_method=TokenizeCard(
                    cardcvv="123",
                    cardexp="02/25",
                    card_holder="John Doe",
                    cardnumber="4111111111111111",
                    cardzip="12345",
                    method="card",
                ),
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.update_method(
            method_id,
            ach_validation=ach_validation,
            customer_data=customer_data,
            entry_point=entry_point,
            fallback_auth=fallback_auth,
            fallback_auth_amount=fallback_auth_amount,
            method_description=method_description,
            payment_method=payment_method,
            source=source,
            subdomain=subdomain,
            request_options=request_options,
        )
        return _response.data
