"""
Custom example tools for the OpenRouter Agent.
"""

import json
from typing import Dict

from core.models import Tool


def get_weather(city: str, country: str = "US") -> str:
    """Simulated weather function - in production, connect to real API"""
    weather_data = {
        "New York": {"temp": 72, "condition": "Sunny", "humidity": 65},
        "London": {"temp": 60, "condition": "Cloudy", "humidity": 80},
        "Tokyo": {"temp": 75, "condition": "Rainy", "humidity": 90},
        "Sydney": {"temp": 85, "condition": "Clear", "humidity": 70},
    }
    
    if city in weather_data:
        data = weather_data[city]
        return f"Weather in {city}, {country}: {data['temp']}°F, {data['condition']}, Humidity: {data['humidity']}%"
    else:
        return f"Weather data not available for {city}. Try: New York, London, Tokyo, or Sydney."


def search_web(query: str, num_results: int = 3) -> str:
    """Simulated web search - in production, connect to search API"""
    results = [
        f"1. {query} - Recent news article",
        f"2. {query} - Wikipedia entry",
        f"3. {query} - Research paper",
        f"4. {query} - Blog post",
        f"5. {query} - Official documentation"
    ]
    
    return f"Search results for '{query}':\n" + "\n".join(results[:num_results])


def format_json(data: str) -> str:
    """Format data as JSON string"""
    try:
        parsed = json.loads(data)
        return json.dumps(parsed, indent=2)
    except:
        return data


CUSTOM_TOOLS: Dict[str, Tool] = {
    "get_weather": Tool(
        name="get_weather",
        description="Get weather information for a city",
        parameters={
            "type": "object",
            "properties": {
                "city": {
                    "type": "string",
                    "description": "The city name"
                },
                "country": {
                    "type": "string",
                    "description": "The country code (optional)",
                    "default": "US"
                }
            },
            "required": ["city"]
        },
        function=get_weather
    ),
    "search_web": Tool(
        name="search_web",
        description="Search the web for information",
        parameters={
            "type": "object",
            "properties": {
                "query": {
                    "type": "string",
                    "description": "The search query"
                },
                "num_results": {
                    "type": "integer",
                    "description": "Number of results to return",
                    "default": 3
                }
            },
            "required": ["query"]
        },
        function=search_web
    ),
    "format_json": Tool(
        name="format_json",
        description="Format data as JSON string",
        parameters={
            "type": "object",
            "properties": {
                "data": {
                    "type": "string",
                    "description": "Data to format as JSON"
                }
            },
            "required": ["data"]
        },
        function=format_json
    ),
}


def get_custom_tools() -> Dict[str, Tool]:
    """Return a copy of the custom tools dictionary"""
    return dict(CUSTOM_TOOLS)