"""
RCABench API

RCABench - A comprehensive root cause analysis benchmarking platform for microservices

The version of the OpenAPI document: 1.1.51
Contact: team@rcabench.com
Generated by OpenAPI Generator (https://rcabench.openapi-generator.tech)

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

from __future__ import annotations

import json
import pprint
from typing import Annotated, Any, ClassVar

from pydantic import BaseModel, ConfigDict, Field, StrictStr
from typing_extensions import Self

from rcabench.openapi.models.chaos_node import ChaosNode
from rcabench.openapi.models.container_spec import ContainerSpec
from rcabench.openapi.models.label_item import LabelItem


class SubmitInjectionReq(BaseModel):
    """
    SubmitInjectionReq
    """  # noqa: E501

    algorithms: list[ContainerSpec] | None = Field(default=None, description="RCA algorithms to execute (optional)")
    benchmark: ContainerSpec = Field(description="Benchmark (detector) configuration")
    interval: Annotated[int, Field(strict=True, ge=1)] = Field(description="Total experiment interval in minutes")
    labels: list[LabelItem] | None = Field(default=None, description="Labels to attach to the injection")
    pedestal: ContainerSpec = Field(description="Pedestal (workload) configuration")
    pre_duration: Annotated[int, Field(strict=True, ge=1)] = Field(
        description="Normal data collection duration before fault injection"
    )
    project_name: StrictStr = Field(description="Project name")
    specs: list[list[ChaosNode]] = Field(
        description="Fault injection specs - 2D array where each sub-array is a batch of parallel faults"
    )
    additional_properties: dict[str, Any] = {}
    __properties: ClassVar[list[str]] = [
        "algorithms",
        "benchmark",
        "interval",
        "labels",
        "pedestal",
        "pre_duration",
        "project_name",
        "specs",
    ]

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

    @classmethod
    def from_json(cls, json_str: str) -> Self | None:
        """Create an instance of SubmitInjectionReq 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.
        * Fields in `self.additional_properties` are added to the output dict.
        """
        excluded_fields: set[str] = set(
            [
                "additional_properties",
            ]
        )

        _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 algorithms (list)
        _items = []
        if self.algorithms:
            for _item_algorithms in self.algorithms:
                if _item_algorithms:
                    _items.append(_item_algorithms.to_dict())
            _dict["algorithms"] = _items
        # override the default output from pydantic by calling `to_dict()` of benchmark
        if self.benchmark:
            _dict["benchmark"] = self.benchmark.to_dict()
        # override the default output from pydantic by calling `to_dict()` of each item in labels (list)
        _items = []
        if self.labels:
            for _item_labels in self.labels:
                if _item_labels:
                    _items.append(_item_labels.to_dict())
            _dict["labels"] = _items
        # override the default output from pydantic by calling `to_dict()` of pedestal
        if self.pedestal:
            _dict["pedestal"] = self.pedestal.to_dict()
        # override the default output from pydantic by calling `to_dict()` of each item in specs (list of list)
        _items = []
        if self.specs:
            for _item_specs in self.specs:
                if _item_specs:
                    _items.append([_inner_item.to_dict() for _inner_item in _item_specs if _inner_item is not None])
            _dict["specs"] = _items
        # puts key-value pairs in additional_properties in the top level
        if self.additional_properties is not None:
            for _key, _value in self.additional_properties.items():
                _dict[_key] = _value

        return _dict

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

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

        _obj = cls.model_validate(
            {
                "algorithms": [ContainerSpec.from_dict(_item) for _item in obj["algorithms"]]
                if obj.get("algorithms") is not None
                else None,
                "benchmark": ContainerSpec.from_dict(obj["benchmark"]) if obj.get("benchmark") is not None else None,
                "interval": obj.get("interval"),
                "labels": [LabelItem.from_dict(_item) for _item in obj["labels"]]
                if obj.get("labels") is not None
                else None,
                "pedestal": ContainerSpec.from_dict(obj["pedestal"]) if obj.get("pedestal") is not None else None,
                "pre_duration": obj.get("pre_duration"),
                "project_name": obj.get("project_name"),
                "specs": [[ChaosNode.from_dict(_inner_item) for _inner_item in _item] for _item in obj["specs"]]
                if obj.get("specs") is not None
                else None,
            }
        )
        # store additional fields in additional_properties
        for _key in obj.keys():
            if _key not in cls.__properties:
                _obj.additional_properties[_key] = obj.get(_key)

        return _obj
