const { request } = require('../utils/request.js')

async function getAgentCapabilities() {
  const res = await request('/ai/capabilities', {
    method: 'GET',
    skillName: 'activity'
  })

  if (res.error) {
    return {
      isError: true,
      content: [{ type: 'text', text: res.safeMessage || 'Agent 能力目录加载失败，请稍后重试。' }]
    }
  }

  const data = res.data || {}
  const tools = Array.isArray(data.tools) ? data.tools : []
  const availableTools = tools.filter((item) => item && item.available !== false)

  return {
    isError: false,
    content: [{
      type: 'text',
      text: `已加载 ${availableTools.length} 个可用 Agent 工具。需要用户补充信息时，请使用 1/2/3/4 选项，其中 4 固定为自由输入。`
    }],
    structuredContent: {
      apiVersion: data.apiVersion,
      groups: data.wechatGroups || {},
      tools: availableTools,
      clarification: data.clarification,
      cardProtocol: data.cardProtocol
    },
    _meta: {
      totalTools: tools.length,
      availableTools: availableTools.length,
      relatedPage: '/pages/live/index'
    }
  }
}

module.exports = getAgentCapabilities
