# coding: utf-8

"""
    Alerts API

    **Note:** API operations for the creation or retrieval of API, Page Load, or Web-Transaction alert rules are not available for ThousandEyes for Government instance.  You can manage the following alert functionalities on the ThousandEyes platform using the Alerts API:  * **Alerts**: Retrieve alert details. Alerts are assigned to tests through alert rules.  * **Alert Rules**: Conditions that you configure in order to highlight or be notified of events of interest in your ThousandEyes tests. When an alert rule’s conditions are met, the associated alert is triggered and the alert becomes active. It remains active until the alert is cleared. Alert rules are reusable across multiple tests..  * **Alert Suppression Windows**: Suppress alerts for tests during periods such as planned maintenance. Windows can be one-time events or recurring events to handle periodic occurrences such as monthly downtime for maintenance.  For more information about the alerts, see [Alerts](https://docs.thousandeyes.com/product-documentation/alerts). 

    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 pydantic import BaseModel, ConfigDict, Field, StrictBool, StrictInt, StrictStr
from typing import Any, ClassVar, Dict, List, Optional
from thousandeyes_sdk.alerts.models.alert_direction import AlertDirection
from thousandeyes_sdk.alerts.models.alert_group_type import AlertGroupType
from thousandeyes_sdk.alerts.models.alert_notification import AlertNotification
from thousandeyes_sdk.alerts.models.alert_rounds_violation_mode import AlertRoundsViolationMode
from thousandeyes_sdk.alerts.models.alert_type import AlertType
from thousandeyes_sdk.alerts.models.sensitivity_level import SensitivityLevel
from thousandeyes_sdk.alerts.models.severity import Severity
from typing import Optional, Set
from typing_extensions import Self

class RuleDetailUpdate(BaseModel):
    """
    RuleDetailUpdate
    """ # noqa: E501
    rule_id: Optional[StrictStr] = Field(default=None, description="Unique ID of the rule.", alias="ruleId")
    rule_name: StrictStr = Field(description="Name of the alert rule.", alias="ruleName")
    expression: StrictStr = Field(description="The expression of the alert rule.")
    description: Optional[StrictStr] = Field(default=None, description="A description of the alert rule.")
    direction: Optional[AlertDirection] = None
    notify_on_clear: Optional[StrictBool] = Field(default=None, description="Send notification when alert clears.", alias="notifyOnClear")
    is_default: Optional[StrictBool] = Field(default=None, description="If set to `true`, this alert rule becomes the default for its test type and is automatically applied to newly created tests with relevant metrics. Only one default alert rule is allowed per test type.", alias="isDefault")
    alert_type: AlertType = Field(alias="alertType")
    alert_group_type: Optional[AlertGroupType] = Field(default=None, alias="alertGroupType")
    minimum_sources: Optional[StrictInt] = Field(default=None, description="The minimum number of agents or monitors that must meet the specified criteria to trigger the alert.", alias="minimumSources")
    minimum_sources_pct: Optional[StrictInt] = Field(default=None, description="The minimum percentage of all assigned agents or monitors that must meet the specified criteria to trigger the alert.", alias="minimumSourcesPct")
    rounds_violating_mode: Optional[AlertRoundsViolationMode] = Field(default=None, alias="roundsViolatingMode")
    rounds_violating_out_of: StrictInt = Field(description="Specifies the divisor (y value) in the “X of Y times” condition.", alias="roundsViolatingOutOf")
    rounds_violating_required: Optional[StrictInt] = Field(default=None, description="Specifies the numerator (x value) in the “X of Y times” condition.", alias="roundsViolatingRequired")
    include_covered_prefixes: Optional[StrictBool] = Field(default=None, description="Set true to include covered prefixes in the BGP alert rule. Only applicable to BGP alert rules.", alias="includeCoveredPrefixes")
    sensitivity_level: Optional[SensitivityLevel] = Field(default=None, alias="sensitivityLevel")
    severity: Optional[Severity] = None
    endpoint_agent_ids: Optional[List[StrictStr]] = Field(default=None, description="An array of endpoint agent IDs associated with the rule (get `id` from `/endpoint/agents` API). This is applicable when `alertGroupType` is `browser-session`.", alias="endpointAgentIds")
    endpoint_label_ids: Optional[List[StrictStr]] = Field(default=None, description="An array of label IDs used to assign specific Endpoint Agents to the test (get `id` from `/endpoint/labels`). This is applicable when `alertGroupType` is `browser-session`.", alias="endpointLabelIds")
    visited_sites_filter: Optional[List[StrictStr]] = Field(default=None, description="A list of website domains visited during the session. This is applicable when `alertGroupType` is `browser-session`.", alias="visitedSitesFilter")
    notifications: Optional[AlertNotification] = None
    test_ids: Optional[List[StrictStr]] = Field(default=None, description="Array of test IDs to link to alert rule (get `testId` from `/tests` endpoint).", alias="testIds")
    __properties: ClassVar[List[str]] = ["ruleId", "ruleName", "expression", "description", "direction", "notifyOnClear", "isDefault", "alertType", "alertGroupType", "minimumSources", "minimumSourcesPct", "roundsViolatingMode", "roundsViolatingOutOf", "roundsViolatingRequired", "includeCoveredPrefixes", "sensitivityLevel", "severity", "endpointAgentIds", "endpointLabelIds", "visitedSitesFilter", "notifications", "testIds"]

    model_config = ConfigDict(
        populate_by_name=True,
        validate_assignment=True,
        protected_namespaces=(),
        extra="allow",
    )


    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 self.model_dump_json(by_alias=True, exclude_unset=True, exclude_none=True)

    @classmethod
    def from_json(cls, json_str: str) -> Optional[Self]:
        """Create an instance of RuleDetailUpdate 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.
        * OpenAPI `readOnly` fields are excluded.
        """
        excluded_fields: Set[str] = set([
            "rule_id",
        ])

        _dict = self.model_dump(
            by_alias=True,
            exclude=excluded_fields,
            exclude_none=True,
        )
        # override the default output from pydantic by calling `to_dict()` of notifications
        if self.notifications:
            _dict['notifications'] = self.notifications.to_dict()
        return _dict

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

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

        _obj = cls.model_validate({
            "ruleId": obj.get("ruleId"),
            "ruleName": obj.get("ruleName"),
            "expression": obj.get("expression"),
            "description": obj.get("description"),
            "direction": obj.get("direction"),
            "notifyOnClear": obj.get("notifyOnClear"),
            "isDefault": obj.get("isDefault"),
            "alertType": obj.get("alertType"),
            "alertGroupType": obj.get("alertGroupType"),
            "minimumSources": obj.get("minimumSources"),
            "minimumSourcesPct": obj.get("minimumSourcesPct"),
            "roundsViolatingMode": obj.get("roundsViolatingMode"),
            "roundsViolatingOutOf": obj.get("roundsViolatingOutOf"),
            "roundsViolatingRequired": obj.get("roundsViolatingRequired"),
            "includeCoveredPrefixes": obj.get("includeCoveredPrefixes"),
            "sensitivityLevel": obj.get("sensitivityLevel"),
            "severity": obj.get("severity"),
            "endpointAgentIds": obj.get("endpointAgentIds"),
            "endpointLabelIds": obj.get("endpointLabelIds"),
            "visitedSitesFilter": obj.get("visitedSitesFilter"),
            "notifications": AlertNotification.from_dict(obj["notifications"]) if obj.get("notifications") is not None else None,
            "testIds": obj.get("testIds")
        })
        return _obj


