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

from __future__ import annotations
from codat_bankfeeds.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from codat_bankfeeds.utils import serialize_decimal, validate_decimal
from decimal import Decimal
import pydantic
from pydantic import model_serializer
from pydantic.functional_serializers import PlainSerializer
from pydantic.functional_validators import BeforeValidator
from typing_extensions import Annotated, NotRequired, TypedDict


class AccountInfoTypedDict(TypedDict):
    account_open_date: NotRequired[Nullable[str]]
    r"""In Codat's data model, dates are represented using the <a class=\"external\" href=\"https://en.wikipedia.org/wiki/ISO_8601\" target=\"_blank\">ISO 8601 standard</a>. Date fields are formatted as strings; for example:
    ```
    2020-10-08
    ```
    """
    available_balance: NotRequired[Nullable[Decimal]]
    r"""Balance of funds available for use. This includes pending activity."""
    description: NotRequired[Nullable[str]]
    r"""Description of the account."""
    nickname: NotRequired[Nullable[str]]
    r"""Name given to the account by the user. Used in UIs to assist in account selection."""


class AccountInfo(BaseModel):
    account_open_date: Annotated[
        OptionalNullable[str], pydantic.Field(alias="accountOpenDate")
    ] = UNSET
    r"""In Codat's data model, dates are represented using the <a class=\"external\" href=\"https://en.wikipedia.org/wiki/ISO_8601\" target=\"_blank\">ISO 8601 standard</a>. Date fields are formatted as strings; for example:
    ```
    2020-10-08
    ```
    """

    available_balance: Annotated[
        Annotated[
            OptionalNullable[Decimal],
            BeforeValidator(validate_decimal),
            PlainSerializer(serialize_decimal(False)),
        ],
        pydantic.Field(alias="availableBalance"),
    ] = UNSET
    r"""Balance of funds available for use. This includes pending activity."""

    description: OptionalNullable[str] = UNSET
    r"""Description of the account."""

    nickname: OptionalNullable[str] = UNSET
    r"""Name given to the account by the user. Used in UIs to assist in account selection."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = [
            "accountOpenDate",
            "availableBalance",
            "description",
            "nickname",
        ]
        nullable_fields = [
            "accountOpenDate",
            "availableBalance",
            "description",
            "nickname",
        ]
        null_default_fields = []

        serialized = handler(self)

        m = {}

        for n, f in self.model_fields.items():
            k = f.alias or n
            val = serialized.get(k)
            serialized.pop(k, None)

            optional_nullable = k in optional_fields and k in nullable_fields
            is_set = (
                self.__pydantic_fields_set__.intersection({n})
                or k in null_default_fields
            )  # pylint: disable=no-member

            if val is not None and val != UNSET_SENTINEL:
                m[k] = val
            elif val != UNSET_SENTINEL and (
                not k in optional_fields or (optional_nullable and is_set)
            ):
                m[k] = val

        return m
