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

from __future__ import annotations
from datetime import datetime
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 List, Literal, Optional
from typing_extensions import Annotated, NotRequired, TypedDict


class SettlementTypedDict(TypedDict):
    r"""A settlement record for a transaction."""

    id: str
    r"""The unique identifier for the record."""
    merchant_account_id: str
    r"""The merchant account this record belongs to."""
    created_at: datetime
    r"""The date and time the record was created, in ISO 8601 format."""
    updated_at: datetime
    r"""The date and time the record was last updated, in ISO 8601 format."""
    posted_at: datetime
    r"""The date and time the record was posted, in ISO 8601 format."""
    ingested_at: datetime
    r"""The date and time the record was ingested, in ISO 8601 format."""
    currency: str
    r"""ISO 4217 currency code."""
    amount: int
    r"""The total amount in the smallest currency unit (e.g. cents)."""
    commission: int
    r"""The commission amount deducted in the smallest currency unit."""
    payment_service_report_id: str
    r"""The report ID from the payment service."""
    payment_service_report_file_ids: List[str]
    r"""List of file IDs for the payment service report."""
    transaction_id: str
    r"""The transaction this record is associated with."""
    exchange_rate: NotRequired[Nullable[float]]
    r"""The exchange rate, if applicable."""
    interchange: NotRequired[Nullable[int]]
    r"""The interchange fee, if applicable, in the smallest currency unit."""
    markup: NotRequired[Nullable[int]]
    r"""The markup fee, if applicable, in the smallest currency unit."""
    scheme_fee: NotRequired[Nullable[int]]
    r"""The scheme fee, if applicable, in the smallest currency unit."""
    type: Literal["settlement"]
    r"""Always `settlement`."""


class Settlement(BaseModel):
    r"""A settlement record for a transaction."""

    id: str
    r"""The unique identifier for the record."""

    merchant_account_id: str
    r"""The merchant account this record belongs to."""

    created_at: datetime
    r"""The date and time the record was created, in ISO 8601 format."""

    updated_at: datetime
    r"""The date and time the record was last updated, in ISO 8601 format."""

    posted_at: datetime
    r"""The date and time the record was posted, in ISO 8601 format."""

    ingested_at: datetime
    r"""The date and time the record was ingested, in ISO 8601 format."""

    currency: str
    r"""ISO 4217 currency code."""

    amount: int
    r"""The total amount in the smallest currency unit (e.g. cents)."""

    commission: int
    r"""The commission amount deducted in the smallest currency unit."""

    payment_service_report_id: str
    r"""The report ID from the payment service."""

    payment_service_report_file_ids: List[str]
    r"""List of file IDs for the payment service report."""

    transaction_id: str
    r"""The transaction this record is associated with."""

    exchange_rate: OptionalNullable[float] = UNSET
    r"""The exchange rate, if applicable."""

    interchange: OptionalNullable[int] = UNSET
    r"""The interchange fee, if applicable, in the smallest currency unit."""

    markup: OptionalNullable[int] = UNSET
    r"""The markup fee, if applicable, in the smallest currency unit."""

    scheme_fee: OptionalNullable[int] = UNSET
    r"""The scheme fee, if applicable, in the smallest currency unit."""

    TYPE: Annotated[
        Annotated[
            Optional[Literal["settlement"]],
            AfterValidator(validate_const("settlement")),
        ],
        pydantic.Field(alias="type"),
    ] = "settlement"
    r"""Always `settlement`."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(
            ["exchange_rate", "interchange", "markup", "scheme_fee", "type"]
        )
        nullable_fields = set(["exchange_rate", "interchange", "markup", "scheme_fee"])
        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:
    Settlement.model_rebuild()
except NameError:
    pass
