"""Google Search tool configuration."""

from __future__ import annotations

from typing import TYPE_CHECKING, Literal

from mindroom.tool_system.declarations import ConfigField, SetupType, ToolCategory, ToolStatus
from mindroom.tool_system.registration import register_tool_with_metadata

if TYPE_CHECKING:
    from agno.tools.websearch import WebSearchTools


@register_tool_with_metadata(
    name="googlesearch",
    display_name="Google Search",
    description="Search Google for web results using the WebSearch backend",
    category=ToolCategory.RESEARCH,
    status=ToolStatus.AVAILABLE,
    setup_type=SetupType.NONE,
    icon="FaGoogle",
    icon_color="text-blue-500",
    config_fields=[
        ConfigField(
            name="enable_search",
            label="Enable Search",
            type="boolean",
            required=False,
            default=True,
        ),
        ConfigField(
            name="enable_news",
            label="Enable News",
            type="boolean",
            required=False,
            default=True,
        ),
        ConfigField(
            name="modifier",
            label="Modifier",
            type="text",
            required=False,
            default=None,
        ),
        ConfigField(
            name="fixed_max_results",
            label="Fixed Max Results",
            type="number",
            required=False,
            default=None,
        ),
        ConfigField(
            name="proxy",
            label="Proxy",
            type="url",
            required=False,
            default=None,
        ),
        ConfigField(
            name="timeout",
            label="Timeout",
            type="number",
            required=False,
            default=10,
        ),
        ConfigField(
            name="verify_ssl",
            label="Verify Ssl",
            type="boolean",
            required=False,
            default=True,
        ),
        ConfigField(
            name="timelimit",
            label="Time Limit",
            type="text",
            required=False,
            default=None,
        ),
        ConfigField(
            name="region",
            label="Region",
            type="text",
            required=False,
            default=None,
        ),
    ],
    dependencies=["ddgs"],
    docs_url="https://docs.agno.com/tools/toolkits/search/websearch",
    function_names=("web_search", "search_news"),
)
def googlesearch_tools() -> type[WebSearchTools]:
    """Return Google Search tools for web search."""
    from agno.tools.websearch import WebSearchTools

    class GoogleSearchTools(WebSearchTools):
        """Convenience wrapper for WebSearchTools with Google as the backend."""

        def __init__(
            self,
            enable_search: bool = True,
            enable_news: bool = True,
            modifier: str | None = None,
            fixed_max_results: int | None = None,
            proxy: str | None = None,
            timeout: int | None = 10,
            verify_ssl: bool = True,
            timelimit: Literal["d", "w", "m", "y"] | None = None,
            region: str | None = None,
            **kwargs: object,
        ) -> None:
            super().__init__(
                enable_search=enable_search,
                enable_news=enable_news,
                backend="google",
                modifier=modifier,
                fixed_max_results=fixed_max_results,
                proxy=proxy,
                timeout=timeout,
                verify_ssl=verify_ssl,
                timelimit=timelimit,
                region=region,
                **kwargs,
            )

    return GoogleSearchTools
