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

from __future__ import annotations
from openrouter.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
    UnrecognizedStr,
)
from openrouter.utils import validate_open_enum
from pydantic import model_serializer
from pydantic.functional_validators import PlainValidator
from typing import Any, Dict, List, Literal, Optional, Union
from typing_extensions import Annotated, NotRequired, TypeAliasType, TypedDict


class AllowedToolsTypedDict(TypedDict):
    read_only: NotRequired[bool]
    tool_names: NotRequired[List[str]]


class AllowedTools(BaseModel):
    read_only: Optional[bool] = None

    tool_names: Optional[List[str]] = None


AllowedToolsUnionTypedDict = TypeAliasType(
    "AllowedToolsUnionTypedDict", Union[AllowedToolsTypedDict, List[str], Any]
)


AllowedToolsUnion = TypeAliasType(
    "AllowedToolsUnion", Union[AllowedTools, List[str], Any]
)


ConnectorID = Union[
    Literal[
        "connector_dropbox",
        "connector_gmail",
        "connector_googlecalendar",
        "connector_googledrive",
        "connector_microsoftteams",
        "connector_outlookcalendar",
        "connector_outlookemail",
        "connector_sharepoint",
    ],
    UnrecognizedStr,
]


RequireApprovalNever = Literal["never",]


RequireApprovalAlways = Literal["always",]


class AlwaysTypedDict(TypedDict):
    tool_names: NotRequired[List[str]]


class Always(BaseModel):
    tool_names: Optional[List[str]] = None


class NeverTypedDict(TypedDict):
    tool_names: NotRequired[List[str]]


class Never(BaseModel):
    tool_names: Optional[List[str]] = None


class RequireApprovalTypedDict(TypedDict):
    always: NotRequired[AlwaysTypedDict]
    never: NotRequired[NeverTypedDict]


class RequireApproval(BaseModel):
    always: Optional[Always] = None

    never: Optional[Never] = None


RequireApprovalUnionTypedDict = TypeAliasType(
    "RequireApprovalUnionTypedDict",
    Union[RequireApprovalTypedDict, RequireApprovalAlways, RequireApprovalNever, Any],
)


RequireApprovalUnion = TypeAliasType(
    "RequireApprovalUnion",
    Union[RequireApproval, RequireApprovalAlways, RequireApprovalNever, Any],
)


McpServerToolType = Literal["mcp",]


class McpServerToolTypedDict(TypedDict):
    r"""MCP (Model Context Protocol) tool configuration"""

    server_label: str
    type: McpServerToolType
    allowed_tools: NotRequired[Nullable[AllowedToolsUnionTypedDict]]
    authorization: NotRequired[str]
    connector_id: NotRequired[ConnectorID]
    headers: NotRequired[Nullable[Dict[str, str]]]
    require_approval: NotRequired[Nullable[RequireApprovalUnionTypedDict]]
    server_description: NotRequired[str]
    server_url: NotRequired[str]


class McpServerTool(BaseModel):
    r"""MCP (Model Context Protocol) tool configuration"""

    server_label: str

    type: McpServerToolType

    allowed_tools: OptionalNullable[AllowedToolsUnion] = UNSET

    authorization: Optional[str] = None

    connector_id: Annotated[
        Optional[ConnectorID], PlainValidator(validate_open_enum(False))
    ] = None

    headers: OptionalNullable[Dict[str, str]] = UNSET

    require_approval: OptionalNullable[RequireApprovalUnion] = UNSET

    server_description: Optional[str] = None

    server_url: Optional[str] = None

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = [
            "allowed_tools",
            "authorization",
            "connector_id",
            "headers",
            "require_approval",
            "server_description",
            "server_url",
        ]
        nullable_fields = ["allowed_tools", "headers", "require_approval"]
        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
