# coding: utf-8

"""
    Civitai Orchestration Consumer API

    No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)

    The version of the OpenAPI document: v1
    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, StrictInt, StrictStr
from typing import Any, ClassVar, Dict, List, Optional
from civitai_py.models.method_base import MethodBase
from typing import Optional, Set
from typing_extensions import Self


class Exception(BaseModel):
    """
    Exception
    """  # noqa: E501
    target_site: Optional[MethodBase] = Field(default=None, alias="targetSite")
    message: Optional[StrictStr] = None
    data: Optional[Dict[str, Any]] = None
    inner_exception: Optional[Exception] = Field(default=None, alias="innerException")
    help_link: Optional[StrictStr] = Field(default=None, alias="helpLink")
    source: Optional[StrictStr] = None
    h_result: Optional[StrictInt] = Field(default=None, alias="hResult")
    stack_trace: Optional[StrictStr] = Field(default=None, alias="stackTrace")
    __properties: ClassVar[List[str]] = ["targetSite", "message", "data",
                                         "innerException", "helpLink", "source", "hResult", "stackTrace"]

    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 Exception 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.
        * OpenAPI `readOnly` fields are excluded.
        * OpenAPI `readOnly` fields are excluded.
        """
        excluded_fields: Set[str] = set([
            "message",
            "data",
            "stack_trace",
        ])

        _dict = self.model_dump(
            by_alias=True,
            exclude=excluded_fields,
            exclude_none=True,
        )
        # override the default output from pydantic by calling `to_dict()` of target_site
        if self.target_site:
            _dict['targetSite'] = self.target_site.to_dict()
        # override the default output from pydantic by calling `to_dict()` of inner_exception
        if self.inner_exception:
            _dict['innerException'] = self.inner_exception.to_dict()
        # set to None if message (nullable) is None
        # and model_fields_set contains the field
        if self.message is None and "message" in self.model_fields_set:
            _dict['message'] = None

        # set to None if data (nullable) is None
        # and model_fields_set contains the field
        if self.data is None and "data" in self.model_fields_set:
            _dict['data'] = None

        # set to None if help_link (nullable) is None
        # and model_fields_set contains the field
        if self.help_link is None and "help_link" in self.model_fields_set:
            _dict['helpLink'] = None

        # set to None if source (nullable) is None
        # and model_fields_set contains the field
        if self.source is None and "source" in self.model_fields_set:
            _dict['source'] = None

        # set to None if stack_trace (nullable) is None
        # and model_fields_set contains the field
        if self.stack_trace is None and "stack_trace" in self.model_fields_set:
            _dict['stackTrace'] = None

        return _dict

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

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

        _obj = cls.model_validate({
            "targetSite": MethodBase.from_dict(obj["targetSite"]) if obj.get("targetSite") is not None else None,
            "message": obj.get("message"),
            "data": obj.get("data"),
            "innerException": Exception.from_dict(obj["innerException"]) if obj.get("innerException") is not None else None,
            "helpLink": obj.get("helpLink"),
            "source": obj.get("source"),
            "hResult": obj.get("hResult"),
            "stackTrace": obj.get("stackTrace")
        })
        return _obj


# TODO: Rewrite to not use raise_errors
Exception.model_rebuild(raise_errors=False)
