"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""

from __future__ import annotations
from gr4vy.types import BaseModel, Nullable, OptionalNullable, UNSET, UNSET_SENTINEL
from gr4vy.utils import validate_const
import pydantic
from pydantic import model_serializer
from pydantic.functional_validators import AfterValidator
from typing import Any, Dict, Literal
from typing_extensions import Annotated, NotRequired, TypedDict


class ApplePayPaymentMethodCreateTypedDict(TypedDict):
    r"""Create an Apple Pay transaction with a device or merchant token."""

    token: Dict[str, Any]
    r"""The opaque token as received from the Apple Pay JS library. This format may change between JS library versions."""
    buyer_external_identifier: NotRequired[Nullable[str]]
    r"""The external identifier of the buyer to create a payment for."""
    buyer_id: NotRequired[Nullable[str]]
    r"""The ID of the buyer to retrieve billing details for."""
    cardholder_name: NotRequired[Nullable[str]]
    r"""The card holder name associated to the original card for the token."""
    redirect_url: NotRequired[Nullable[str]]
    r"""The URL to redirect a user back to after the complete 3DS in browser."""
    card_suffix: NotRequired[Nullable[str]]
    r"""The last 4 digits of the original card used to generate the token."""
    card_scheme: NotRequired[Nullable[str]]
    r"""The original card scheme for which the token was generated."""
    card_type: NotRequired[Nullable[str]]
    r"""The payment scheme of the card."""
    method: Literal["applepay"]
    r"""Always `applepay`"""


class ApplePayPaymentMethodCreate(BaseModel):
    r"""Create an Apple Pay transaction with a device or merchant token."""

    token: Dict[str, Any]
    r"""The opaque token as received from the Apple Pay JS library. This format may change between JS library versions."""

    buyer_external_identifier: OptionalNullable[str] = UNSET
    r"""The external identifier of the buyer to create a payment for."""

    buyer_id: OptionalNullable[str] = UNSET
    r"""The ID of the buyer to retrieve billing details for."""

    cardholder_name: OptionalNullable[str] = UNSET
    r"""The card holder name associated to the original card for the token."""

    redirect_url: OptionalNullable[str] = UNSET
    r"""The URL to redirect a user back to after the complete 3DS in browser."""

    card_suffix: OptionalNullable[str] = UNSET
    r"""The last 4 digits of the original card used to generate the token."""

    card_scheme: OptionalNullable[str] = UNSET
    r"""The original card scheme for which the token was generated."""

    card_type: OptionalNullable[str] = UNSET
    r"""The payment scheme of the card."""

    METHOD: Annotated[
        Annotated[Literal["applepay"], AfterValidator(validate_const("applepay"))],
        pydantic.Field(alias="method"),
    ] = "applepay"
    r"""Always `applepay`"""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(
            [
                "buyer_external_identifier",
                "buyer_id",
                "cardholder_name",
                "redirect_url",
                "card_suffix",
                "card_scheme",
                "card_type",
            ]
        )
        nullable_fields = set(
            [
                "buyer_external_identifier",
                "buyer_id",
                "cardholder_name",
                "redirect_url",
                "card_suffix",
                "card_scheme",
                "card_type",
            ]
        )
        serialized = handler(self)
        m = {}

        for n, f in type(self).model_fields.items():
            k = f.alias or n
            val = serialized.get(k)
            is_nullable_and_explicitly_set = (
                k in nullable_fields
                and (self.__pydantic_fields_set__.intersection({n}))  # pylint: disable=no-member
            )

            if val != UNSET_SENTINEL:
                if (
                    val is not None
                    or k not in optional_fields
                    or is_nullable_and_explicitly_set
                ):
                    m[k] = val

        return m
