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

from __future__ import annotations
from .chatassistantimages import ChatAssistantImages, ChatAssistantImagesTypedDict
from .chataudiooutput import ChatAudioOutput, ChatAudioOutputTypedDict
from .chatcontentitems import ChatContentItems, ChatContentItemsTypedDict
from .chattoolcall import ChatToolCall, ChatToolCallTypedDict
from .reasoningdetailunion import ReasoningDetailUnion, ReasoningDetailUnionTypedDict
from openrouter.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from pydantic import model_serializer
from typing import Any, List, Literal, Optional, Union
from typing_extensions import NotRequired, TypeAliasType, TypedDict


ChatAssistantMessageContentTypedDict = TypeAliasType(
    "ChatAssistantMessageContentTypedDict",
    Union[str, List[ChatContentItemsTypedDict], Any],
)
r"""Assistant message content"""


ChatAssistantMessageContent = TypeAliasType(
    "ChatAssistantMessageContent", Union[str, List[ChatContentItems], Any]
)
r"""Assistant message content"""


ChatAssistantMessageRole = Literal["assistant",]


class ChatAssistantMessageTypedDict(TypedDict):
    r"""Assistant message for requests and responses"""

    role: ChatAssistantMessageRole
    audio: NotRequired[ChatAudioOutputTypedDict]
    r"""Audio output data or reference"""
    content: NotRequired[Nullable[ChatAssistantMessageContentTypedDict]]
    r"""Assistant message content"""
    images: NotRequired[List[ChatAssistantImagesTypedDict]]
    r"""Generated images from image generation models"""
    name: NotRequired[str]
    r"""Optional name for the assistant"""
    reasoning: NotRequired[Nullable[str]]
    r"""Reasoning output"""
    reasoning_details: NotRequired[List[ReasoningDetailUnionTypedDict]]
    r"""Reasoning details for extended thinking models"""
    refusal: NotRequired[Nullable[str]]
    r"""Refusal message if content was refused"""
    tool_calls: NotRequired[List[ChatToolCallTypedDict]]
    r"""Tool calls made by the assistant"""


class ChatAssistantMessage(BaseModel):
    r"""Assistant message for requests and responses"""

    role: ChatAssistantMessageRole

    audio: Optional[ChatAudioOutput] = None
    r"""Audio output data or reference"""

    content: OptionalNullable[ChatAssistantMessageContent] = UNSET
    r"""Assistant message content"""

    images: Optional[List[ChatAssistantImages]] = None
    r"""Generated images from image generation models"""

    name: Optional[str] = None
    r"""Optional name for the assistant"""

    reasoning: OptionalNullable[str] = UNSET
    r"""Reasoning output"""

    reasoning_details: Optional[List[ReasoningDetailUnion]] = None
    r"""Reasoning details for extended thinking models"""

    refusal: OptionalNullable[str] = UNSET
    r"""Refusal message if content was refused"""

    tool_calls: Optional[List[ChatToolCall]] = None
    r"""Tool calls made by the assistant"""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = [
            "audio",
            "content",
            "images",
            "name",
            "reasoning",
            "reasoning_details",
            "refusal",
            "tool_calls",
        ]
        nullable_fields = ["content", "reasoning", "refusal"]
        null_default_fields = []

        serialized = handler(self)

        m = {}

        for n, f in type(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
