"""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 Any, Dict, Optional
from typing_extensions import NotRequired, TypedDict


class EventMessageTypedDict(TypedDict):
    r"""Represents an envelope and the data of an event"""

    account_id: NotRequired[str]
    r"""The account ID related to the event (if applicable)"""
    client_id: NotRequired[str]
    r"""The client ID related to the event"""
    correspondent_id: NotRequired[str]
    r"""The correspondent ID related to the event (if applicable)"""
    data: NotRequired[Nullable[Dict[str, Any]]]
    r"""A data payload containing the fields specific to the type of event being sent"""
    event_type: NotRequired[str]
    r"""Specifies the action that triggered the event as well as what resource changed"""
    message_id: NotRequired[str]
    r"""A unique identifier for the event"""
    name: NotRequired[str]
    r"""The resource name of the event; Format: messages/{message}"""
    partition_key: NotRequired[str]
    r"""A value, if present, is used to group related events together. Events with the same partition key are guaranteed to be sent to the consumer in the same order they were published."""
    publish_time: NotRequired[Nullable[datetime]]
    r"""The date and time of the event publication (not necessarily the time the event occurred)"""


class EventMessage(BaseModel):
    r"""Represents an envelope and the data of an event"""

    account_id: Optional[str] = None
    r"""The account ID related to the event (if applicable)"""

    client_id: Optional[str] = None
    r"""The client ID related to the event"""

    correspondent_id: Optional[str] = None
    r"""The correspondent ID related to the event (if applicable)"""

    data: OptionalNullable[Dict[str, Any]] = UNSET
    r"""A data payload containing the fields specific to the type of event being sent"""

    event_type: Optional[str] = None
    r"""Specifies the action that triggered the event as well as what resource changed"""

    message_id: Optional[str] = None
    r"""A unique identifier for the event"""

    name: Optional[str] = None
    r"""The resource name of the event; Format: messages/{message}"""

    partition_key: Optional[str] = None
    r"""A value, if present, is used to group related events together. Events with the same partition key are guaranteed to be sent to the consumer in the same order they were published."""

    publish_time: OptionalNullable[datetime] = UNSET
    r"""The date and time of the event publication (not necessarily the time the event occurred)"""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = [
            "account_id",
            "client_id",
            "correspondent_id",
            "data",
            "event_type",
            "message_id",
            "name",
            "partition_key",
            "publish_time",
        ]
        nullable_fields = ["data", "publish_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
