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

from __future__ import annotations
from .bankaccountholder import BankAccountHolder, BankAccountHolderTypedDict
from gr4vy.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
    UnrecognizedStr,
)
from gr4vy.utils import validate_const
import pydantic
from pydantic import model_serializer
from pydantic.functional_validators import AfterValidator
from typing import Literal, Optional, Union
from typing_extensions import Annotated, NotRequired, TypedDict


AccountType = Union[
    Literal[
        "checking",
        "savings",
    ],
    UnrecognizedStr,
]
r"""Specify whether this is a `checking` or `savings` account"""


class ACHBankPaymentMethodCreateTypedDict(TypedDict):
    r"""ACH Bank Payment Method

    Bank Payment Method for ACH bank accounts.
    """

    account_holder: BankAccountHolderTypedDict
    account_number: str
    r"""The account number for this ACH bank account"""
    routing_number: str
    r"""The routing number for this ACH bank account"""
    account_type: AccountType
    r"""Specify whether this is a `checking` or `savings` account"""
    method: Literal["bank"]
    r"""Always `bank`."""
    buyer_id: NotRequired[Nullable[str]]
    r"""The ID of the buyer to attach the method to."""
    buyer_external_identifier: NotRequired[Nullable[str]]
    r"""The merchant reference for this payment method."""
    external_identifier: NotRequired[Nullable[str]]
    r"""The merchant identifier for this payment method."""
    scheme: Literal["ach"]
    r"""Always `ach`."""
    is_tokenized: NotRequired[bool]
    r"""Whether the account number is tokenized"""


class ACHBankPaymentMethodCreate(BaseModel):
    r"""ACH Bank Payment Method

    Bank Payment Method for ACH bank accounts.
    """

    account_holder: BankAccountHolder

    account_number: str
    r"""The account number for this ACH bank account"""

    routing_number: str
    r"""The routing number for this ACH bank account"""

    account_type: AccountType
    r"""Specify whether this is a `checking` or `savings` account"""

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

    buyer_id: OptionalNullable[str] = UNSET
    r"""The ID of the buyer to attach the method to."""

    buyer_external_identifier: OptionalNullable[str] = UNSET
    r"""The merchant reference for this payment method."""

    external_identifier: OptionalNullable[str] = UNSET
    r"""The merchant identifier for this payment method."""

    SCHEME: Annotated[
        Annotated[Optional[Literal["ach"]], AfterValidator(validate_const("ach"))],
        pydantic.Field(alias="scheme"),
    ] = "ach"
    r"""Always `ach`."""

    is_tokenized: Optional[bool] = False
    r"""Whether the account number is tokenized"""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(
            [
                "method",
                "buyer_id",
                "buyer_external_identifier",
                "external_identifier",
                "scheme",
                "is_tokenized",
            ]
        )
        nullable_fields = set(
            ["buyer_id", "buyer_external_identifier", "external_identifier"]
        )
        serialized = handler(self)
        m = {}

        for n, f in type(self).model_fields.items():
            k = f.alias or n
            val = serialized.get(k, serialized.get(n))
            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


try:
    ACHBankPaymentMethodCreate.model_rebuild()
except NameError:
    pass
