"""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,
)
from datetime import datetime
from pydantic import model_serializer
from typing import Optional
from typing_extensions import NotRequired, TypedDict


class RestrictionTypedDict(TypedDict):
    r"""An account restriction."""

    active: NotRequired[bool]
    r"""Indicates whether the restriction is active or not."""
    create_reason: NotRequired[str]
    r"""The plain-text reason set by a person or system explaining why the restriction was set"""
    create_time: NotRequired[Nullable[datetime]]
    r"""The datetime of the object's creation"""
    create_user_id: NotRequired[str]
    r"""The user attributed to the restriction's placement on the account; typically expressed as an Auth user ID"""
    ended_reason: NotRequired[str]
    r"""The plain-text reason set by a person or system explaining why the restriction was removed If the restriction is active, the reason will not be set."""
    ended_time: NotRequired[Nullable[datetime]]
    r"""The datetime of a restriction's removal from an account If the restriction is active, the ended time will not be set."""
    ended_user_id: NotRequired[str]
    r"""The user attributed to the restriction's removal from the account; typically expressed as an Auth user ID If the restriction is active, the user will not be set."""
    restriction_code: NotRequired[str]
    r"""A enumerated value indicating the nature of a restriction; prefixed by the concerned department (e.g., `MARGIN_CALL_VIOLATION_REG_T`); suspends related entitlements of the account until it is removed; most codes are administrated by the custodian but some are available for use by the integrator"""


class Restriction(BaseModel):
    r"""An account restriction."""

    active: Optional[bool] = None
    r"""Indicates whether the restriction is active or not."""

    create_reason: Optional[str] = None
    r"""The plain-text reason set by a person or system explaining why the restriction was set"""

    create_time: OptionalNullable[datetime] = UNSET
    r"""The datetime of the object's creation"""

    create_user_id: Optional[str] = None
    r"""The user attributed to the restriction's placement on the account; typically expressed as an Auth user ID"""

    ended_reason: Optional[str] = None
    r"""The plain-text reason set by a person or system explaining why the restriction was removed If the restriction is active, the reason will not be set."""

    ended_time: OptionalNullable[datetime] = UNSET
    r"""The datetime of a restriction's removal from an account If the restriction is active, the ended time will not be set."""

    ended_user_id: Optional[str] = None
    r"""The user attributed to the restriction's removal from the account; typically expressed as an Auth user ID If the restriction is active, the user will not be set."""

    restriction_code: Optional[str] = None
    r"""A enumerated value indicating the nature of a restriction; prefixed by the concerned department (e.g., `MARGIN_CALL_VIOLATION_REG_T`); suspends related entitlements of the account until it is removed; most codes are administrated by the custodian but some are available for use by the integrator"""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = [
            "active",
            "create_reason",
            "create_time",
            "create_user_id",
            "ended_reason",
            "ended_time",
            "ended_user_id",
            "restriction_code",
        ]
        nullable_fields = ["create_time", "ended_time"]
        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
