"""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 Result(str, Enum, metaclass=utils.OpenEnumMeta):
    r"""The outcome of the delivery"""

    PUSH_SUBSCRIPTION_DELIVERY_RESULT_UNSPECIFIED = (
        "PUSH_SUBSCRIPTION_DELIVERY_RESULT_UNSPECIFIED"
    )
    SUCCEEDED = "SUCCEEDED"
    FAILED = "FAILED"


class PushSubscriptionDeliveryTypedDict(TypedDict):
    r"""Represents the delivery results of an event for a push subscription"""

    delivery_id: NotRequired[str]
    r"""The unique identifier for the delivery"""
    duration: NotRequired[str]
    r"""The total time spent delivering"""
    event: NotRequired[str]
    r"""The resource name of the event; Format: messages/{message}"""
    event_publish_time: NotRequired[Nullable[datetime]]
    r"""The date and time of the event publication"""
    last_response: NotRequired[str]
    r"""The last response received when delivering the event"""
    last_send_time: NotRequired[Nullable[datetime]]
    r"""The time the event was last pushed"""
    name: NotRequired[str]
    r"""The resource name of the delivery; Format: subscriptions/{subscription}/deliveries/{delivery}"""
    result: NotRequired[Result]
    r"""The outcome of the delivery"""


class PushSubscriptionDelivery(BaseModel):
    r"""Represents the delivery results of an event for a push subscription"""

    delivery_id: Optional[str] = None
    r"""The unique identifier for the delivery"""

    duration: Optional[str] = None
    r"""The total time spent delivering"""

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

    event_publish_time: OptionalNullable[datetime] = UNSET
    r"""The date and time of the event publication"""

    last_response: Optional[str] = None
    r"""The last response received when delivering the event"""

    last_send_time: OptionalNullable[datetime] = UNSET
    r"""The time the event was last pushed"""

    name: Optional[str] = None
    r"""The resource name of the delivery; Format: subscriptions/{subscription}/deliveries/{delivery}"""

    result: Annotated[
        Optional[Result], PlainValidator(validate_open_enum(False))
    ] = None
    r"""The outcome of the delivery"""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = [
            "delivery_id",
            "duration",
            "event",
            "event_publish_time",
            "last_response",
            "last_send_time",
            "name",
            "result",
        ]
        nullable_fields = ["event_publish_time", "last_send_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
