from __future__ import annotations

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

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.script_schedule import ScriptSchedule


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


@_attrs_define
class Script:
    """
    Attributes:
        field_id (str | Unset): MongoDB ObjectId
        team (str | Unset): MongoDB ObjectId
        name (str | Unset):
        requirements (list[str] | Unset):
        source (str | Unset):
        language (str | Unset):
        schedule (ScriptSchedule | Unset):
        running (bool | Unset):
        failed (bool | Unset):
        error (str | Unset):
        traceback (str | Unset):
        last_run (datetime.datetime | Unset):
        created_on (datetime.datetime | Unset):
        last_modified (datetime.datetime | Unset):
    """

    field_id: str | Unset = UNSET
    team: str | Unset = UNSET
    name: str | Unset = UNSET
    requirements: list[str] | Unset = UNSET
    source: str | Unset = UNSET
    language: str | Unset = UNSET
    schedule: ScriptSchedule | Unset = UNSET
    running: bool | Unset = UNSET
    failed: bool | Unset = UNSET
    error: str | Unset = UNSET
    traceback: str | Unset = UNSET
    last_run: datetime.datetime | Unset = UNSET
    created_on: datetime.datetime | Unset = UNSET
    last_modified: datetime.datetime | 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

        name = self.name

        requirements: list[str] | Unset = UNSET
        if not isinstance(self.requirements, Unset):
            requirements = self.requirements

        source = self.source

        language = self.language

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

        running = self.running

        failed = self.failed

        error = self.error

        traceback = self.traceback

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

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

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

        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 name is not UNSET:
            field_dict["name"] = name
        if requirements is not UNSET:
            field_dict["requirements"] = requirements
        if source is not UNSET:
            field_dict["source"] = source
        if language is not UNSET:
            field_dict["language"] = language
        if schedule is not UNSET:
            field_dict["schedule"] = schedule
        if running is not UNSET:
            field_dict["running"] = running
        if failed is not UNSET:
            field_dict["failed"] = failed
        if error is not UNSET:
            field_dict["error"] = error
        if traceback is not UNSET:
            field_dict["traceback"] = traceback
        if last_run is not UNSET:
            field_dict["lastRun"] = last_run
        if created_on is not UNSET:
            field_dict["createdOn"] = created_on
        if last_modified is not UNSET:
            field_dict["lastModified"] = last_modified

        return field_dict

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

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

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

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

        requirements = cast(list[str], d.pop("requirements", UNSET))

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

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

        _schedule = d.pop("schedule", UNSET)
        schedule: ScriptSchedule | Unset
        if isinstance(_schedule, Unset):
            schedule = UNSET
        else:
            schedule = ScriptSchedule.from_dict(_schedule)

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

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

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

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

        _last_run = d.pop("lastRun", UNSET)
        last_run: datetime.datetime | Unset
        if isinstance(_last_run, Unset):
            last_run = UNSET
        else:
            last_run = isoparse(_last_run)

        _created_on = d.pop("createdOn", UNSET)
        created_on: datetime.datetime | Unset
        if isinstance(_created_on, Unset):
            created_on = UNSET
        else:
            created_on = isoparse(_created_on)

        _last_modified = d.pop("lastModified", UNSET)
        last_modified: datetime.datetime | Unset
        if isinstance(_last_modified, Unset):
            last_modified = UNSET
        else:
            last_modified = isoparse(_last_modified)

        script = cls(
            field_id=field_id,
            team=team,
            name=name,
            requirements=requirements,
            source=source,
            language=language,
            schedule=schedule,
            running=running,
            failed=failed,
            error=error,
            traceback=traceback,
            last_run=last_run,
            created_on=created_on,
            last_modified=last_modified,
        )

        script.additional_properties = d
        return script

    @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
