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

from __future__ import annotations
from ascend_sdk import utils
from ascend_sdk.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from ascend_sdk.utils import validate_open_enum
from datetime import datetime
from enum import Enum
from pydantic import model_serializer
from pydantic.functional_validators import PlainValidator
from typing import Optional
from typing_extensions import Annotated, NotRequired, TypedDict


class BasketState(str, Enum, metaclass=utils.OpenEnumMeta):
    r"""The processing status of the basket"""

    STATE_UNSPECIFIED = "STATE_UNSPECIFIED"
    NEW = "NEW"
    SUBMITTED = "SUBMITTED"
    EXECUTING = "EXECUTING"
    ALLOCATING = "ALLOCATING"
    DONE = "DONE"
    REJECTED = "REJECTED"


class BasketTypedDict(TypedDict):
    r"""The message describing a basket"""

    basket_id: NotRequired[str]
    r"""System generated unique id for the basket"""
    basket_order_count: NotRequired[str]
    r"""Number of orders in the basket"""
    basket_state: NotRequired[BasketState]
    r"""The processing status of the basket"""
    client_basket_id: NotRequired[str]
    r"""User-supplied unique basket ID. Cannot be more than 40 characters long."""
    complete_time: NotRequired[Nullable[datetime]]
    r"""Time the basket was completed"""
    compressed_order_count: NotRequired[str]
    r"""Number of compressed orders in the basket that will go to market. This number is calculated after basket submission."""
    correspondent_id: NotRequired[str]
    r"""The unique id for the correspondent for the basket"""
    create_time: NotRequired[Nullable[datetime]]
    r"""Time of the basket creation"""
    distinct_account_count: NotRequired[str]
    r"""Number of distinct accounts in the basket."""
    last_update_time: NotRequired[Nullable[datetime]]
    r"""Time of the last basket update"""
    name: NotRequired[str]
    r"""System generated name of the basket"""
    rejected_account_count: NotRequired[str]
    r"""Number of accounts that did not pass risk checks and their orders were rejected."""
    submit_time: NotRequired[Nullable[datetime]]
    r"""Time the basket was submitted"""


class Basket(BaseModel):
    r"""The message describing a basket"""

    basket_id: Optional[str] = None
    r"""System generated unique id for the basket"""

    basket_order_count: Optional[str] = None
    r"""Number of orders in the basket"""

    basket_state: Annotated[
        Optional[BasketState], PlainValidator(validate_open_enum(False))
    ] = None
    r"""The processing status of the basket"""

    client_basket_id: Optional[str] = None
    r"""User-supplied unique basket ID. Cannot be more than 40 characters long."""

    complete_time: OptionalNullable[datetime] = UNSET
    r"""Time the basket was completed"""

    compressed_order_count: Optional[str] = None
    r"""Number of compressed orders in the basket that will go to market. This number is calculated after basket submission."""

    correspondent_id: Optional[str] = None
    r"""The unique id for the correspondent for the basket"""

    create_time: OptionalNullable[datetime] = UNSET
    r"""Time of the basket creation"""

    distinct_account_count: Optional[str] = None
    r"""Number of distinct accounts in the basket."""

    last_update_time: OptionalNullable[datetime] = UNSET
    r"""Time of the last basket update"""

    name: Optional[str] = None
    r"""System generated name of the basket"""

    rejected_account_count: Optional[str] = None
    r"""Number of accounts that did not pass risk checks and their orders were rejected."""

    submit_time: OptionalNullable[datetime] = UNSET
    r"""Time the basket was submitted"""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = [
            "basket_id",
            "basket_order_count",
            "basket_state",
            "client_basket_id",
            "complete_time",
            "compressed_order_count",
            "correspondent_id",
            "create_time",
            "distinct_account_count",
            "last_update_time",
            "name",
            "rejected_account_count",
            "submit_time",
        ]
        nullable_fields = [
            "complete_time",
            "create_time",
            "last_update_time",
            "submit_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
