# ANVIL-MANAGED: true
# version: 1.0
# hash: 8eb65325
# ---------------------------------------------------------
# AUTO-GENERATED BY ANVIL.
# To eject and take manual control, change 'ANVIL-MANAGED' to 'false'.
# ---------------------------------------------------------

import requests
from typing import Dict, Any, Optional

def run(**kwargs) -> Dict[str, Any]:
    """
    Get current weather information for a specified city using wttr.in API.
    
    Args:
        city (str): The name of the city to get weather for
        
    Returns:
        Dict[str, Any]: Weather information including temperature, condition, and description
    """
    city = kwargs.get('city')
    
    if not city:
        return {"error": "City parameter is required"}
    
    try:
        # Use wttr.in API which provides free weather data
        url = f"https://wttr.in/{city}?format=j1"
        
        response = requests.get(url, timeout=10)
        response.raise_for_status()
        
        data = response.json()
        
        # Extract current weather information
        current = data.get('current_condition', [{}])[0]
        weather_desc = current.get('weatherDesc', [{}])[0].get('value', 'Unknown')
        temp_c = current.get('temp_C', 'Unknown')
        temp_f = current.get('temp_F', 'Unknown')
        humidity = current.get('humidity', 'Unknown')
        wind_speed = current.get('windspeedKmph', 'Unknown')
        wind_dir = current.get('winddir16Point', 'Unknown')
        
        return {
            "city": city,
            "temperature_celsius": temp_c,
            "temperature_fahrenheit": temp_f,
            "condition": weather_desc,
            "humidity": f"{humidity}%",
            "wind_speed": f"{wind_speed} km/h",
            "wind_direction": wind_dir,
            "status": "success"
        }
        
    except requests.exceptions.RequestException as e:
        return {"error": f"Failed to fetch weather data: {str(e)}"}
    except KeyError as e:
        return {"error": f"Unexpected response format: missing key {str(e)}"}
    except Exception as e:
        return {"error": f"An unexpected error occurred: {str(e)}"}