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

from __future__ import annotations
from .lookbackwindowdto import LookBackWindowDto, LookBackWindowDtoTypedDict
from .steptypeenum import StepTypeEnum
from enum import Enum
from novu_py.types import BaseModel, Nullable, OptionalNullable, UNSET, UNSET_SENTINEL
import pydantic
from pydantic import model_serializer
from typing import Any, Dict, Optional
from typing_extensions import Annotated, NotRequired, TypedDict


class DigestStepUpsertDtoType(str, Enum):
    r"""The type of digest strategy. Determines which fields are applicable."""

    REGULAR = "regular"
    TIMED = "timed"


class DigestStepUpsertDtoUnit(str, Enum):
    r"""The unit of time for the digest interval (for REGULAR type)."""

    SECONDS = "seconds"
    MINUTES = "minutes"
    HOURS = "hours"
    DAYS = "days"
    WEEKS = "weeks"
    MONTHS = "months"


class DigestStepUpsertDtoControlValuesTypedDict(TypedDict):
    r"""Control values for the Digest step"""

    skip: NotRequired[Dict[str, Any]]
    r"""JSONLogic filter conditions for conditionally skipping the step execution. Supports complex logical operations with AND, OR, and comparison operators. See https://jsonlogic.com/ for full typing reference."""
    type: NotRequired[DigestStepUpsertDtoType]
    r"""The type of digest strategy. Determines which fields are applicable."""
    amount: NotRequired[float]
    r"""The amount of time for the digest interval (for REGULAR type). Min 1."""
    unit: NotRequired[DigestStepUpsertDtoUnit]
    r"""The unit of time for the digest interval (for REGULAR type)."""
    look_back_window: NotRequired[LookBackWindowDtoTypedDict]
    r"""Configuration for look-back window (for REGULAR type)."""
    cron: NotRequired[str]
    r"""Cron expression for TIMED digest. Min length 1."""
    digest_key: NotRequired[str]
    r"""Specify a custom key for digesting events instead of the default event key."""


class DigestStepUpsertDtoControlValues(BaseModel):
    r"""Control values for the Digest step"""

    skip: Optional[Dict[str, Any]] = None
    r"""JSONLogic filter conditions for conditionally skipping the step execution. Supports complex logical operations with AND, OR, and comparison operators. See https://jsonlogic.com/ for full typing reference."""

    type: Optional[DigestStepUpsertDtoType] = None
    r"""The type of digest strategy. Determines which fields are applicable."""

    amount: Optional[float] = None
    r"""The amount of time for the digest interval (for REGULAR type). Min 1."""

    unit: Optional[DigestStepUpsertDtoUnit] = None
    r"""The unit of time for the digest interval (for REGULAR type)."""

    look_back_window: Annotated[
        Optional[LookBackWindowDto], pydantic.Field(alias="lookBackWindow")
    ] = None
    r"""Configuration for look-back window (for REGULAR type)."""

    cron: Optional[str] = None
    r"""Cron expression for TIMED digest. Min length 1."""

    digest_key: Annotated[Optional[str], pydantic.Field(alias="digestKey")] = None
    r"""Specify a custom key for digesting events instead of the default event key."""


class DigestStepUpsertDtoTypedDict(TypedDict):
    name: str
    r"""Name of the step"""
    type: StepTypeEnum
    r"""Type of the step"""
    id: NotRequired[str]
    r"""Unique identifier of the step"""
    control_values: NotRequired[Nullable[DigestStepUpsertDtoControlValuesTypedDict]]
    r"""Control values for the Digest step"""


class DigestStepUpsertDto(BaseModel):
    name: str
    r"""Name of the step"""

    type: StepTypeEnum
    r"""Type of the step"""

    id: Annotated[Optional[str], pydantic.Field(alias="_id")] = None
    r"""Unique identifier of the step"""

    control_values: Annotated[
        OptionalNullable[DigestStepUpsertDtoControlValues],
        pydantic.Field(alias="controlValues"),
    ] = UNSET
    r"""Control values for the Digest step"""

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