import { tavily } from '@tavily/core';
import type { ToolResult } from '../../types/index.js';
import type { ToolWithMetadata } from '../types.js';
import { loadDescription } from '../load_description.js';
import { getPermissionManager } from '../../permission/index.js';

export const webSearchTool: ToolWithMetadata = {
  name: 'web_search',
  description: loadDescription('web_search'),
  metadata: {
    name: 'web_search',
    category: 'web',
    description: '搜索网络获取最新信息',
    keywords: ['search', 'web', 'internet', 'google', 'query', '搜索', '网络', '查询', '互联网'],
    deferLoading: false, // 核心工具，始终可用
  },
  parameters: {
    query: {
      type: 'string',
      description: '搜索查询关键词',
      required: true,
    },
    max_results: {
      type: 'number',
      description: '返回结果数量（默认 5，最大 20）',
      required: false,
    },
    search_depth: {
      type: 'string',
      description: '搜索深度: "basic" 快速搜索，"advanced" 深度搜索（默认 basic）',
      required: false,
    },
    topic: {
      type: 'string',
      description: '搜索主题: "general" 通用，"news" 新闻，"finance" 财经（默认 general）',
      required: false,
    },
    include_answer: {
      type: 'boolean',
      description: '是否包含 AI 生成的摘要答案（默认 true）',
      required: false,
    },
  },
  execute: async (params: Record<string, unknown>): Promise<ToolResult> => {
    const query = params.query as string;
    const maxResults = Math.min((params.max_results as number) || 5, 20);
    const searchDepth = (params.search_depth as 'basic' | 'advanced') || 'basic';
    const topic = (params.topic as 'general' | 'news' | 'finance') || 'general';
    const includeAnswer = params.include_answer !== false;

    // 权限检查
    const permissionManager = getPermissionManager();
    const permResult = await permissionManager.checkWebPermission({
      query,
      searchDepth,
      topic,
      maxResults,
    });

    if (!permResult.allowed) {
      // 如果需要用户确认但没有设置回调，返回等待确认的状态
      if (permResult.needsConfirmation) {
        return {
          success: false,
          output: '',
          error: `需要用户确认网络搜索: "${query}"\n原因: ${permResult.reason || '需要权限确认'}`,
        };
      }

      return {
        success: false,
        output: '',
        error: `网络搜索权限被拒绝: ${permResult.reason || '搜索不被允许'}`,
      };
    }

    // 获取 Tavily API Key（从环境变量）
    const apiKey = process.env.TAVILY_API_KEY;

    if (!apiKey) {
      return {
        success: false,
        output: '',
        error: '未配置 Tavily API Key。请设置环境变量 TAVILY_API_KEY。',
      };
    }

    try {
      // 使用 Tavily SDK
      const client = tavily({ apiKey });
      const response = await client.search(query, {
        searchDepth,
        topic,
        maxResults,
        includeAnswer,
      });

      // 格式化输出
      let output = `## 搜索结果: "${query}"\n\n`;

      // 如果有 AI 摘要答案
      if (response.answer) {
        output += `### 摘要\n${response.answer}\n\n`;
      }

      // 搜索结果列表
      if (response.results && response.results.length > 0) {
        output += `### 相关链接 (${response.results.length} 条)\n\n`;

        for (let i = 0; i < response.results.length; i++) {
          const result = response.results[i];
          output += `**${i + 1}. ${result.title}**\n`;
          output += `链接: ${result.url}\n`;
          // 截断过长的内容
          const content = result.content.length > 300
            ? result.content.substring(0, 300) + '...'
            : result.content;
          output += `${content}\n\n`;
        }
      } else {
        output += '未找到相关结果。\n';
      }

      return {
        success: true,
        output,
      };
    } catch (error) {
      return {
        success: false,
        output: '',
        error: `搜索失败: ${error instanceof Error ? error.message : String(error)}`,
      };
    }
  },
};
