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

from __future__ import annotations
from .audio import Audio, AudioTypedDict
from .chatcompletioncontentpartrefusalparam import (
    ChatCompletionContentPartRefusalParam,
    ChatCompletionContentPartRefusalParamTypedDict,
)
from .chatcompletioncontentparttextparam import (
    ChatCompletionContentPartTextParam,
    ChatCompletionContentPartTextParamTypedDict,
)
from .chatcompletionmessagetoolcallparam import (
    ChatCompletionMessageToolCallParam,
    ChatCompletionMessageToolCallParamTypedDict,
)
from .functioncall_input import FunctionCallInput, FunctionCallInputTypedDict
from opperai.types import BaseModel, Nullable, OptionalNullable, UNSET, UNSET_SENTINEL
from opperai.utils import validate_const
import pydantic
from pydantic import model_serializer
from pydantic.functional_validators import AfterValidator
from typing import List, Literal, Optional, Union
from typing_extensions import Annotated, NotRequired, TypeAliasType, TypedDict


ChatCompletionAssistantMessageParamContent1TypedDict = TypeAliasType(
    "ChatCompletionAssistantMessageParamContent1TypedDict",
    Union[
        ChatCompletionContentPartTextParamTypedDict,
        ChatCompletionContentPartRefusalParamTypedDict,
    ],
)


ChatCompletionAssistantMessageParamContent1 = TypeAliasType(
    "ChatCompletionAssistantMessageParamContent1",
    Union[ChatCompletionContentPartTextParam, ChatCompletionContentPartRefusalParam],
)


ChatCompletionAssistantMessageParamContent2TypedDict = TypeAliasType(
    "ChatCompletionAssistantMessageParamContent2TypedDict",
    Union[str, List[ChatCompletionAssistantMessageParamContent1TypedDict]],
)


ChatCompletionAssistantMessageParamContent2 = TypeAliasType(
    "ChatCompletionAssistantMessageParamContent2",
    Union[str, List[ChatCompletionAssistantMessageParamContent1]],
)


class ChatCompletionAssistantMessageParamTypedDict(TypedDict):
    role: Literal["assistant"]
    audio: NotRequired[Nullable[AudioTypedDict]]
    content: NotRequired[Nullable[ChatCompletionAssistantMessageParamContent2TypedDict]]
    function_call: NotRequired[Nullable[FunctionCallInputTypedDict]]
    name: NotRequired[str]
    refusal: NotRequired[Nullable[str]]
    tool_calls: NotRequired[List[ChatCompletionMessageToolCallParamTypedDict]]


class ChatCompletionAssistantMessageParam(BaseModel):
    ROLE: Annotated[
        Annotated[Literal["assistant"], AfterValidator(validate_const("assistant"))],
        pydantic.Field(alias="role"),
    ] = "assistant"

    audio: OptionalNullable[Audio] = UNSET

    content: OptionalNullable[ChatCompletionAssistantMessageParamContent2] = UNSET

    function_call: OptionalNullable[FunctionCallInput] = UNSET

    name: Optional[str] = None

    refusal: OptionalNullable[str] = UNSET

    tool_calls: Optional[List[ChatCompletionMessageToolCallParam]] = None

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = [
            "audio",
            "content",
            "function_call",
            "name",
            "refusal",
            "tool_calls",
        ]
        nullable_fields = ["audio", "content", "function_call", "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
