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.config_item import ConfigItem
    from ..models.module_trainer_data import ModuleTrainerData


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


@_attrs_define
class ModuleTrainer:
    """
    Attributes:
        usage (float | Unset):
        last_trained (datetime.datetime | Unset):
        training (bool | Unset):
        config (list[ConfigItem] | Unset):
        data (ModuleTrainerData | Unset):
    """

    usage: float | Unset = UNSET
    last_trained: datetime.datetime | Unset = UNSET
    training: bool | Unset = UNSET
    config: list[ConfigItem] | Unset = UNSET
    data: ModuleTrainerData | Unset = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

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

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

        training = self.training

        config: list[dict[str, Any]] | Unset = UNSET
        if not isinstance(self.config, Unset):
            config = []
            for config_item_data in self.config:
                config_item = config_item_data.to_dict()
                config.append(config_item)

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

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update({})
        if usage is not UNSET:
            field_dict["usage"] = usage
        if last_trained is not UNSET:
            field_dict["lastTrained"] = last_trained
        if training is not UNSET:
            field_dict["training"] = training
        if config is not UNSET:
            field_dict["config"] = config
        if data is not UNSET:
            field_dict["data"] = data

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        from ..models.config_item import ConfigItem
        from ..models.module_trainer_data import ModuleTrainerData

        d = dict(src_dict)
        usage = d.pop("usage", UNSET)

        _last_trained = d.pop("lastTrained", UNSET)
        last_trained: datetime.datetime | Unset
        if isinstance(_last_trained, Unset):
            last_trained = UNSET
        else:
            last_trained = isoparse(_last_trained)

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

        _config = d.pop("config", UNSET)
        config: list[ConfigItem] | Unset = UNSET
        if _config is not UNSET:
            config = []
            for config_item_data in _config:
                config_item = ConfigItem.from_dict(config_item_data)

                config.append(config_item)

        _data = d.pop("data", UNSET)
        data: ModuleTrainerData | Unset
        if isinstance(_data, Unset):
            data = UNSET
        else:
            data = ModuleTrainerData.from_dict(_data)

        module_trainer = cls(
            usage=usage,
            last_trained=last_trained,
            training=training,
            config=config,
            data=data,
        )

        module_trainer.additional_properties = d
        return module_trainer

    @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
