# coding: utf-8

"""
    Hindsight HTTP API

    HTTP API for Hindsight

    The version of the OpenAPI document: 0.5.3
    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, StrictStr, field_validator
from typing import Any, ClassVar, Dict, List, Optional
from hindsight_client_api.models.child_operation_status import ChildOperationStatus
from typing import Optional, Set
from typing_extensions import Self

class OperationStatusResponse(BaseModel):
    """
    Response model for getting a single operation status.
    """ # noqa: E501
    operation_id: StrictStr
    status: StrictStr
    operation_type: Optional[StrictStr] = None
    created_at: Optional[StrictStr] = None
    updated_at: Optional[StrictStr] = None
    completed_at: Optional[StrictStr] = None
    error_message: Optional[StrictStr] = None
    result_metadata: Optional[Dict[str, Any]] = None
    child_operations: Optional[List[ChildOperationStatus]] = None
    task_payload: Optional[Dict[str, Any]] = None
    __properties: ClassVar[List[str]] = ["operation_id", "status", "operation_type", "created_at", "updated_at", "completed_at", "error_message", "result_metadata", "child_operations", "task_payload"]

    @field_validator('status')
    def status_validate_enum(cls, value):
        """Validates the enum"""
        if value not in set(['pending', 'completed', 'failed', 'not_found']):
            raise ValueError("must be one of enum values ('pending', 'completed', 'failed', 'not_found')")
        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 OperationStatusResponse 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 child_operations (list)
        _items = []
        if self.child_operations:
            for _item_child_operations in self.child_operations:
                if _item_child_operations:
                    _items.append(_item_child_operations.to_dict())
            _dict['child_operations'] = _items
        # set to None if operation_type (nullable) is None
        # and model_fields_set contains the field
        if self.operation_type is None and "operation_type" in self.model_fields_set:
            _dict['operation_type'] = None

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

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

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

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

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

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

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

        return _dict

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

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

        _obj = cls.model_validate({
            "operation_id": obj.get("operation_id"),
            "status": obj.get("status"),
            "operation_type": obj.get("operation_type"),
            "created_at": obj.get("created_at"),
            "updated_at": obj.get("updated_at"),
            "completed_at": obj.get("completed_at"),
            "error_message": obj.get("error_message"),
            "result_metadata": obj.get("result_metadata"),
            "child_operations": [ChildOperationStatus.from_dict(_item) for _item in obj["child_operations"]] if obj.get("child_operations") is not None else None,
            "task_payload": obj.get("task_payload")
        })
        return _obj


