# This file was auto-generated by Fern from our API Definition.

import typing

import pydantic
import typing_extensions
from ..core.pydantic_utilities import IS_PYDANTIC_V2, UniversalBaseModel
from ..core.serialization import FieldMetadata
from .message_event_content import MessageEventContent
from .message_event_conversation import MessageEventConversation
from .message_event_direction import MessageEventDirection
from .message_event_fallback_message import MessageEventFallbackMessage
from .message_status_enum import MessageStatusEnum
from .webhook_event_enum import WebhookEventEnum


class MessageEvent(UniversalBaseModel):
    """
    Represents an incoming message or message status update received via webhook.
    """

    type: WebhookEventEnum = pydantic.Field()
    """
    Type of webhook event. MESSAGE.STATUS for message status updates or MESSAGE.RECEIVED for inbound messages.
    """

    conversation: MessageEventConversation = pydantic.Field()
    """
    Conversation metadata containing the conversation ID, sender, and recipient information.
    """

    status: MessageStatusEnum
    direction: MessageEventDirection = pydantic.Field()
    """
    Direction of the message flow.
    """

    segments: int = pydantic.Field()
    """
    Number of segments for this message.
    """

    sent_at: typing_extensions.Annotated[typing.Optional[str], FieldMetadata(alias="sentAt")] = pydantic.Field(
        default=None
    )
    """
    Timestamp when the message was sent in ISO 8601 format.
    Null if the message has not been sent yet.
    """

    delivered_at: typing_extensions.Annotated[typing.Optional[str], FieldMetadata(alias="deliveredAt")] = (
        pydantic.Field(default=None)
    )
    """
    Timestamp when the message was delivered in ISO 8601 format.
    Null if not yet delivered or for inbound messages.
    """

    message: MessageEventContent
    original_message_id: typing_extensions.Annotated[typing.Optional[str], FieldMetadata(alias="originalMessageId")] = (
        pydantic.Field(default=None)
    )
    """
    The unique identifier of the original RCS message that this fallback SMS/MMS was sent on behalf of. Always begins with the prefix `msg_`.
    
    Only present on webhook events for fallback SMS/MMS messages. Use this to link the fallback back to the RCS message that could not be delivered. The `message.id` field on this event refers to the actual SMS/MMS that was sent — `originalMessageId` refers to the RCS message it replaced.
    
    Null when the message is not a fallback.
    """

    fallback_message: typing_extensions.Annotated[
        typing.Optional[MessageEventFallbackMessage], FieldMetadata(alias="fallbackMessage")
    ] = pydantic.Field(default=None)
    """
    Details of the fallback SMS/MMS message(s) that were actually sent to the recipient instead of the original RCS message.
    
    Only present when the message `status` is `FALLBACK_SENT`. The `message.id` on this event refers to the original RCS message that could not be delivered. The `fallbackMessage.ids` contain the identifiers of the actual SMS/MMS messages that were sent.
    """

    if IS_PYDANTIC_V2:
        model_config: typing.ClassVar[pydantic.ConfigDict] = pydantic.ConfigDict(extra="allow", frozen=True)  # type: ignore # Pydantic v2
    else:

        class Config:
            frozen = True
            smart_union = True
            extra = pydantic.Extra.allow
