from __future__ import annotations

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 ..types import UNSET, Unset

if TYPE_CHECKING:
    from ..models.config_item import ConfigItem
    from ..models.history_item import HistoryItem
    from ..models.module_trainer import ModuleTrainer


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


@_attrs_define
class PipelineModule:
    """
    Attributes:
        module (str | Unset): MongoDB ObjectId
        config (list[ConfigItem] | Unset):
        locked (bool | Unset):
        trainer (ModuleTrainer | Unset):
        usage (float | Unset):
        limit (float | Unset):
        history (list[HistoryItem] | Unset):
    """

    module: str | Unset = UNSET
    config: list[ConfigItem] | Unset = UNSET
    locked: bool | Unset = UNSET
    trainer: ModuleTrainer | Unset = UNSET
    usage: float | Unset = UNSET
    limit: float | Unset = UNSET
    history: list[HistoryItem] | Unset = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

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

        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)

        locked = self.locked

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

        usage = self.usage

        limit = self.limit

        history: list[dict[str, Any]] | Unset = UNSET
        if not isinstance(self.history, Unset):
            history = []
            for history_item_data in self.history:
                history_item = history_item_data.to_dict()
                history.append(history_item)

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update({})
        if module is not UNSET:
            field_dict["module"] = module
        if config is not UNSET:
            field_dict["config"] = config
        if locked is not UNSET:
            field_dict["locked"] = locked
        if trainer is not UNSET:
            field_dict["trainer"] = trainer
        if usage is not UNSET:
            field_dict["usage"] = usage
        if limit is not UNSET:
            field_dict["limit"] = limit
        if history is not UNSET:
            field_dict["history"] = history

        return field_dict

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

        d = dict(src_dict)
        module = d.pop("module", 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)

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

        _trainer = d.pop("trainer", UNSET)
        trainer: ModuleTrainer | Unset
        if isinstance(_trainer, Unset):
            trainer = UNSET
        else:
            trainer = ModuleTrainer.from_dict(_trainer)

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

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

        _history = d.pop("history", UNSET)
        history: list[HistoryItem] | Unset = UNSET
        if _history is not UNSET:
            history = []
            for history_item_data in _history:
                history_item = HistoryItem.from_dict(history_item_data)

                history.append(history_item)

        pipeline_module = cls(
            module=module,
            config=config,
            locked=locked,
            trainer=trainer,
            usage=usage,
            limit=limit,
            history=history,
        )

        pipeline_module.additional_properties = d
        return pipeline_module

    @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
