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

from __future__ import annotations
from .caseexpression import CaseExpression, CaseExpressionTypedDict
from .celexpression import CelExpression, CelExpressionTypedDict
from .jmespathexpression import JMESPathExpression, JMESPathExpressionTypedDict
import pydantic
from pydantic import Discriminator, Tag, model_serializer
from pydantic.functional_validators import AfterValidator
from syllable_sdk.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from syllable_sdk.utils import get_discriminator, validate_const
from typing import Any, Dict, Literal, Optional, Union
from typing_extensions import Annotated, NotRequired, TypeAliasType, TypedDict


CallActionIf1TypedDict = TypeAliasType(
    "CallActionIf1TypedDict", Union[CelExpressionTypedDict, JMESPathExpressionTypedDict]
)


CallActionIf1 = Annotated[
    Union[
        Annotated[CelExpression, Tag("cel")],
        Annotated[JMESPathExpression, Tag("jmespath")],
        Annotated[JMESPathExpression, Tag("jp")],
    ],
    Discriminator(lambda m: get_discriminator(m, "type", "type")),
]


CallActionIf2TypedDict = TypeAliasType(
    "CallActionIf2TypedDict",
    Union[CaseExpressionTypedDict, CallActionIf1TypedDict, str],
)
r"""An expression that must evaluate to true for the action to be applied."""


CallActionIf2 = TypeAliasType(
    "CallActionIf2", Union[CaseExpression, CallActionIf1, str]
)
r"""An expression that must evaluate to true for the action to be applied."""


AutoPopulateTypedDict = TypeAliasType("AutoPopulateTypedDict", Union[bool, str])
r"""Auto-population strategy: True (hybrid - synthetic or LLM), False (always LLM), \"required\" (synthetic only if all required args available, else LLM)"""


AutoPopulate = TypeAliasType("AutoPopulate", Union[bool, str])
r"""Auto-population strategy: True (hybrid - synthetic or LLM), False (always LLM), \"required\" (synthetic only if all required args available, else LLM)"""


class CallActionTypedDict(TypedDict):
    name: str
    r"""The name of the tool to call."""
    if_: NotRequired[Nullable[CallActionIf2TypedDict]]
    r"""An expression that must evaluate to true for the action to be applied."""
    action: Literal["call"]
    arguments: NotRequired[Nullable[Dict[str, Any]]]
    r"""Optional arguments to pass to the tool (supports template strings)"""
    auto_populate: NotRequired[AutoPopulateTypedDict]
    r"""Auto-population strategy: True (hybrid - synthetic or LLM), False (always LLM), \"required\" (synthetic only if all required args available, else LLM)"""


class CallAction(BaseModel):
    name: str
    r"""The name of the tool to call."""

    if_: Annotated[OptionalNullable[CallActionIf2], pydantic.Field(alias="if")] = UNSET
    r"""An expression that must evaluate to true for the action to be applied."""

    ACTION: Annotated[
        Annotated[Optional[Literal["call"]], AfterValidator(validate_const("call"))],
        pydantic.Field(alias="action"),
    ] = "call"

    arguments: OptionalNullable[Dict[str, Any]] = UNSET
    r"""Optional arguments to pass to the tool (supports template strings)"""

    auto_populate: Optional[AutoPopulate] = None
    r"""Auto-population strategy: True (hybrid - synthetic or LLM), False (always LLM), \"required\" (synthetic only if all required args available, else LLM)"""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(["if", "action", "arguments", "auto_populate"])
        nullable_fields = set(["if", "arguments"])
        serialized = handler(self)
        m = {}

        for n, f in type(self).model_fields.items():
            k = f.alias or n
            val = serialized.get(k)
            is_nullable_and_explicitly_set = (
                k in nullable_fields
                and (self.__pydantic_fields_set__.intersection({n}))  # pylint: disable=no-member
            )

            if val != UNSET_SENTINEL:
                if (
                    val is not None
                    or k not in optional_fields
                    or is_nullable_and_explicitly_set
                ):
                    m[k] = val

        return m
