// 调试工具
export const debugAPI = {
  // 打印API请求详情
  logRequest: (url, method, data) => {
    console.log('=== API请求详情 ===');
    console.log('URL:', url);
    console.log('方法:', method);
    console.log('请求数据:', data);
    console.log('时间:', new Date().toLocaleString());
    console.log('==================');
  },

  // 打印API响应详情
  logResponse: (response) => {
    console.log('=== API响应详情 ===');
    console.log('完整响应:', response);
    console.log('响应类型:', typeof response);
    console.log('是否为对象:', typeof response === 'object');
    
    if (typeof response === 'object' && response !== null) {
      console.log('响应键:', Object.keys(response));
      console.log('响应值:', Object.values(response));
    }
    
    console.log('时间:', new Date().toLocaleString());
    console.log('==================');
  },

  // 分析响应数据结构
  analyzeResponse: (response) => {
    console.log('=== 响应数据分析 ===');
    
    if (typeof response === 'string') {
      console.log('响应是字符串:', response);
      return { type: 'string', value: response };
    }
    
    if (typeof response === 'object' && response !== null) {
      console.log('响应是对象');
      console.log('对象结构:', JSON.stringify(response, null, 2));
      
      // 查找可能的AI回复字段
      const possibleFields = ['response', 'message', 'content', 'reply', 'answer', 'text', 'data'];
      const foundFields = {};
      
      possibleFields.forEach(field => {
        if (response.hasOwnProperty(field)) {
          foundFields[field] = response[field];
          console.log(`找到字段 ${field}:`, response[field]);
        }
      });
      
      return { type: 'object', fields: foundFields, full: response };
    }
    
    console.log('未知响应类型:', typeof response);
    return { type: 'unknown', value: response };
  },

  // 测试API响应处理
  testResponseHandling: (response) => {
    console.log('=== 测试响应处理 ===');
    
    const analysis = debugAPI.analyzeResponse(response);
    
    // 尝试提取AI回复
    let aiResponse = null;
    
    if (analysis.type === 'string') {
      aiResponse = analysis.value;
      console.log('从字符串提取回复:', aiResponse);
    } else if (analysis.type === 'object') {
      // 尝试多种字段
      const fields = ['response', 'message', 'content', 'reply', 'answer', 'text'];
      
      for (const field of fields) {
        if (response[field] && typeof response[field] === 'string') {
          aiResponse = response[field];
          console.log(`从字段 ${field} 提取回复:`, aiResponse);
          break;
        }
      }
      
      // 如果没找到，尝试嵌套对象
      if (!aiResponse && response.data) {
        for (const field of fields) {
          if (response.data[field] && typeof response.data[field] === 'string') {
            aiResponse = response.data[field];
            console.log(`从 data.${field} 提取回复:`, aiResponse);
            break;
          }
        }
      }
    }
    
    console.log('最终提取的回复:', aiResponse);
    console.log('回复是否有效:', aiResponse && typeof aiResponse === 'string' && aiResponse.trim());
    
    // 检测是否为状态信息而非实际AI回复
    if (aiResponse && typeof aiResponse === 'string') {
      const statusKeywords = ['对话成功', 'success', '请求成功', '成功', 'ok', '完成'];
      const isStatusMessage = statusKeywords.some(keyword => 
        aiResponse.toLowerCase().includes(keyword.toLowerCase())
      );
      
      if (isStatusMessage) {
        console.warn('⚠️ 检测到状态信息而非AI回复:', aiResponse);
        console.log('建议使用本地模拟回复');
      }
    }
    
    console.log('==================');
    
    return aiResponse;
  }
};

// 导出默认调试工具
export default debugAPI;
