"""
RCABench API

RCABench - A comprehensive root cause analysis benchmarking platform for microservices

The version of the OpenAPI document: 1.1.44
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 Any, ClassVar

from pydantic import BaseModel, ConfigDict, Field, StrictBool, StrictInt, StrictStr, field_validator
from typing_extensions import Self

from rcabench.openapi.models.label_item import LabelItem


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

    benchmarks: list[StrictStr] | None = None
    created_at_gte: StrictStr | None = None
    created_at_lte: StrictStr | None = None
    end_time_gte: StrictStr | None = None
    end_time_lte: StrictStr | None = None
    fault_types: list[StrictInt] | None = None
    include_labels: StrictBool | None = Field(default=None, description="Whether to include labels in the response")
    include_task: StrictBool | None = Field(default=None, description="Whether to include task details in the response")
    labels: list[LabelItem] | None = Field(default=None, description="Custom labels to filter by")
    page: StrictInt | None = None
    search: StrictStr | None = None
    size: StrictInt | None = None
    sort_by: StrictStr | None = None
    sort_order: StrictStr | None = None
    start_time_gte: StrictStr | None = None
    start_time_lte: StrictStr | None = None
    statuses: list[StrictInt] | None = None
    tags: list[StrictStr] | None = Field(default=None, description="Tag values to filter by")
    task_ids: list[StrictStr] | None = None
    additional_properties: dict[str, Any] = {}
    __properties: ClassVar[list[str]] = [
        "benchmarks",
        "created_at_gte",
        "created_at_lte",
        "end_time_gte",
        "end_time_lte",
        "fault_types",
        "include_labels",
        "include_task",
        "labels",
        "page",
        "search",
        "size",
        "sort_by",
        "sort_order",
        "start_time_gte",
        "start_time_lte",
        "statuses",
        "tags",
        "task_ids",
    ]

    @field_validator("sort_by")
    def sort_by_validate_enum(cls, value):
        """Validates the enum"""
        if value is None:
            return value

        if value not in set(
            ["id", "task_id", "fault_type", "status", "benchmark", "injection_name", "created_at", "updated_at"]
        ):
            raise ValueError(
                "must be one of enum values ('id', 'task_id', 'fault_type', 'status', 'benchmark', 'injection_name', 'created_at', 'updated_at')"
            )
        return value

    @field_validator("sort_order")
    def sort_order_validate_enum(cls, value):
        """Validates the enum"""
        if value is None:
            return value

        if value not in set(["asc", "desc"]):
            raise ValueError("must be one of enum values ('asc', 'desc')")
        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) -> Self | None:
        """Create an instance of SearchInjectionReq 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 labels (list)
        _items = []
        if self.labels:
            for _item_labels in self.labels:
                if _item_labels:
                    _items.append(_item_labels.to_dict())
            _dict["labels"] = _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 SearchInjectionReq from a dict"""
        if obj is None:
            return None

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

        _obj = cls.model_validate(
            {
                "benchmarks": obj.get("benchmarks"),
                "created_at_gte": obj.get("created_at_gte"),
                "created_at_lte": obj.get("created_at_lte"),
                "end_time_gte": obj.get("end_time_gte"),
                "end_time_lte": obj.get("end_time_lte"),
                "fault_types": obj.get("fault_types"),
                "include_labels": obj.get("include_labels"),
                "include_task": obj.get("include_task"),
                "labels": [LabelItem.from_dict(_item) for _item in obj["labels"]]
                if obj.get("labels") is not None
                else None,
                "page": obj.get("page"),
                "search": obj.get("search"),
                "size": obj.get("size"),
                "sort_by": obj.get("sort_by"),
                "sort_order": obj.get("sort_order"),
                "start_time_gte": obj.get("start_time_gte"),
                "start_time_lte": obj.get("start_time_lte"),
                "statuses": obj.get("statuses"),
                "tags": obj.get("tags"),
                "task_ids": obj.get("task_ids"),
            }
        )
        # 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
