# coding: utf-8

"""
    Instana REST API documentation

    Documentation for INSTANA REST API

    The version of the OpenAPI document: 1.309.1268
    Contact: support@instana.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 pydantic import BaseModel, ConfigDict, Field, StrictStr
from typing import Any, ClassVar, Dict, List, Optional
from typing_extensions import Annotated
from instana_client.models.trigger import Trigger
from instana_client.models.type_configuration import TypeConfiguration
from typing import Optional, Set
from typing_extensions import Self

class Policy(BaseModel):
    """
    Policy
    """ # noqa: E501
    description: Optional[Annotated[str, Field(min_length=0, strict=True, max_length=512)]] = Field(default=None, description="Policy description.")
    id: Annotated[str, Field(min_length=1, strict=True, max_length=128)] = Field(description="Policy identifier.")
    name: Annotated[str, Field(min_length=1, strict=True, max_length=128)] = Field(description="Policy name.")
    tags: Optional[List[StrictStr]] = Field(default=None, description="A list of policy tags.")
    trigger: Trigger
    type_configurations: List[TypeConfiguration] = Field(description="List of configurations that contains the list of actions to run and the mode (automatic or manual) in which the policy is run.", alias="typeConfigurations")
    __properties: ClassVar[List[str]] = ["description", "id", "name", "tags", "trigger", "typeConfigurations"]

    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 Policy 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([
            "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 trigger
        if self.trigger:
            _dict['trigger'] = self.trigger.to_dict()
        # override the default output from pydantic by calling `to_dict()` of each item in type_configurations (list)
        _items = []
        if self.type_configurations:
            for _item_type_configurations in self.type_configurations:
                if _item_type_configurations:
                    _items.append(_item_type_configurations.to_dict())
            _dict['typeConfigurations'] = _items
        return _dict

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

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

        _obj = cls.model_validate({
            "description": obj.get("description"),
            "id": obj.get("id"),
            "name": obj.get("name"),
            "tags": obj.get("tags"),
            "trigger": Trigger.from_dict(obj["trigger"]) if obj.get("trigger") is not None else None,
            "typeConfigurations": [TypeConfiguration.from_dict(_item) for _item in obj["typeConfigurations"]] if obj.get("typeConfigurations") is not None else None
        })
        return _obj


