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

import typing
from ...core.client_wrapper import SyncClientWrapper
from .raw_client import RawCardsClient
from ...requests.address import AddressParams
from ...core.request_options import RequestOptions
from ...types.create_customer_card_response import CreateCustomerCardResponse
from ...types.delete_customer_card_response import DeleteCustomerCardResponse
from ...core.client_wrapper import AsyncClientWrapper
from .raw_client import AsyncRawCardsClient

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


class CardsClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawCardsClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        RawCardsClient
        """
        return self._raw_client

    def create(
        self,
        customer_id: str,
        *,
        card_nonce: str,
        billing_address: typing.Optional[AddressParams] = OMIT,
        cardholder_name: typing.Optional[str] = OMIT,
        verification_token: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> CreateCustomerCardResponse:
        """
        Adds a card on file to an existing customer.

        As with charges, calls to `CreateCustomerCard` are idempotent. Multiple
        calls with the same card nonce return the same card record that was created
        with the provided nonce during the _first_ call.

        Parameters
        ----------
        customer_id : str
            The Square ID of the customer profile the card is linked to.

        card_nonce : str
            A card nonce representing the credit card to link to the customer.

            Card nonces are generated by the Square payment form when customers enter
            their card information. For more information, see
            [Walkthrough: Integrate Square Payments in a Website](https://developer.squareup.com/docs/web-payments/take-card-payment).

            __NOTE:__ Card nonces generated by digital wallets (such as Apple Pay)
            cannot be used to create a customer card.

        billing_address : typing.Optional[AddressParams]
            Address information for the card on file.

            __NOTE:__ If a billing address is provided in the request, the
            `CreateCustomerCardRequest.billing_address.postal_code` must match
            the postal code encoded in the card nonce.

        cardholder_name : typing.Optional[str]
            The full name printed on the credit card.

        verification_token : typing.Optional[str]
            An identifying token generated by [Payments.verifyBuyer()](https://developer.squareup.com/reference/sdks/web/payments/objects/Payments#Payments.verifyBuyer).
            Verification tokens encapsulate customer device information and 3-D Secure
            challenge results to indicate that Square has verified the buyer identity.

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

        Returns
        -------
        CreateCustomerCardResponse
            Success

        Examples
        --------
        from square import Square

        client = Square(
            token="YOUR_TOKEN",
        )
        client.customers.cards.create(
            customer_id="customer_id",
            card_nonce="YOUR_CARD_NONCE",
            billing_address={
                "address_line1": "500 Electric Ave",
                "address_line2": "Suite 600",
                "locality": "New York",
                "administrative_district_level1": "NY",
                "postal_code": "10003",
                "country": "US",
            },
            cardholder_name="Amelia Earhart",
        )
        """
        response = self._raw_client.create(
            customer_id,
            card_nonce=card_nonce,
            billing_address=billing_address,
            cardholder_name=cardholder_name,
            verification_token=verification_token,
            request_options=request_options,
        )
        return response.data

    def delete(
        self, customer_id: str, card_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> DeleteCustomerCardResponse:
        """
        Removes a card on file from a customer.

        Parameters
        ----------
        customer_id : str
            The ID of the customer that the card on file belongs to.

        card_id : str
            The ID of the card on file to delete.

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

        Returns
        -------
        DeleteCustomerCardResponse
            Success

        Examples
        --------
        from square import Square

        client = Square(
            token="YOUR_TOKEN",
        )
        client.customers.cards.delete(
            customer_id="customer_id",
            card_id="card_id",
        )
        """
        response = self._raw_client.delete(customer_id, card_id, request_options=request_options)
        return response.data


class AsyncCardsClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawCardsClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        AsyncRawCardsClient
        """
        return self._raw_client

    async def create(
        self,
        customer_id: str,
        *,
        card_nonce: str,
        billing_address: typing.Optional[AddressParams] = OMIT,
        cardholder_name: typing.Optional[str] = OMIT,
        verification_token: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> CreateCustomerCardResponse:
        """
        Adds a card on file to an existing customer.

        As with charges, calls to `CreateCustomerCard` are idempotent. Multiple
        calls with the same card nonce return the same card record that was created
        with the provided nonce during the _first_ call.

        Parameters
        ----------
        customer_id : str
            The Square ID of the customer profile the card is linked to.

        card_nonce : str
            A card nonce representing the credit card to link to the customer.

            Card nonces are generated by the Square payment form when customers enter
            their card information. For more information, see
            [Walkthrough: Integrate Square Payments in a Website](https://developer.squareup.com/docs/web-payments/take-card-payment).

            __NOTE:__ Card nonces generated by digital wallets (such as Apple Pay)
            cannot be used to create a customer card.

        billing_address : typing.Optional[AddressParams]
            Address information for the card on file.

            __NOTE:__ If a billing address is provided in the request, the
            `CreateCustomerCardRequest.billing_address.postal_code` must match
            the postal code encoded in the card nonce.

        cardholder_name : typing.Optional[str]
            The full name printed on the credit card.

        verification_token : typing.Optional[str]
            An identifying token generated by [Payments.verifyBuyer()](https://developer.squareup.com/reference/sdks/web/payments/objects/Payments#Payments.verifyBuyer).
            Verification tokens encapsulate customer device information and 3-D Secure
            challenge results to indicate that Square has verified the buyer identity.

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

        Returns
        -------
        CreateCustomerCardResponse
            Success

        Examples
        --------
        import asyncio

        from square import AsyncSquare

        client = AsyncSquare(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.customers.cards.create(
                customer_id="customer_id",
                card_nonce="YOUR_CARD_NONCE",
                billing_address={
                    "address_line1": "500 Electric Ave",
                    "address_line2": "Suite 600",
                    "locality": "New York",
                    "administrative_district_level1": "NY",
                    "postal_code": "10003",
                    "country": "US",
                },
                cardholder_name="Amelia Earhart",
            )


        asyncio.run(main())
        """
        response = await self._raw_client.create(
            customer_id,
            card_nonce=card_nonce,
            billing_address=billing_address,
            cardholder_name=cardholder_name,
            verification_token=verification_token,
            request_options=request_options,
        )
        return response.data

    async def delete(
        self, customer_id: str, card_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> DeleteCustomerCardResponse:
        """
        Removes a card on file from a customer.

        Parameters
        ----------
        customer_id : str
            The ID of the customer that the card on file belongs to.

        card_id : str
            The ID of the card on file to delete.

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

        Returns
        -------
        DeleteCustomerCardResponse
            Success

        Examples
        --------
        import asyncio

        from square import AsyncSquare

        client = AsyncSquare(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.customers.cards.delete(
                customer_id="customer_id",
                card_id="card_id",
            )


        asyncio.run(main())
        """
        response = await self._raw_client.delete(customer_id, card_id, request_options=request_options)
        return response.data
