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 ..models.web_search_options_search_context_size import WebSearchOptionsSearchContextSize
from ..types import UNSET, Unset

if TYPE_CHECKING:
    from ..models.web_search_options_user_location import WebSearchOptionsUserLocation


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


@_attrs_define
class WebSearchOptions:
    """This tool searches the web for relevant results to use in a response.
    Learn more about the [web search tool](https://platform.openai.com/docs/guides/tools-web-search?api-mode=chat).

        Attributes:
            search_context_size (WebSearchOptionsSearchContextSize | Unset):
            user_location (None | Unset | WebSearchOptionsUserLocation):
    """

    search_context_size: WebSearchOptionsSearchContextSize | Unset = UNSET
    user_location: None | Unset | WebSearchOptionsUserLocation = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

    def to_dict(self) -> dict[str, Any]:
        from ..models.web_search_options_user_location import WebSearchOptionsUserLocation

        search_context_size: str | Unset = UNSET
        if not isinstance(self.search_context_size, Unset):
            search_context_size = self.search_context_size.value

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

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

        return field_dict

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

        d = dict(src_dict)
        _search_context_size = d.pop("search_context_size", UNSET)
        search_context_size: WebSearchOptionsSearchContextSize | Unset
        if isinstance(_search_context_size, Unset):
            search_context_size = UNSET
        else:
            search_context_size = WebSearchOptionsSearchContextSize(_search_context_size)

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

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

        user_location = _parse_user_location(d.pop("user_location", UNSET))

        web_search_options = cls(
            search_context_size=search_context_size,
            user_location=user_location,
        )

        web_search_options.additional_properties = d
        return web_search_options

    @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
