# coding: utf-8

"""
    Picteus

    The Picteus API

    The version of the OpenAPI document: 0.12.0
    Contact: edouard@koppasoft.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
from typing import Any, ClassVar, Dict, List, Optional
from typing_extensions import Annotated
from picteus_ws_client.models.manifest_capability import ManifestCapability
from picteus_ws_client.models.manifest_event import ManifestEvent
from picteus_ws_client.models.manifest_execution import ManifestExecution
from picteus_ws_client.models.manifest_extension_command import ManifestExtensionCommand
from picteus_ws_client.models.manifest_throttling_policy import ManifestThrottlingPolicy
from typing import Optional, Set
from typing_extensions import Self

class ManifestInstructions(BaseModel):
    """
    The full specifications of an extension, including the events it handles, its capabilities, its execution and its commands
    """ # noqa: E501
    events: Annotated[List[ManifestEvent], Field(min_length=1)] = Field(description="The events which will trigger the instructions")
    capabilities: Optional[List[ManifestCapability]] = Field(default=None, description="The capabilities attached to the instructions")
    throttling_policies: Optional[List[ManifestThrottlingPolicy]] = Field(default=None, description="The throttling policies for some events", alias="throttlingPolicies")
    execution: ManifestExecution = Field(description="The execution to run when one of the event of the instructions occurs")
    commands: Optional[List[ManifestExtensionCommand]] = Field(default=None, description="The possible commands of the event")
    __properties: ClassVar[List[str]] = ["events", "capabilities", "throttlingPolicies", "execution", "commands"]

    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 ManifestInstructions 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 capabilities (list)
        _items = []
        if self.capabilities:
            for _item_capabilities in self.capabilities:
                if _item_capabilities:
                    _items.append(_item_capabilities.to_dict())
            _dict['capabilities'] = _items
        # override the default output from pydantic by calling `to_dict()` of each item in throttling_policies (list)
        _items = []
        if self.throttling_policies:
            for _item_throttling_policies in self.throttling_policies:
                if _item_throttling_policies:
                    _items.append(_item_throttling_policies.to_dict())
            _dict['throttlingPolicies'] = _items
        # override the default output from pydantic by calling `to_dict()` of execution
        if self.execution:
            _dict['execution'] = self.execution.to_dict()
        # override the default output from pydantic by calling `to_dict()` of each item in commands (list)
        _items = []
        if self.commands:
            for _item_commands in self.commands:
                if _item_commands:
                    _items.append(_item_commands.to_dict())
            _dict['commands'] = _items
        return _dict

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

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

        _obj = cls.model_validate({
            "events": obj.get("events"),
            "capabilities": [ManifestCapability.from_dict(_item) for _item in obj["capabilities"]] if obj.get("capabilities") is not None else None,
            "throttlingPolicies": [ManifestThrottlingPolicy.from_dict(_item) for _item in obj["throttlingPolicies"]] if obj.get("throttlingPolicies") is not None else None,
            "execution": ManifestExecution.from_dict(obj["execution"]) if obj.get("execution") is not None else None,
            "commands": [ManifestExtensionCommand.from_dict(_item) for _item in obj["commands"]] if obj.get("commands") is not None else None
        })
        return _obj


