# job_status.py

"""
    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, StrictBool, StrictFloat, StrictInt, StrictStr
from typing import Any, ClassVar, Dict, List, Optional, Union
from civitai_py.models.job_event import JobEvent
from civitai_py.models.job_status_job import JobStatusJob
from civitai_py.models.provider_job_status import ProviderJobStatus
from typing import Optional, Set
from typing_extensions import Self


class JobStatus(BaseModel):
    """
    JobStatus
    """  # noqa: E501
    job: Optional[JobStatusJob] = None
    job_id: Optional[StrictStr] = Field(default=None, alias="jobId")
    cost: Optional[Union[StrictFloat, StrictInt]] = None
    properties: Optional[Dict[str, Any]] = None
    result: Optional[Any] = Field(default=None, description="A optional result of the job")
    last_event: Optional[JobEvent] = Field(default=None, alias="lastEvent")
    service_providers: Optional[Dict[str, ProviderJobStatus]] = Field(
        default=None, description="The position of this job in the different queues for different providers. If this is an empty object then this job remains unscheduled.", alias="serviceProviders")
    scheduled: Optional[StrictBool] = Field(default=None, description="Wether")
    __properties: ClassVar[List[str]] = ["job", "jobId", "cost",
                                         "properties", "result", "lastEvent", "serviceProviders", "scheduled"]

    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 JobStatus 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.
        * OpenAPI `readOnly` fields are excluded.
        """
        excluded_fields: Set[str] = set([
            "job_id",
            "cost",
            "properties",
            "scheduled",
        ])

        _dict = self.model_dump(
            by_alias=True,
            exclude=excluded_fields,
            exclude_none=True,
        )
        # override the default output from pydantic by calling `to_dict()` of job
        if self.job:
            _dict['job'] = self.job.to_dict()
        # override the default output from pydantic by calling `to_dict()` of last_event
        if self.last_event:
            _dict['lastEvent'] = self.last_event.to_dict()
        # override the default output from pydantic by calling `to_dict()` of each value in service_providers (dict)
        _field_dict = {}
        if self.service_providers:
            for _key in self.service_providers:
                if self.service_providers[_key]:
                    _field_dict[_key] = self.service_providers[_key].to_dict()
            _dict['serviceProviders'] = _field_dict
        # set to None if job (nullable) is None
        # and model_fields_set contains the field
        if self.job is None and "job" in self.model_fields_set:
            _dict['job'] = None

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

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

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

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

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

        return _dict

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

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

        _obj = cls.model_validate({
            "job": JobStatusJob.from_dict(obj["job"]) if obj.get("job") is not None else None,
            "jobId": obj.get("jobId"),
            "cost": obj.get("cost"),
            "properties": obj.get("properties"),
            "result": obj.get("result"),
            "lastEvent": JobEvent.from_dict(obj["lastEvent"]) if obj.get("lastEvent") is not None else None,
            "serviceProviders": dict(
                (_k, ProviderJobStatus.from_dict(_v))
                for _k, _v in obj["serviceProviders"].items()
            )
            if obj.get("serviceProviders") is not None
            else None,
            "scheduled": obj.get("scheduled")
        })
        return _obj
