# 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 datetime import datetime
from pydantic import BaseModel, ConfigDict, Field, StrictStr, field_validator
from typing import Any, ClassVar, Dict, List, Optional
from typing_extensions import Annotated
from instana_client.models.meta_data import MetaData
from instana_client.models.model_field import ModelField
from instana_client.models.parameter import Parameter
from typing import Optional, Set
from typing_extensions import Self

class Action(BaseModel):
    """
    Automation action definition. When this is used in policy creation request, only `id` should be specified.
    """ # noqa: E501
    created_at: datetime = Field(description="Action created time.", alias="createdAt")
    description: Optional[Annotated[str, Field(min_length=0, strict=True, max_length=512)]] = Field(default=None, description="Action description.")
    fields: Optional[List[ModelField]] = Field(default=None, description="List of fields that describe an action.")
    id: Annotated[str, Field(min_length=1, strict=True, max_length=128)] = Field(description="Action identifier.")
    input_parameters: Optional[List[Parameter]] = Field(default=None, description="List of inputs to the action.", alias="inputParameters")
    metadata: Optional[MetaData] = None
    modified_at: datetime = Field(description="Action modified time.", alias="modifiedAt")
    name: Annotated[str, Field(min_length=1, strict=True, max_length=128)] = Field(description="Action name.")
    tags: Optional[List[StrictStr]] = Field(default=None, description="List of tags added to the action.")
    type: Annotated[str, Field(min_length=0, strict=True, max_length=128)] = Field(description="Action type can be one of the following values:  SCRIPT, HTTP, ANSIBLE, EXTERNAL, GITHUB, GITLAB, JIRA, MANUAL, DOC_LINK")
    __properties: ClassVar[List[str]] = ["createdAt", "description", "fields", "id", "inputParameters", "metadata", "modifiedAt", "name", "tags", "type"]

    @field_validator('type')
    def type_validate_enum(cls, value):
        """Validates the enum"""
        if value not in set(['SCRIPT', 'HTTP', 'ANSIBLE', 'EXTERNAL', 'GITHUB', 'GITLAB', 'JIRA', 'MANUAL', 'DOC_LINK']):
            raise ValueError("must be one of enum values ('SCRIPT', 'HTTP', 'ANSIBLE', 'EXTERNAL', 'GITHUB', 'GITLAB', 'JIRA', 'MANUAL', 'DOC_LINK')")
        return value

    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 Action 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,
        )
        # override the default output from pydantic by calling `to_dict()` of each item in fields (list)
        _items = []
        if self.fields:
            for _item_fields in self.fields:
                if _item_fields:
                    _items.append(_item_fields.to_dict())
            _dict['fields'] = _items
        # override the default output from pydantic by calling `to_dict()` of each item in input_parameters (list)
        _items = []
        if self.input_parameters:
            for _item_input_parameters in self.input_parameters:
                if _item_input_parameters:
                    _items.append(_item_input_parameters.to_dict())
            _dict['inputParameters'] = _items
        # override the default output from pydantic by calling `to_dict()` of metadata
        if self.metadata:
            _dict['metadata'] = self.metadata.to_dict()
        return _dict

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

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

        _obj = cls.model_validate({
            "createdAt": obj.get("createdAt"),
            "description": obj.get("description"),
            "fields": [ModelField.from_dict(_item) for _item in obj["fields"]] if obj.get("fields") is not None else None,
            "id": obj.get("id"),
            "inputParameters": [Parameter.from_dict(_item) for _item in obj["inputParameters"]] if obj.get("inputParameters") is not None else None,
            "metadata": MetaData.from_dict(obj["metadata"]) if obj.get("metadata") is not None else None,
            "modifiedAt": obj.get("modifiedAt"),
            "name": obj.get("name"),
            "tags": obj.get("tags"),
            "type": obj.get("type")
        })
        return _obj


