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

from __future__ import annotations
from ascend_sdk.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
import pydantic
from pydantic import model_serializer
from typing import Optional
from typing_extensions import Annotated, NotRequired, TypedDict


class AdjustedSettledTypedDict(TypedDict):
    r"""`settled` + any as of settled amounts for the date"""

    value: NotRequired[str]
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class AdjustedSettled(BaseModel):
    r"""`settled` + any as of settled amounts for the date"""

    value: Optional[str] = None
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class AdjustedTradeTypedDict(TypedDict):
    r"""`trade` + any as of trade amounts for the date"""

    value: NotRequired[str]
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class AdjustedTrade(BaseModel):
    r"""`trade` + any as of trade amounts for the date"""

    value: Optional[str] = None
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class DateTypedDict(TypedDict):
    r"""The date for which the positions were calculated"""

    day: NotRequired[int]
    r"""Day of a month. Must be from 1 to 31 and valid for the year and month, or 0 to specify a year by itself or a year and month where the day isn't significant."""
    month: NotRequired[int]
    r"""Month of a year. Must be from 1 to 12, or 0 to specify a year without a month and day."""
    year: NotRequired[int]
    r"""Year of the date. Must be from 1 to 9999, or 0 to specify a date without a year."""


class Date(BaseModel):
    r"""The date for which the positions were calculated"""

    day: Optional[int] = None
    r"""Day of a month. Must be from 1 to 31 and valid for the year and month, or 0 to specify a year by itself or a year and month where the day isn't significant."""

    month: Optional[int] = None
    r"""Month of a year. Must be from 1 to 12, or 0 to specify a year without a month and day."""

    year: Optional[int] = None
    r"""Year of the date. Must be from 1 to 9999, or 0 to specify a date without a year."""


class PositionFpslTypedDict(TypedDict):
    r"""Quantity of asset in use by the FPSL program. Should not be used by currency assets"""

    value: NotRequired[str]
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class PositionFpsl(BaseModel):
    r"""Quantity of asset in use by the FPSL program. Should not be used by currency assets"""

    value: Optional[str] = None
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class FreeTypedDict(TypedDict):
    r"""Quantity of asset available for allocation for use by the FPSL program. Raw bucket values. These denote that a position is allocated to this purpose. Values may be negative"""

    value: NotRequired[str]
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class Free(BaseModel):
    r"""Quantity of asset available for allocation for use by the FPSL program. Raw bucket values. These denote that a position is allocated to this purpose. Values may be negative"""

    value: Optional[str] = None
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class LastAdjustedDateTypedDict(TypedDict):
    r"""The most recent date an adjustment occurred"""

    day: NotRequired[int]
    r"""Day of a month. Must be from 1 to 31 and valid for the year and month, or 0 to specify a year by itself or a year and month where the day isn't significant."""
    month: NotRequired[int]
    r"""Month of a year. Must be from 1 to 12, or 0 to specify a year without a month and day."""
    year: NotRequired[int]
    r"""Year of the date. Must be from 1 to 9999, or 0 to specify a date without a year."""


class LastAdjustedDate(BaseModel):
    r"""The most recent date an adjustment occurred"""

    day: Optional[int] = None
    r"""Day of a month. Must be from 1 to 31 and valid for the year and month, or 0 to specify a year by itself or a year and month where the day isn't significant."""

    month: Optional[int] = None
    r"""Month of a year. Must be from 1 to 12, or 0 to specify a year without a month and day."""

    year: Optional[int] = None
    r"""Year of the date. Must be from 1 to 9999, or 0 to specify a date without a year."""


class PendingDripTypedDict(TypedDict):
    r"""Quantity of currency from a dividend being reserved for reinvestment. should not be used by non-currency assets"""

    value: NotRequired[str]
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class PendingDrip(BaseModel):
    r"""Quantity of currency from a dividend being reserved for reinvestment. should not be used by non-currency assets"""

    value: Optional[str] = None
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class PendingOutgoingAcatTypedDict(TypedDict):
    r"""Quantity/ amount of asset restricted due to an outgoing acat request"""

    value: NotRequired[str]
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class PendingOutgoingAcat(BaseModel):
    r"""Quantity/ amount of asset restricted due to an outgoing acat request"""

    value: Optional[str] = None
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class PendingWithdrawalTypedDict(TypedDict):
    r"""Quantity of currency being reserved for withdrawal. should not be used by non-currency assets"""

    value: NotRequired[str]
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class PendingWithdrawal(BaseModel):
    r"""Quantity of currency being reserved for withdrawal. should not be used by non-currency assets"""

    value: Optional[str] = None
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class SettledTypedDict(TypedDict):
    r"""Computed fieldsOriginal Settled Position before and as-of changesComputed based on the bucket values to represet the total settled position in an account  Currently defined as `free` + `fpsl` + `pending_outgoing_acat` + `drip` + `pending_withdrawal`, but if/when new buckets are added this value will need to change to reflect them"""

    value: NotRequired[str]
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class Settled(BaseModel):
    r"""Computed fieldsOriginal Settled Position before and as-of changesComputed based on the bucket values to represet the total settled position in an account  Currently defined as `free` + `fpsl` + `pending_outgoing_acat` + `drip` + `pending_withdrawal`, but if/when new buckets are added this value will need to change to reflect them"""

    value: Optional[str] = None
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class PositionTradeTypedDict(TypedDict):
    r"""original trade position"""

    value: NotRequired[str]
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class PositionTrade(BaseModel):
    r"""original trade position"""

    value: Optional[str] = None
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class UnrestrictedTypedDict(TypedDict):
    r"""Computed based on the bucket values to represent the total unrestricted position in an account. Will always be less than or equal to `settled`  settled - (pending_outgoing_acat + pending_drip + pending_withdrawal) ; however, if/when the API adds new buckets, Apex may adjust this to either incorporate the new value or not"""

    value: NotRequired[str]
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class Unrestricted(BaseModel):
    r"""Computed based on the bucket values to represent the total unrestricted position in an account. Will always be less than or equal to `settled`  settled - (pending_outgoing_acat + pending_drip + pending_withdrawal) ; however, if/when the API adds new buckets, Apex may adjust this to either incorporate the new value or not"""

    value: Optional[str] = None
    r"""The decimal value, as a string; Refer to [Google’s Decimal type protocol buffer](https://github.com/googleapis/googleapis/blob/40203ca1880849480bbff7b8715491060bbccdf1/google/type/decimal.proto#L33) for details"""


class PositionTypedDict(TypedDict):
    r"""Position"""

    account_id: NotRequired[str]
    r"""A globally unique identifier referencing a single account; this is the main identifier for an account used for machine-to-machine interactions"""
    adjusted_settled: NotRequired[Nullable[AdjustedSettledTypedDict]]
    r"""`settled` + any as of settled amounts for the date"""
    adjusted_trade: NotRequired[Nullable[AdjustedTradeTypedDict]]
    r"""`trade` + any as of trade amounts for the date"""
    asset_id: NotRequired[str]
    r"""An Apex-provided, global identifier created on a per asset bases which provides connectivity across all areas"""
    correspondent_id: NotRequired[str]
    r"""The correspondent id associated with the account for the position"""
    date_: NotRequired[Nullable[DateTypedDict]]
    r"""The date for which the positions were calculated"""
    fpsl: NotRequired[Nullable[PositionFpslTypedDict]]
    r"""Quantity of asset in use by the FPSL program. Should not be used by currency assets"""
    free: NotRequired[Nullable[FreeTypedDict]]
    r"""Quantity of asset available for allocation for use by the FPSL program. Raw bucket values. These denote that a position is allocated to this purpose. Values may be negative"""
    last_adjusted_date: NotRequired[Nullable[LastAdjustedDateTypedDict]]
    r"""The most recent date an adjustment occurred"""
    name: NotRequired[str]
    r"""accounts/{account_id}/positions/{position_id}"""
    pending_drip: NotRequired[Nullable[PendingDripTypedDict]]
    r"""Quantity of currency from a dividend being reserved for reinvestment. should not be used by non-currency assets"""
    pending_outgoing_acat: NotRequired[Nullable[PendingOutgoingAcatTypedDict]]
    r"""Quantity/ amount of asset restricted due to an outgoing acat request"""
    pending_withdrawal: NotRequired[Nullable[PendingWithdrawalTypedDict]]
    r"""Quantity of currency being reserved for withdrawal. should not be used by non-currency assets"""
    settled: NotRequired[Nullable[SettledTypedDict]]
    r"""Computed fieldsOriginal Settled Position before and as-of changesComputed based on the bucket values to represet the total settled position in an account  Currently defined as `free` + `fpsl` + `pending_outgoing_acat` + `drip` + `pending_withdrawal`, but if/when new buckets are added this value will need to change to reflect them"""
    trade: NotRequired[Nullable[PositionTradeTypedDict]]
    r"""original trade position"""
    unrestricted: NotRequired[Nullable[UnrestrictedTypedDict]]
    r"""Computed based on the bucket values to represent the total unrestricted position in an account. Will always be less than or equal to `settled`  settled - (pending_outgoing_acat + pending_drip + pending_withdrawal) ; however, if/when the API adds new buckets, Apex may adjust this to either incorporate the new value or not"""


class Position(BaseModel):
    r"""Position"""

    account_id: Optional[str] = None
    r"""A globally unique identifier referencing a single account; this is the main identifier for an account used for machine-to-machine interactions"""

    adjusted_settled: OptionalNullable[AdjustedSettled] = UNSET
    r"""`settled` + any as of settled amounts for the date"""

    adjusted_trade: OptionalNullable[AdjustedTrade] = UNSET
    r"""`trade` + any as of trade amounts for the date"""

    asset_id: Optional[str] = None
    r"""An Apex-provided, global identifier created on a per asset bases which provides connectivity across all areas"""

    correspondent_id: Optional[str] = None
    r"""The correspondent id associated with the account for the position"""

    date_: Annotated[OptionalNullable[Date], pydantic.Field(alias="date")] = UNSET
    r"""The date for which the positions were calculated"""

    fpsl: OptionalNullable[PositionFpsl] = UNSET
    r"""Quantity of asset in use by the FPSL program. Should not be used by currency assets"""

    free: OptionalNullable[Free] = UNSET
    r"""Quantity of asset available for allocation for use by the FPSL program. Raw bucket values. These denote that a position is allocated to this purpose. Values may be negative"""

    last_adjusted_date: OptionalNullable[LastAdjustedDate] = UNSET
    r"""The most recent date an adjustment occurred"""

    name: Optional[str] = None
    r"""accounts/{account_id}/positions/{position_id}"""

    pending_drip: OptionalNullable[PendingDrip] = UNSET
    r"""Quantity of currency from a dividend being reserved for reinvestment. should not be used by non-currency assets"""

    pending_outgoing_acat: OptionalNullable[PendingOutgoingAcat] = UNSET
    r"""Quantity/ amount of asset restricted due to an outgoing acat request"""

    pending_withdrawal: OptionalNullable[PendingWithdrawal] = UNSET
    r"""Quantity of currency being reserved for withdrawal. should not be used by non-currency assets"""

    settled: OptionalNullable[Settled] = UNSET
    r"""Computed fieldsOriginal Settled Position before and as-of changesComputed based on the bucket values to represet the total settled position in an account  Currently defined as `free` + `fpsl` + `pending_outgoing_acat` + `drip` + `pending_withdrawal`, but if/when new buckets are added this value will need to change to reflect them"""

    trade: OptionalNullable[PositionTrade] = UNSET
    r"""original trade position"""

    unrestricted: OptionalNullable[Unrestricted] = UNSET
    r"""Computed based on the bucket values to represent the total unrestricted position in an account. Will always be less than or equal to `settled`  settled - (pending_outgoing_acat + pending_drip + pending_withdrawal) ; however, if/when the API adds new buckets, Apex may adjust this to either incorporate the new value or not"""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = [
            "account_id",
            "adjusted_settled",
            "adjusted_trade",
            "asset_id",
            "correspondent_id",
            "date",
            "fpsl",
            "free",
            "last_adjusted_date",
            "name",
            "pending_drip",
            "pending_outgoing_acat",
            "pending_withdrawal",
            "settled",
            "trade",
            "unrestricted",
        ]
        nullable_fields = [
            "adjusted_settled",
            "adjusted_trade",
            "date",
            "fpsl",
            "free",
            "last_adjusted_date",
            "pending_drip",
            "pending_outgoing_acat",
            "pending_withdrawal",
            "settled",
            "trade",
            "unrestricted",
        ]
        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
