"""
内置工具列表
系统启动时会自动加载这些工具
"""

WEATHER_API_KEY = "2d5c904b712686d2"

BUILTIN_TOOLS = [
    {
        "name": "天气查询",
        "description": "查询全国各城市的实时天气信息，包括温度、天气状况、湿度、风力、气压、降水量等详细数据。支持全国所有地级市和县级市。",
        "category": "信息查询",
        "is_code_tool": True,
        "is_builtin": True,  # 标记为内置工具
        "function_code": f'''def get_weather(city):
    """
    查询指定城市的天气信息
    
    参数:
        city: 城市名称，例如：北京、上海、广州、深圳、杭州等
    
    返回:
        包含天气详细信息的字典
    """
    import json
    
    # 由于沙箱限制，这里返回提示信息
    # 实际调用通过后端 API: GET /api/weather?city={{city}}
    
    return {{
        "提示": "请使用后端 API 查询天气",
        "API": f"GET /api/weather?city={{city}}",
        "城市": city,
        "说明": "天气数据通过后端 API 实时获取，包含温度、湿度、风力等详细信息"
    }}
''',
        "parameters_schema": {
            "type": "object",
            "properties": {
                "city": {
                    "type": "string",
                    "description": "要查询天气的城市名称，支持全国所有城市",
                    "example": "北京",
                    "examples": ["北京", "上海", "广州", "深圳", "杭州", "成都", "武汉", "西安"]
                }
            },
            "required": ["city"]
        },
        "usage_example": "查询北京的天气",
        "tags": ["天气", "查询", "信息"]
    },
    {
        "name": "计算器",
        "description": "执行基本的数学运算，支持加法、减法、乘法、除法四则运算",
        "category": "工具",
        "is_code_tool": True,
        "is_builtin": True,
        "function_code": '''def calculate(operation, a, b):
    """
    执行数学运算
    
    参数:
        operation: 运算类型 (add/subtract/multiply/divide)
        a: 第一个数
        b: 第二个数
    
    返回:
        运算结果
    """
    if operation == "add":
        return a + b
    elif operation == "subtract":
        return a - b
    elif operation == "multiply":
        return a * b
    elif operation == "divide":
        if b == 0:
            return "错误：除数不能为0"
        return a / b
    else:
        return "不支持的运算类型"
''',
        "parameters_schema": {
            "type": "object",
            "properties": {
                "operation": {
                    "type": "string",
                    "description": "运算类型",
                    "enum": ["add", "subtract", "multiply", "divide"],
                    "example": "add"
                },
                "a": {
                    "type": "number",
                    "description": "第一个数",
                    "example": 10
                },
                "b": {
                    "type": "number",
                    "description": "第二个数",
                    "example": 5
                }
            },
            "required": ["operation", "a", "b"]
        },
        "usage_example": "计算 10 + 5",
        "tags": ["计算", "数学", "工具"]
    },
    {
        "name": "日期格式化",
        "description": "格式化日期时间，支持多种格式输出",
        "category": "工具",
        "is_code_tool": True,
        "is_builtin": True,
        "function_code": '''import datetime

def format_date(date_string, format_type):
    """
    格式化日期
    
    参数:
        date_string: 日期字符串 (格式: YYYY-MM-DD)
        format_type: 格式类型 (chinese/full/short/default)
    
    返回:
        格式化后的日期字符串
    """
    try:
        date_obj = datetime.datetime.strptime(date_string, "%Y-%m-%d")
        
        if format_type == "chinese":
            return date_obj.strftime("%Y年%m月%d日")
        elif format_type == "full":
            return date_obj.strftime("%Y-%m-%d %H:%M:%S")
        elif format_type == "short":
            return date_obj.strftime("%m/%d")
        else:
            return date_obj.strftime("%Y-%m-%d")
    except Exception as e:
        return f"日期解析错误: {{str(e)}}"
''',
        "parameters_schema": {
            "type": "object",
            "properties": {
                "date_string": {
                    "type": "string",
                    "description": "日期字符串 (格式: YYYY-MM-DD)",
                    "example": "2024-01-15"
                },
                "format_type": {
                    "type": "string",
                    "description": "格式类型",
                    "enum": ["chinese", "full", "short", "default"],
                    "example": "chinese"
                }
            },
            "required": ["date_string", "format_type"]
        },
        "usage_example": "将 2024-01-15 格式化为中文",
        "tags": ["日期", "格式化", "工具"]
    },
    {
        "name": "文本处理",
        "description": "对文本进行各种处理，包括大小写转换、反转、统计等",
        "category": "工具",
        "is_code_tool": True,
        "is_builtin": True,
        "function_code": '''def process_text(text, operation):
    """
    文本处理工具
    
    参数:
        text: 要处理的文本
        operation: 操作类型 (uppercase/lowercase/reverse/word_count/char_count)
    
    返回:
        处理后的结果
    """
    if operation == "uppercase":
        return text.upper()
    elif operation == "lowercase":
        return text.lower()
    elif operation == "reverse":
        return text[::-1]
    elif operation == "word_count":
        return len(text.split())
    elif operation == "char_count":
        return len(text)
    else:
        return text
''',
        "parameters_schema": {
            "type": "object",
            "properties": {
                "text": {
                    "type": "string",
                    "description": "要处理的文本",
                    "example": "Hello World"
                },
                "operation": {
                    "type": "string",
                    "description": "操作类型",
                    "enum": ["uppercase", "lowercase", "reverse", "word_count", "char_count"],
                    "example": "uppercase"
                }
            },
            "required": ["text", "operation"]
        },
        "usage_example": "将文本转换为大写",
        "tags": ["文本", "处理", "工具"]
    },
    {
        "name": "随机数生成",
        "description": "生成指定范围内的随机数或随机字符串",
        "category": "工具",
        "is_code_tool": True,
        "is_builtin": True,
        "function_code": '''import random

def generate_random(type, count, min_val=1, max_val=100):
    """
    生成随机数据
    
    参数:
        type: 类型 (number/letter/password)
        count: 数量
        min_val: 最小值 (仅用于 number 类型)
        max_val: 最大值 (仅用于 number 类型)
    
    返回:
        随机生成的数据
    """
    if type == "number":
        return [random.randint(min_val, max_val) for _ in range(count)]
    elif type == "letter":
        return [random.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZ') for _ in range(count)]
    elif type == "password":
        chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*'
        return ''.join(random.choice(chars) for _ in range(count))
    else:
        return []
''',
        "parameters_schema": {
            "type": "object",
            "properties": {
                "type": {
                    "type": "string",
                    "description": "生成类型",
                    "enum": ["number", "letter", "password"],
                    "example": "number"
                },
                "count": {
                    "type": "integer",
                    "description": "生成数量",
                    "example": 5
                },
                "min_val": {
                    "type": "integer",
                    "description": "最小值 (仅用于 number 类型)",
                    "example": 1
                },
                "max_val": {
                    "type": "integer",
                    "description": "最大值 (仅用于 number 类型)",
                    "example": 100
                }
            },
            "required": ["type", "count"]
        },
        "usage_example": "生成5个1-100之间的随机数",
        "tags": ["随机", "生成", "工具"]
    }
]

def get_builtin_tools():
    """获取所有内置工具"""
    return BUILTIN_TOOLS

def get_tool_by_name(name):
    """根据名称获取内置工具"""
    for tool in BUILTIN_TOOLS:
        if tool["name"] == name:
            return tool
    return None

def get_tools_by_category(category):
    """根据分类获取内置工具"""
    return [tool for tool in BUILTIN_TOOLS if tool.get("category") == category]
