from __future__ import annotations

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

if TYPE_CHECKING:
    from ..models.tool_resources_code_interpreter import ToolResourcesCodeInterpreter
    from ..models.tool_resources_file_search import ToolResourcesFileSearch


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


@_attrs_define
class ToolResources:
    """A set of resources that are used by the assistant's tools.

    The resources are specific to the type of tool. For example, the `code_interpreter` tool requires a list of file
    IDs, while the `file_search` tool requires a list of vector store IDs.

        Attributes:
            code_interpreter (None | ToolResourcesCodeInterpreter | Unset):
            file_search (None | ToolResourcesFileSearch | Unset):
    """

    code_interpreter: None | ToolResourcesCodeInterpreter | Unset = UNSET
    file_search: None | ToolResourcesFileSearch | Unset = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

    def to_dict(self) -> dict[str, Any]:
        from ..models.tool_resources_code_interpreter import ToolResourcesCodeInterpreter
        from ..models.tool_resources_file_search import ToolResourcesFileSearch

        code_interpreter: dict[str, Any] | None | Unset
        if isinstance(self.code_interpreter, Unset):
            code_interpreter = UNSET
        elif isinstance(self.code_interpreter, ToolResourcesCodeInterpreter):
            code_interpreter = self.code_interpreter.to_dict()
        else:
            code_interpreter = self.code_interpreter

        file_search: dict[str, Any] | None | Unset
        if isinstance(self.file_search, Unset):
            file_search = UNSET
        elif isinstance(self.file_search, ToolResourcesFileSearch):
            file_search = self.file_search.to_dict()
        else:
            file_search = self.file_search

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update({})
        if code_interpreter is not UNSET:
            field_dict["code_interpreter"] = code_interpreter
        if file_search is not UNSET:
            field_dict["file_search"] = file_search

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        from ..models.tool_resources_code_interpreter import ToolResourcesCodeInterpreter
        from ..models.tool_resources_file_search import ToolResourcesFileSearch

        d = dict(src_dict)

        def _parse_code_interpreter(data: object) -> None | ToolResourcesCodeInterpreter | Unset:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            try:
                if not isinstance(data, dict):
                    raise TypeError()
                code_interpreter_type_0 = ToolResourcesCodeInterpreter.from_dict(data)

                return code_interpreter_type_0
            except (TypeError, ValueError, AttributeError, KeyError):
                pass
            return cast(None | ToolResourcesCodeInterpreter | Unset, data)

        code_interpreter = _parse_code_interpreter(d.pop("code_interpreter", UNSET))

        def _parse_file_search(data: object) -> None | ToolResourcesFileSearch | Unset:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            try:
                if not isinstance(data, dict):
                    raise TypeError()
                file_search_type_0 = ToolResourcesFileSearch.from_dict(data)

                return file_search_type_0
            except (TypeError, ValueError, AttributeError, KeyError):
                pass
            return cast(None | ToolResourcesFileSearch | Unset, data)

        file_search = _parse_file_search(d.pop("file_search", UNSET))

        tool_resources = cls(
            code_interpreter=code_interpreter,
            file_search=file_search,
        )

        tool_resources.additional_properties = d
        return tool_resources

    @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
