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

if TYPE_CHECKING:
    from ..models.compaction_tool import CompactionTool
    from ..models.model_citation_tool import ModelCitationTool
    from ..models.retrieval_chunk_tool import RetrievalChunkTool
    from ..models.retrieval_full_context_tool import RetrievalFullContextTool
    from ..models.retrieval_toc_tool import RetrievalTOCTool
    from ..models.trace_tool import TraceTool


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


@_attrs_define
class MessageInputTools:
    """ """

    additional_properties: dict[
        str,
        CompactionTool
        | ModelCitationTool
        | RetrievalChunkTool
        | RetrievalFullContextTool
        | RetrievalTOCTool
        | TraceTool,
    ] = _attrs_field(init=False, factory=dict)

    def to_dict(self) -> dict[str, Any]:
        from ..models.model_citation_tool import ModelCitationTool
        from ..models.retrieval_chunk_tool import RetrievalChunkTool
        from ..models.retrieval_full_context_tool import RetrievalFullContextTool
        from ..models.retrieval_toc_tool import RetrievalTOCTool
        from ..models.trace_tool import TraceTool

        field_dict: dict[str, Any] = {}
        for prop_name, prop in self.additional_properties.items():
            if isinstance(prop, ModelCitationTool):
                field_dict[prop_name] = prop.to_dict()
            elif isinstance(prop, RetrievalChunkTool):
                field_dict[prop_name] = prop.to_dict()
            elif isinstance(prop, RetrievalFullContextTool):
                field_dict[prop_name] = prop.to_dict()
            elif isinstance(prop, RetrievalTOCTool):
                field_dict[prop_name] = prop.to_dict()
            elif isinstance(prop, TraceTool):
                field_dict[prop_name] = prop.to_dict()
            else:
                field_dict[prop_name] = prop.to_dict()

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        from ..models.compaction_tool import CompactionTool
        from ..models.model_citation_tool import ModelCitationTool
        from ..models.retrieval_chunk_tool import RetrievalChunkTool
        from ..models.retrieval_full_context_tool import RetrievalFullContextTool
        from ..models.retrieval_toc_tool import RetrievalTOCTool
        from ..models.trace_tool import TraceTool

        d = dict(src_dict)
        message_input_tools = cls()

        additional_properties = {}
        for prop_name, prop_dict in d.items():

            def _parse_additional_property(
                data: object,
            ) -> (
                CompactionTool
                | ModelCitationTool
                | RetrievalChunkTool
                | RetrievalFullContextTool
                | RetrievalTOCTool
                | TraceTool
            ):
                try:
                    if not isinstance(data, dict):
                        raise TypeError()
                    additional_property_type_0 = ModelCitationTool.from_dict(data)

                    return additional_property_type_0
                except (TypeError, ValueError, AttributeError, KeyError):
                    pass
                try:
                    if not isinstance(data, dict):
                        raise TypeError()
                    additional_property_type_1 = RetrievalChunkTool.from_dict(data)

                    return additional_property_type_1
                except (TypeError, ValueError, AttributeError, KeyError):
                    pass
                try:
                    if not isinstance(data, dict):
                        raise TypeError()
                    additional_property_type_2 = RetrievalFullContextTool.from_dict(data)

                    return additional_property_type_2
                except (TypeError, ValueError, AttributeError, KeyError):
                    pass
                try:
                    if not isinstance(data, dict):
                        raise TypeError()
                    additional_property_type_3 = RetrievalTOCTool.from_dict(data)

                    return additional_property_type_3
                except (TypeError, ValueError, AttributeError, KeyError):
                    pass
                try:
                    if not isinstance(data, dict):
                        raise TypeError()
                    additional_property_type_4 = TraceTool.from_dict(data)

                    return additional_property_type_4
                except (TypeError, ValueError, AttributeError, KeyError):
                    pass
                if not isinstance(data, dict):
                    raise TypeError()
                additional_property_type_5 = CompactionTool.from_dict(data)

                return additional_property_type_5

            additional_property = _parse_additional_property(prop_dict)

            additional_properties[prop_name] = additional_property

        message_input_tools.additional_properties = additional_properties
        return message_input_tools

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

    def __getitem__(
        self, key: str
    ) -> (
        CompactionTool
        | ModelCitationTool
        | RetrievalChunkTool
        | RetrievalFullContextTool
        | RetrievalTOCTool
        | TraceTool
    ):
        return self.additional_properties[key]

    def __setitem__(
        self,
        key: str,
        value: CompactionTool
        | ModelCitationTool
        | RetrievalChunkTool
        | RetrievalFullContextTool
        | RetrievalTOCTool
        | TraceTool,
    ) -> 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
