"""Web search tool using DuckDuckGo (no API key required)."""

import json

from ddgs import DDGS

from .base import Tool, ToolResult


class WebSearchTool(Tool):
    name = "web_search"
    description = "Search the web for current information. Returns a list of results with title, URL, and snippet."
    parameters = {
        "type": "object",
        "properties": {
            "query": {"type": "string", "description": "Search query"},
            "max_results": {
                "type": "integer",
                "description": "Number of results to return (default 5)",
                "default": 5,
            },
        },
        "required": ["query"],
    }

    async def execute(self, params: dict) -> ToolResult:
        query = params["query"]
        max_results = int(params.get("max_results", 5))
        try:
            results = DDGS().text(query, max_results=max_results)
            if not results:
                return ToolResult(success=True, output="No results found.")

            formatted = [
                f"[{i+1}] {r['title']}\n    URL: {r['href']}\n    {r['body']}"
                for i, r in enumerate(results)
            ]
            output = "\n\n".join(formatted)
            return ToolResult(success=True, output=output, metadata={"results": results})
        except Exception as e:
            return ToolResult(success=False, output=f"Search failed: {e}", error=str(e))
