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

from __future__ import annotations
from .callaction import CallAction, CallActionTypedDict
from .getvalueaction import GetValueAction, GetValueActionTypedDict
from .incrementaction import IncrementAction, IncrementActionTypedDict
from .saveaction import SaveAction, SaveActionTypedDict
from .sayaction import SayAction, SayActionTypedDict
from .setvalueaction import SetValueAction, SetValueActionTypedDict
from pydantic import Discriminator, Tag, model_serializer
from syllable_sdk.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from syllable_sdk.utils import get_discriminator
from typing import List, Union
from typing_extensions import Annotated, NotRequired, TypeAliasType, TypedDict


StepEventActionsStartTypedDict = TypeAliasType(
    "StepEventActionsStartTypedDict",
    Union[
        IncrementActionTypedDict,
        SayActionTypedDict,
        SetValueActionTypedDict,
        CallActionTypedDict,
    ],
)


StepEventActionsStart = Annotated[
    Union[
        Annotated[CallAction, Tag("call")],
        Annotated[IncrementAction, Tag("inc")],
        Annotated[SayAction, Tag("say")],
        Annotated[SetValueAction, Tag("set")],
    ],
    Discriminator(lambda m: get_discriminator(m, "action", "action")),
]


EnterTypedDict = TypeAliasType(
    "EnterTypedDict",
    Union[
        IncrementActionTypedDict,
        SayActionTypedDict,
        SetValueActionTypedDict,
        CallActionTypedDict,
        GetValueActionTypedDict,
    ],
)


Enter = Annotated[
    Union[
        Annotated[CallAction, Tag("call")],
        Annotated[GetValueAction, Tag("get")],
        Annotated[IncrementAction, Tag("inc")],
        Annotated[GetValueAction, Tag("load")],
        Annotated[SayAction, Tag("say")],
        Annotated[SetValueAction, Tag("set")],
    ],
    Discriminator(lambda m: get_discriminator(m, "action", "action")),
]


PresubmitTypedDict = TypeAliasType(
    "PresubmitTypedDict",
    Union[
        IncrementActionTypedDict,
        SaveActionTypedDict,
        SetValueActionTypedDict,
        GetValueActionTypedDict,
    ],
)


Presubmit = Annotated[
    Union[
        Annotated[GetValueAction, Tag("get")],
        Annotated[IncrementAction, Tag("inc")],
        Annotated[GetValueAction, Tag("load")],
        Annotated[SaveAction, Tag("save")],
        Annotated[SetValueAction, Tag("set")],
    ],
    Discriminator(lambda m: get_discriminator(m, "action", "action")),
]


StepEventActionsSubmitTypedDict = TypeAliasType(
    "StepEventActionsSubmitTypedDict",
    Union[
        IncrementActionTypedDict,
        SaveActionTypedDict,
        SayActionTypedDict,
        SetValueActionTypedDict,
        CallActionTypedDict,
    ],
)


StepEventActionsSubmit = Annotated[
    Union[
        Annotated[CallAction, Tag("call")],
        Annotated[IncrementAction, Tag("inc")],
        Annotated[SaveAction, Tag("save")],
        Annotated[SayAction, Tag("say")],
        Annotated[SetValueAction, Tag("set")],
    ],
    Discriminator(lambda m: get_discriminator(m, "action", "action")),
]


class StepEventActionsTypedDict(TypedDict):
    r"""Actions to perform when events occur (enter, presubmit, submit)."""

    start: NotRequired[Nullable[List[StepEventActionsStartTypedDict]]]
    r"""Actions to execute on the first input from the user."""
    enter: NotRequired[Nullable[List[EnterTypedDict]]]
    r"""Actions to execute when entering a step (before collecting inputs)."""
    presubmit: NotRequired[Nullable[List[PresubmitTypedDict]]]
    r"""Actions to execute before validation (data-mutation only: set, inc, save). Use this to set default values for required fields that would otherwise fail validation."""
    submit: NotRequired[Nullable[List[StepEventActionsSubmitTypedDict]]]
    r"""Actions to execute when the tool/step is submitted by the LLM."""


class StepEventActions(BaseModel):
    r"""Actions to perform when events occur (enter, presubmit, submit)."""

    start: OptionalNullable[List[StepEventActionsStart]] = UNSET
    r"""Actions to execute on the first input from the user."""

    enter: OptionalNullable[List[Enter]] = UNSET
    r"""Actions to execute when entering a step (before collecting inputs)."""

    presubmit: OptionalNullable[List[Presubmit]] = UNSET
    r"""Actions to execute before validation (data-mutation only: set, inc, save). Use this to set default values for required fields that would otherwise fail validation."""

    submit: OptionalNullable[List[StepEventActionsSubmit]] = UNSET
    r"""Actions to execute when the tool/step is submitted by the LLM."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(["start", "enter", "presubmit", "submit"])
        nullable_fields = set(["start", "enter", "presubmit", "submit"])
        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
