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

from __future__ import annotations
from .anthropicbase64pdfsource import (
    AnthropicBase64PdfSource,
    AnthropicBase64PdfSourceTypedDict,
)
from .anthropiccachecontroldirective import (
    AnthropicCacheControlDirective,
    AnthropicCacheControlDirectiveTypedDict,
)
from .anthropicfiledocumentsource import (
    AnthropicFileDocumentSource,
    AnthropicFileDocumentSourceTypedDict,
)
from .anthropicimageblockparam import (
    AnthropicImageBlockParam,
    AnthropicImageBlockParamTypedDict,
)
from .anthropicplaintextsource import (
    AnthropicPlainTextSource,
    AnthropicPlainTextSourceTypedDict,
)
from .anthropictextblockparam import (
    AnthropicTextBlockParam,
    AnthropicTextBlockParamTypedDict,
)
from .anthropicurlpdfsource import AnthropicURLPdfSource, AnthropicURLPdfSourceTypedDict
from openrouter.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from openrouter.utils import get_discriminator
from pydantic import Discriminator, Tag, model_serializer
from typing import List, Literal, Optional, Union
from typing_extensions import Annotated, NotRequired, TypeAliasType, TypedDict


class AnthropicDocumentBlockParamCitationsTypedDict(TypedDict):
    enabled: NotRequired[bool]


class AnthropicDocumentBlockParamCitations(BaseModel):
    enabled: Optional[bool] = None


AnthropicDocumentBlockParamContent1TypedDict = TypeAliasType(
    "AnthropicDocumentBlockParamContent1TypedDict",
    Union[AnthropicImageBlockParamTypedDict, AnthropicTextBlockParamTypedDict],
)


AnthropicDocumentBlockParamContent1 = Annotated[
    Union[
        Annotated[AnthropicImageBlockParam, Tag("image")],
        Annotated[AnthropicTextBlockParam, Tag("text")],
    ],
    Discriminator(lambda m: get_discriminator(m, "type", "type")),
]


AnthropicDocumentBlockParamContent2TypedDict = TypeAliasType(
    "AnthropicDocumentBlockParamContent2TypedDict",
    Union[str, List[AnthropicDocumentBlockParamContent1TypedDict]],
)


AnthropicDocumentBlockParamContent2 = TypeAliasType(
    "AnthropicDocumentBlockParamContent2",
    Union[str, List[AnthropicDocumentBlockParamContent1]],
)


SourceType = Literal["content",]


class SourceContentTypedDict(TypedDict):
    content: AnthropicDocumentBlockParamContent2TypedDict
    type: SourceType


class SourceContent(BaseModel):
    content: AnthropicDocumentBlockParamContent2

    type: SourceType


AnthropicDocumentBlockParamSourceUnionTypedDict = TypeAliasType(
    "AnthropicDocumentBlockParamSourceUnionTypedDict",
    Union[
        SourceContentTypedDict,
        AnthropicURLPdfSourceTypedDict,
        AnthropicFileDocumentSourceTypedDict,
        AnthropicBase64PdfSourceTypedDict,
        AnthropicPlainTextSourceTypedDict,
    ],
)


AnthropicDocumentBlockParamSourceUnion = Annotated[
    Union[
        Annotated[AnthropicBase64PdfSource, Tag("base64")],
        Annotated[AnthropicPlainTextSource, Tag("text")],
        Annotated[SourceContent, Tag("content")],
        Annotated[AnthropicURLPdfSource, Tag("url")],
        Annotated[AnthropicFileDocumentSource, Tag("file")],
    ],
    Discriminator(lambda m: get_discriminator(m, "type", "type")),
]


TypeDocument = Literal["document",]


class AnthropicDocumentBlockParamTypedDict(TypedDict):
    source: AnthropicDocumentBlockParamSourceUnionTypedDict
    type: TypeDocument
    cache_control: NotRequired[AnthropicCacheControlDirectiveTypedDict]
    r"""Enable automatic prompt caching. When set at the top level, the system automatically applies cache breakpoints to the last cacheable block in the request. Currently supported for Anthropic Claude models."""
    citations: NotRequired[Nullable[AnthropicDocumentBlockParamCitationsTypedDict]]
    context: NotRequired[Nullable[str]]
    title: NotRequired[Nullable[str]]


class AnthropicDocumentBlockParam(BaseModel):
    source: AnthropicDocumentBlockParamSourceUnion

    type: TypeDocument

    cache_control: Optional[AnthropicCacheControlDirective] = None
    r"""Enable automatic prompt caching. When set at the top level, the system automatically applies cache breakpoints to the last cacheable block in the request. Currently supported for Anthropic Claude models."""

    citations: OptionalNullable[AnthropicDocumentBlockParamCitations] = UNSET

    context: OptionalNullable[str] = UNSET

    title: OptionalNullable[str] = UNSET

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = ["cache_control", "citations", "context", "title"]
        nullable_fields = ["citations", "context", "title"]
        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
