# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# pyformat: disable

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

from __future__ import annotations
from .. import BaseModel, UNSET_SENTINEL, UnrecognizedStr
from ..interactions import interaction as interactions_interaction
from datetime import datetime
from pydantic import model_serializer
from typing import Literal, Optional, Union
from typing_extensions import NotRequired, TypedDict


TriggerStatus = Union[
    Literal[
        "active",
        "paused",
        "error",
    ],
    UnrecognizedStr,
]
r"""Output only. The current status of the trigger."""


class TriggerTypedDict(TypedDict):
    r"""A trigger configuration that is scheduled to run an agent."""

    id: str
    r"""Required. Output only. Identifier. The ID of the trigger."""
    interaction: interactions_interaction.InteractionTypedDict
    r"""The Interaction resource."""
    schedule: str
    r"""Required. The cron schedule on which the trigger should run.
    Standard cron format.
    """
    time_zone: str
    r"""Required. Time zone in which the schedule should be interpreted."""
    consecutive_failure_count: NotRequired[int]
    r"""Output only. The number of consecutive failures that have occurred
    since the last successful execution.
    """
    create_time: NotRequired[datetime]
    r"""Output only. The time when the trigger was created."""
    display_name: NotRequired[str]
    r"""Optional. The display name of the trigger."""
    environment_id: NotRequired[str]
    r"""Optional. The environment ID for the trigger execution."""
    execution_timeout_seconds: NotRequired[int]
    r"""Optional. The execution timeout for the triggered interaction."""
    last_pause_time: NotRequired[datetime]
    r"""Output only. The time when the trigger was last paused."""
    last_resume_time: NotRequired[datetime]
    r"""Output only. The time when the trigger was last resumed."""
    last_run_time: NotRequired[datetime]
    r"""Output only. The time when the trigger was last run."""
    max_consecutive_failures: NotRequired[int]
    r"""Optional. The maximum number of consecutive failures allowed before
    the trigger is automatically paused (status becomes ERROR).
    """
    next_run_time: NotRequired[datetime]
    r"""Output only. The time when the trigger is scheduled to run next."""
    previous_interaction_id: NotRequired[str]
    r"""Output only. The ID of the last interaction created by this trigger."""
    status: NotRequired[TriggerStatus]
    r"""Output only. The current status of the trigger."""
    update_time: NotRequired[datetime]
    r"""Output only. The time when the trigger was last updated."""


class Trigger(BaseModel):
    r"""A trigger configuration that is scheduled to run an agent."""

    id: str
    r"""Required. Output only. Identifier. The ID of the trigger."""

    interaction: interactions_interaction.Interaction
    r"""The Interaction resource."""

    schedule: str
    r"""Required. The cron schedule on which the trigger should run.
    Standard cron format.
    """

    time_zone: str
    r"""Required. Time zone in which the schedule should be interpreted."""

    consecutive_failure_count: Optional[int] = None
    r"""Output only. The number of consecutive failures that have occurred
    since the last successful execution.
    """

    create_time: Optional[datetime] = None
    r"""Output only. The time when the trigger was created."""

    display_name: Optional[str] = None
    r"""Optional. The display name of the trigger."""

    environment_id: Optional[str] = None
    r"""Optional. The environment ID for the trigger execution."""

    execution_timeout_seconds: Optional[int] = None
    r"""Optional. The execution timeout for the triggered interaction."""

    last_pause_time: Optional[datetime] = None
    r"""Output only. The time when the trigger was last paused."""

    last_resume_time: Optional[datetime] = None
    r"""Output only. The time when the trigger was last resumed."""

    last_run_time: Optional[datetime] = None
    r"""Output only. The time when the trigger was last run."""

    max_consecutive_failures: Optional[int] = None
    r"""Optional. The maximum number of consecutive failures allowed before
    the trigger is automatically paused (status becomes ERROR).
    """

    next_run_time: Optional[datetime] = None
    r"""Output only. The time when the trigger is scheduled to run next."""

    previous_interaction_id: Optional[str] = None
    r"""Output only. The ID of the last interaction created by this trigger."""

    status: Optional[TriggerStatus] = None
    r"""Output only. The current status of the trigger."""

    update_time: Optional[datetime] = None
    r"""Output only. The time when the trigger was last updated."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(
            [
                "consecutive_failure_count",
                "create_time",
                "display_name",
                "environment_id",
                "execution_timeout_seconds",
                "last_pause_time",
                "last_resume_time",
                "last_run_time",
                "max_consecutive_failures",
                "next_run_time",
                "previous_interaction_id",
                "status",
                "update_time",
            ]
        )
        serialized = handler(self)
        m = {}

        for n, f in type(self).model_fields.items():
            k = f.alias or n
            val = serialized.get(k, serialized.get(n))

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

        return m
