from __future__ import annotations

import datetime
from collections.abc import Mapping
from typing import TYPE_CHECKING, Any, TypeVar

from attrs import define as _attrs_define
from attrs import field as _attrs_field
from dateutil.parser import isoparse

from ..types import UNSET, Unset

if TYPE_CHECKING:
    from ..models.job_job_data import JobJobData


T = TypeVar("T", bound="Job")


@_attrs_define
class Job:
    """
    Attributes:
        field_id (str | Unset): MongoDB ObjectId
        team (str | Unset): MongoDB ObjectId
        user (str | Unset): MongoDB ObjectId
        success (bool | Unset):
        working (bool | Unset):
        error (str | Unset):
        worker (str | Unset):
        queue_time (datetime.datetime | Unset):
        start_time (datetime.datetime | Unset):
        end_time (datetime.datetime | Unset):
        job_type (str | Unset):
        job_id (str | Unset):
        job_data (JobJobData | Unset):
    """

    field_id: str | Unset = UNSET
    team: str | Unset = UNSET
    user: str | Unset = UNSET
    success: bool | Unset = UNSET
    working: bool | Unset = UNSET
    error: str | Unset = UNSET
    worker: str | Unset = UNSET
    queue_time: datetime.datetime | Unset = UNSET
    start_time: datetime.datetime | Unset = UNSET
    end_time: datetime.datetime | Unset = UNSET
    job_type: str | Unset = UNSET
    job_id: str | Unset = UNSET
    job_data: JobJobData | Unset = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

    def to_dict(self) -> dict[str, Any]:
        field_id = self.field_id

        team = self.team

        user = self.user

        success = self.success

        working = self.working

        error = self.error

        worker = self.worker

        queue_time: str | Unset = UNSET
        if not isinstance(self.queue_time, Unset):
            queue_time = self.queue_time.isoformat()

        start_time: str | Unset = UNSET
        if not isinstance(self.start_time, Unset):
            start_time = self.start_time.isoformat()

        end_time: str | Unset = UNSET
        if not isinstance(self.end_time, Unset):
            end_time = self.end_time.isoformat()

        job_type = self.job_type

        job_id = self.job_id

        job_data: dict[str, Any] | Unset = UNSET
        if not isinstance(self.job_data, Unset):
            job_data = self.job_data.to_dict()

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update({})
        if field_id is not UNSET:
            field_dict["_id"] = field_id
        if team is not UNSET:
            field_dict["team"] = team
        if user is not UNSET:
            field_dict["user"] = user
        if success is not UNSET:
            field_dict["success"] = success
        if working is not UNSET:
            field_dict["working"] = working
        if error is not UNSET:
            field_dict["error"] = error
        if worker is not UNSET:
            field_dict["worker"] = worker
        if queue_time is not UNSET:
            field_dict["queueTime"] = queue_time
        if start_time is not UNSET:
            field_dict["startTime"] = start_time
        if end_time is not UNSET:
            field_dict["endTime"] = end_time
        if job_type is not UNSET:
            field_dict["jobType"] = job_type
        if job_id is not UNSET:
            field_dict["jobId"] = job_id
        if job_data is not UNSET:
            field_dict["jobData"] = job_data

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        from ..models.job_job_data import JobJobData

        d = dict(src_dict)
        field_id = d.pop("_id", UNSET)

        team = d.pop("team", UNSET)

        user = d.pop("user", UNSET)

        success = d.pop("success", UNSET)

        working = d.pop("working", UNSET)

        error = d.pop("error", UNSET)

        worker = d.pop("worker", UNSET)

        _queue_time = d.pop("queueTime", UNSET)
        queue_time: datetime.datetime | Unset
        if isinstance(_queue_time, Unset):
            queue_time = UNSET
        else:
            queue_time = isoparse(_queue_time)

        _start_time = d.pop("startTime", UNSET)
        start_time: datetime.datetime | Unset
        if isinstance(_start_time, Unset):
            start_time = UNSET
        else:
            start_time = isoparse(_start_time)

        _end_time = d.pop("endTime", UNSET)
        end_time: datetime.datetime | Unset
        if isinstance(_end_time, Unset):
            end_time = UNSET
        else:
            end_time = isoparse(_end_time)

        job_type = d.pop("jobType", UNSET)

        job_id = d.pop("jobId", UNSET)

        _job_data = d.pop("jobData", UNSET)
        job_data: JobJobData | Unset
        if isinstance(_job_data, Unset):
            job_data = UNSET
        else:
            job_data = JobJobData.from_dict(_job_data)

        job = cls(
            field_id=field_id,
            team=team,
            user=user,
            success=success,
            working=working,
            error=error,
            worker=worker,
            queue_time=queue_time,
            start_time=start_time,
            end_time=end_time,
            job_type=job_type,
            job_id=job_id,
            job_data=job_data,
        )

        job.additional_properties = d
        return job

    @property
    def additional_keys(self) -> list[str]:
        return list(self.additional_properties.keys())

    def __getitem__(self, key: str) -> Any:
        return self.additional_properties[key]

    def __setitem__(self, key: str, value: Any) -> None:
        self.additional_properties[key] = value

    def __delitem__(self, key: str) -> None:
        del self.additional_properties[key]

    def __contains__(self, key: str) -> bool:
        return key in self.additional_properties
