# coding: utf-8

"""
    Mixpeek API

    This is the Mixpeek API, providing access to various endpoints for data processing and retrieval.

    The version of the OpenAPI document: 1.3.0
    Contact: info@mixpeek.com
    Generated by OpenAPI Generator (https://openapi-generator.tech)

    Do not edit the class manually.
"""  # noqa: E501


from __future__ import annotations
import pprint
import re  # noqa: F401
import json

from datetime import datetime
from pydantic import BaseModel, ConfigDict, Field, StrictInt, StrictStr
from typing import Any, ClassVar, Dict, List, Optional
from mixpeek.models.shared_triggers_models_trigger_status import SharedTriggersModelsTriggerStatus
from mixpeek.models.shared_triggers_models_trigger_type import SharedTriggersModelsTriggerType
from mixpeek.models.trigger_action_type import TriggerActionType
from typing import Optional, Set
from typing_extensions import Self

class SharedTriggersModelsTriggerModel(BaseModel):
    """
    Unified trigger model for all action types.  A trigger defines: 1. **What** to execute (action_type + action_config) 2. **When** to execute (trigger_type + schedule_config) 3. **State** tracking (execution count, failures, next scheduled time)
    """ # noqa: E501
    trigger_id: Optional[StrictStr] = Field(default=None, description="Unique trigger identifier")
    namespace_id: StrictStr = Field(description="Namespace ID")
    internal_id: StrictStr = Field(description="Organization internal ID")
    action_type: TriggerActionType = Field(description="Type of action to execute")
    action_config: Dict[str, Any] = Field(description="Action-specific configuration")
    trigger_type: SharedTriggersModelsTriggerType = Field(description="Type of schedule")
    schedule_config: Dict[str, Any] = Field(description="Schedule-specific configuration")
    status: Optional[SharedTriggersModelsTriggerStatus] = Field(default=None, description="Current trigger status")
    last_triggered_at: Optional[datetime] = Field(default=None, description="Last time trigger fired")
    last_execution_task_id: Optional[StrictStr] = Field(default=None, description="Task ID of last execution")
    next_scheduled_at: Optional[datetime] = Field(default=None, description="Next scheduled execution time")
    execution_count: Optional[StrictInt] = Field(default=0, description="Total successful executions")
    consecutive_failures: Optional[StrictInt] = Field(default=0, description="Consecutive failures")
    last_execution_status: Optional[StrictStr] = Field(default=None, description="Status of last execution")
    last_execution_error: Optional[StrictStr] = Field(default=None, description="Error from last execution (if failed)")
    event_counter: Optional[StrictInt] = Field(default=0, description="Current event count since last trigger")
    last_cooldown_at: Optional[datetime] = Field(default=None, description="Last time cooldown was applied")
    baseline_snapshot: Optional[Dict[str, Any]] = Field(default=None, description="Baseline snapshot for drift measurement (captured after successful execution)")
    last_drift_measurement: Optional[Dict[str, Any]] = Field(default=None, description="Result of most recent drift measurement check")
    last_condition_check_at: Optional[datetime] = Field(default=None, description="When condition was last evaluated")
    description: Optional[StrictStr] = Field(default=None, description="Human-readable description")
    created_at: Optional[datetime] = Field(default=None, description="Creation timestamp")
    updated_at: Optional[datetime] = Field(default=None, description="Last update timestamp")
    created_by: Optional[StrictStr] = Field(default=None, description="User who created trigger")
    __properties: ClassVar[List[str]] = ["trigger_id", "namespace_id", "internal_id", "action_type", "action_config", "trigger_type", "schedule_config", "status", "last_triggered_at", "last_execution_task_id", "next_scheduled_at", "execution_count", "consecutive_failures", "last_execution_status", "last_execution_error", "event_counter", "last_cooldown_at", "baseline_snapshot", "last_drift_measurement", "last_condition_check_at", "description", "created_at", "updated_at", "created_by"]

    model_config = ConfigDict(
        populate_by_name=True,
        validate_assignment=True,
        protected_namespaces=(),
    )


    def to_str(self) -> str:
        """Returns the string representation of the model using alias"""
        return pprint.pformat(self.model_dump(by_alias=True))

    def to_json(self) -> str:
        """Returns the JSON representation of the model using alias"""
        # TODO: pydantic v2: use .model_dump_json(by_alias=True, exclude_unset=True) instead
        return json.dumps(self.to_dict())

    @classmethod
    def from_json(cls, json_str: str) -> Optional[Self]:
        """Create an instance of SharedTriggersModelsTriggerModel from a JSON string"""
        return cls.from_dict(json.loads(json_str))

    def to_dict(self) -> Dict[str, Any]:
        """Return the dictionary representation of the model using alias.

        This has the following differences from calling pydantic's
        `self.model_dump(by_alias=True)`:

        * `None` is only added to the output dict for nullable fields that
          were set at model initialization. Other fields with value `None`
          are ignored.
        """
        excluded_fields: Set[str] = set([
        ])

        _dict = self.model_dump(
            by_alias=True,
            exclude=excluded_fields,
            exclude_none=True,
        )
        return _dict

    @classmethod
    def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:
        """Create an instance of SharedTriggersModelsTriggerModel from a dict"""
        if obj is None:
            return None

        if not isinstance(obj, dict):
            return cls.model_validate(obj)

        _obj = cls.model_validate({
            "trigger_id": obj.get("trigger_id"),
            "namespace_id": obj.get("namespace_id"),
            "internal_id": obj.get("internal_id"),
            "action_type": obj.get("action_type"),
            "action_config": obj.get("action_config"),
            "trigger_type": obj.get("trigger_type"),
            "schedule_config": obj.get("schedule_config"),
            "status": obj.get("status"),
            "last_triggered_at": obj.get("last_triggered_at"),
            "last_execution_task_id": obj.get("last_execution_task_id"),
            "next_scheduled_at": obj.get("next_scheduled_at"),
            "execution_count": obj.get("execution_count") if obj.get("execution_count") is not None else 0,
            "consecutive_failures": obj.get("consecutive_failures") if obj.get("consecutive_failures") is not None else 0,
            "last_execution_status": obj.get("last_execution_status"),
            "last_execution_error": obj.get("last_execution_error"),
            "event_counter": obj.get("event_counter") if obj.get("event_counter") is not None else 0,
            "last_cooldown_at": obj.get("last_cooldown_at"),
            "baseline_snapshot": obj.get("baseline_snapshot"),
            "last_drift_measurement": obj.get("last_drift_measurement"),
            "last_condition_check_at": obj.get("last_condition_check_at"),
            "description": obj.get("description"),
            "created_at": obj.get("created_at"),
            "updated_at": obj.get("updated_at"),
            "created_by": obj.get("created_by")
        })
        return _obj


