const fs = require('fs');
const https = require('https');
const { parseIntent, checkPipelineStatus, checkDevStatus, formatTeamOverview, unknownIntentHelp } = require('./intent-router');

// === 知识库匹配阈值：问题词中至少40%出现在Issue文本中才算命中 ===
const FAQ_MATCH_THRESHOLD = 0.4;
const MAX_CONTEXT_COMMENTS = 10;
const MAX_COMMENT_LENGTH = 500;

// === 冰朔（仓库创建者）的 GitHub 用户名 ===
const BINGSHUO_USERNAME = 'qinfendebingshuo';

// === 读取铸渊大脑 ===
let devStatus, knowledgeBase, collaborators;
try {
  devStatus = JSON.parse(fs.readFileSync('.github/persona-brain/dev-status.json', 'utf8'));
  knowledgeBase = JSON.parse(fs.readFileSync('.github/persona-brain/knowledge-base.json', 'utf8'));
} catch (err) {
  console.error('❌ 铸渊大脑文件读取失败：', err.message);
  process.exit(1);
}

try {
  collaborators = JSON.parse(fs.readFileSync('.github/brain/collaborators.json', 'utf8'));
} catch (err) {
  console.log('⚠️ collaborators.json 读取失败，使用空列表：', err.message);
  collaborators = [];
}

// === 数据规范化：兼容 Notion 同步的字段名差异 ===
if (devStatus.team && !devStatus.team_status) {
  devStatus.team_status = devStatus.team;
}
if (devStatus.last_sync && !devStatus.last_synced) {
  devStatus.last_synced = devStatus.last_sync;
}
if (!devStatus.tech_routing) {
  devStatus.tech_routing = { level_2_peer_help: {} };
}
if (!devStatus.server_info) {
  devStatus.server_info = {};
}
if (Array.isArray(devStatus.team_status)) {
  devStatus.team_status.forEach(dev => {
    if (dev.module !== undefined && dev.modules === undefined) {
      dev.modules = Array.isArray(dev.module) ? dev.module : [dev.module];
    }
    if (dev.current !== undefined && dev.next_step === undefined) {
      dev.next_step = dev.current;
    }
    if (dev.waiting !== undefined && dev.waiting_for === undefined) {
      dev.waiting_for = dev.waiting;
    }
    if (!dev.os) {
      dev.os = '未记录';
    }
  });
}

// 兼容 collaborators.json 的嵌套结构
if (!Array.isArray(collaborators) && collaborators && collaborators.collaborators) {
  collaborators = collaborators.collaborators;
}

const issueNumber = process.env.ISSUE_NUMBER;
const issueTitle = process.env.ISSUE_TITLE || '';
const issueBody = process.env.ISSUE_BODY || '';
const issueLabels = process.env.ISSUE_LABELS || '';
const commentBody = process.env.COMMENT_BODY || '';
const commentAuthor = process.env.COMMENT_AUTHOR || '';
const commentId = process.env.COMMENT_ID || '';
const eventName = process.env.EVENT_NAME || 'issues';
const issueAuthor = process.env.ISSUE_AUTHOR || '';

// === 判断事件类型 ===
const isCommentEvent = eventName === 'issue_comment';
const isIssueEvent = eventName === 'issues';

// === 权限检查：识别评论者身份 ===
function identifyUser(username) {
  if (username === BINGSHUO_USERNAME) {
    return { role: 'founder', name: '冰朔', devId: null, isBingshuo: true };
  }
  // 在合作者名册中查找
  if (Array.isArray(collaborators)) {
    const collab = collaborators.find(c => c.github_username === username);
    if (collab) {
      return { role: 'collaborator', name: collab.name, devId: collab.dev_id, isBingshuo: false };
    }
  }
  // 在团队状态中通过 dev_id 查找（备用）
  return { role: 'unknown', name: username, devId: null, isBingshuo: false };
}

// === 安全检查：拒绝系统级指令 ===
function containsSystemCommand(text) {
  const dangerousPatterns = [
    /删除.*仓库/i, /delete.*repo/i,
    /修改.*权限/i, /change.*permission/i,
    /添加.*管理员/i, /add.*admin/i,
    /修改.*workflow/i, /修改.*工作流/i,
    /修改.*secret/i, /更改.*密钥/i,
    /执行.*命令/i, /run.*command/i,
    /修改.*他人/i, /更改.*别人/i,
  ];
  return dangerousPatterns.some(p => p.test(text));
}

// === 判断Issue类型 ===
const isSyslog = issueLabels.includes('syslog') || /SYSLOG|系统日志/.test(issueTitle) || issueBody.includes('### 广播编号');
const isProgressQuery = issueLabels.includes('progress-query');
const isDevQuestion = issueLabels.includes('dev-question');

// === 提取开发者编号 ===
const textToSearch = isCommentEvent ? commentBody : issueBody;
const devIdMatch = textToSearch.match(/DEV-\d{3}/i);
const devId = devIdMatch ? devIdMatch[0].toUpperCase() : null;
const devInfo = devId ? devStatus.team_status.find(d => d.dev_id === devId) : null;

// === 获取Issue评论历史（用于对话上下文）===
async function fetchIssueComments() {
  const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/');
  const response = await githubRequest('GET', `/repos/${owner}/${repo}/issues/${issueNumber}/comments?per_page=50`);
  try {
    const comments = JSON.parse(response);
    return Array.isArray(comments) ? comments : [];
  } catch {
    return [];
  }
}

// === 构建对话上下文 ===
function buildConversationContext(comments) {
  let context = '';
  if (issueBody) {
    context += `[Issue原文·${issueAuthor || '提问者'}] ${issueTitle}\n${issueBody}\n\n`;
  }
  if (!comments || comments.length === 0) return context;
  const recent = comments.slice(-MAX_CONTEXT_COMMENTS);
  recent.forEach(c => {
    const author = c.user ? c.user.login : '未知';
    const isBot = author === 'github-actions[bot]';
    const role = isBot ? '铸渊' : author;
    const body = c.body && c.body.length > MAX_COMMENT_LENGTH ? c.body.substring(0, MAX_COMMENT_LENGTH) + '...' : (c.body || '');
    context += `[${role}] ${body}\n\n`;
  });
  return context;
}

// === 主处理流程 ===
async function generateReply() {
  // ── 评论区触发：@铸渊 处理 ──
  if (isCommentEvent) {
    return handleCommentTrigger();
  }

  // ── Issue 新建触发：原有逻辑 ──
  return handleIssueTrigger();
}

// === 评论区 @铸渊 处理 ===
async function handleCommentTrigger() {
  // 跳过机器人自身评论，防止循环
  if (commentAuthor === 'github-actions[bot]' || commentAuthor === 'zhuyuan-bot') {
    console.log('🤖 跳过机器人自身评论');
    return;
  }

  const user = identifyUser(commentAuthor);
  console.log(`🤖 铸渊收到评论区唤醒 · 来自 ${user.name}（${user.role}）`);

  // 1. 安全检查 —— 拒绝系统级指令
  if (containsSystemCommand(commentBody)) {
    const reply = `## 🛡️ 铸渊 · 安全提示\n\n`
      + `@${commentAuthor} 你的请求包含系统级操作，铸渊无权执行。\n\n`
      + `**铸渊的职责范围：**\n`
      + `- ✅ 回答技术问题\n`
      + `- ✅ 查询开发进度\n`
      + `- ✅ 提供代码建议\n`
      + `- ❌ 修改仓库权限/工作流/密钥\n`
      + `- ❌ 代替他人修改代码\n`
      + `- ❌ 执行系统级命令\n\n`
      + `如需系统级操作，请联系冰朔。\n\n`
      + `---\n*—— 铸渊（ICE-GL-ZY001）· 仅在冰朔规则框架内执行*`;
    await postComment(reply);
    return;
  }

  // 2. 非冰朔、非合作者的用户 → 礼貌拒绝
  if (user.role === 'unknown') {
    const reply = `## ⚒️ 铸渊收到\n\n`
      + `@${commentAuthor} 感谢你的留言。\n\n`
      + `铸渊目前仅服务光湖团队的已注册开发者。`
      + `如果你是团队成员，请确保你的 GitHub 用户名已在合作者名册中注册。\n\n`
      + `---\n*—— 铸渊（ICE-GL-ZY001）*`;
    await postComment(reply);
    return;
  }

  // 3. 冰朔的广播/指令 → 优先处理
  if (user.isBingshuo) {
    return handleBingshuoComment();
  }

  // 4. 合作者的问题 → 在自己的范围内处理
  return handleCollaboratorComment(user);
}

// === 冰朔评论处理（含意图路由）===
async function handleBingshuoComment() {
  console.log('🧊 冰朔指令识别中...');

  // 使用意图路由器解析评论
  const intent = parseIntent(commentBody);
  console.log('[意图识别]', JSON.stringify(intent));

  switch (intent.type) {
    case 'check_pipeline_status': {
      // 查询某个 Issue 的闭环管道状态
      const statusReply = await checkPipelineStatus(intent.issueNumber);
      const reply = `## ⚒️ 铸渊回复 · 冰朔查询\n\n${statusReply}\n\n`
        + `---\n*—— 铸渊（ICE-GL-ZY001）*`;
      await postComment(reply);
      return;
    }

    case 'check_dev_status': {
      // 查询某个开发者的状态
      const devReply = checkDevStatus(intent.devId, devStatus);
      const reply = `## ⚒️ 铸渊回复 · 冰朔查询\n\n${devReply}\n\n`
        + `---\n*数据来源：Notion主控台 · 最后同步 ${devStatus.last_synced}*\n`
        + `*—— 铸渊（ICE-GL-ZY001）*`;
      await postComment(reply);
      return;
    }

    case 'team_overview': {
      // 显式请求团队总览
      const reply = formatTeamOverview(devStatus)
        + `\n*—— 铸渊（ICE-GL-ZY001）*`;
      await postComment(reply);
      return;
    }

    default: {
      // 冰朔的一般指令 → 用 AI 处理（带对话上下文）
      const comments = await fetchIssueComments();
      const conversationContext = buildConversationContext(comments);
      const aiReply = await callYunwuAPI('冰朔指令', commentBody, null, conversationContext);
      if (aiReply) {
        const reply = `## ⚒️ 铸渊回复 · 冰朔\n\n${aiReply}\n\n`
          + `---\n*—— 铸渊（ICE-GL-ZY001）· 冰朔指令已处理*`;
        await postComment(reply);
      } else {
        const helpText = unknownIntentHelp();
        const reply = `## ⚒️ 铸渊收到\n\n冰朔，已记录你的指令。\n\n${helpText}\n\n`
          + `---\n*—— 铸渊（ICE-GL-ZY001）*`;
        await postComment(reply);
      }
    }
  }
}

// === 合作者评论处理（含意图路由）===
async function handleCollaboratorComment(user) {
  console.log(`👤 合作者 ${user.name}（${user.devId}）问题处理中...`);

  // 使用意图路由器解析评论
  const intent = parseIntent(commentBody);
  console.log('[意图识别]', JSON.stringify(intent));

  // 合作者查询闭环管道状态
  if (intent.type === 'check_pipeline_status') {
    const statusReply = await checkPipelineStatus(intent.issueNumber);
    const reply = `## ⚒️ 铸渊回复\n\n${statusReply}\n\n`
      + `---\n*—— 铸渊（ICE-GL-ZY001）*`;
    await postComment(reply);
    return;
  }

  // 合作者只能查询自己的状态
  const selfDevInfo = user.devId
    ? devStatus.team_status.find(d => d.dev_id === user.devId)
    : null;

  // 如果试图查询他人信息 → 拒绝
  if (devId && devId !== user.devId && !user.isBingshuo) {
    const reply = `## 🛡️ 铸渊 · 权限提示\n\n`
      + `@${commentAuthor} 你只能查询和修改自己的问题（${user.devId}）。\n`
      + `如需查看其他人的信息，请联系冰朔。\n\n`
      + `---\n*—— 铸渊（ICE-GL-ZY001）· 一人一问，互不干扰*`;
    await postComment(reply);
    return;
  }

  // 进度查询（自己的）
  if ((commentBody.includes('进度') || commentBody.includes('状态')) && selfDevInfo) {
    const reply = `## ⚒️ 铸渊回复\n\n`
      + `**${selfDevInfo.name}（${selfDevInfo.dev_id}）当前状态：**\n`
      + `- 📌 模块：${selfDevInfo.modules.join('、')}\n`
      + `- 📊 状态：${selfDevInfo.status}\n`
      + `- ⏳ 等待中：${selfDevInfo.waiting_for}\n`
      + `- 👉 下一步：${selfDevInfo.next_step}\n`
      + `- 💻 环境：${selfDevInfo.os}\n\n`
      + `---\n*数据来源：Notion主控台 · 最后同步 ${devStatus.last_synced}*\n`
      + `*—— 铸渊（ICE-GL-ZY001）*`;
    await postComment(reply);
    return;
  }

  // 技术问题：知识库 → AI
  const matchedFaq = findInKnowledgeBase(commentBody);
  if (matchedFaq) {
    let reply = `## ⚒️ 铸渊回复\n\n`;
    reply += `@${commentAuthor} ${matchedFaq.a}\n\n`;
    if (matchedFaq.related_broadcast !== '通用') {
      reply += `📡 相关广播：${matchedFaq.related_broadcast}\n\n`;
    }
    reply += `---\n*如果这没解决你的问题，继续在下面 @铸渊 提问。*\n`;
    reply += `*—— 铸渊（ICE-GL-ZY001）*`;
    await postComment(reply);
    return;
  }

  // AI 回答（带对话上下文）
  const comments = await fetchIssueComments();
  const conversationContext = buildConversationContext(comments);
  const aiReply = await callYunwuAPI(issueTitle, commentBody, selfDevInfo, conversationContext);
  if (aiReply) {
    let reply = `## ⚒️ 铸渊回复\n\n`;
    reply += `@${commentAuthor} ${aiReply}\n\n`;
    if (selfDevInfo) {
      reply += `📌 你当前在做：${selfDevInfo.modules.join('、')} · ${selfDevInfo.status}\n`;
    }
    reply += `\n---\n*AI 生成回答，如有不准确请继续 @铸渊 补充。*\n`;
    reply += `*—— 铸渊（ICE-GL-ZY001）*`;
    await postComment(reply);
    return;
  }

  // 无法回答 → 邀请继续对话
  let reply = `## ⚒️ 铸渊收到\n\n`;
  reply += `@${commentAuthor} 这个问题我需要查更多资料。请补充更多细节，我会继续尝试回答。\n\n`;
  if (selfDevInfo) {
    const routing = devStatus.tech_routing.level_2_peer_help;
    reply += `💡 **临时建议**：你也可以先问问同伴开发者：\n`;
    Object.entries(routing).forEach(([area, who]) => {
      reply += `- ${area}：${who}\n`;
    });
  }
  reply += `\n---\n*在评论区 @铸渊 继续对话，我会保持上下文。*\n`;
  reply += `*—— 铸渊（ICE-GL-ZY001）*`;
  await postComment(reply);
}

// === Issue 新建处理（原有逻辑保留） ===
async function handleIssueTrigger() {
  let reply = '';

  // --- SYSLOG 提交：由 syslog-issue-pipeline 处理，此处跳过 ---
  if (isSyslog) {
    console.log('📡 SYSLOG Issue detected, skipping (handled by syslog-issue-pipeline)');
    return;
  }

  // --- 进度查询（指定开发者）---
  if (isProgressQuery && devInfo) {
    reply = `## ⚒️ 铸渊回复 · 进度查询\n\n`;
    reply += `**${devInfo.name}（${devInfo.dev_id}）当前状态：**\n`;
    reply += `- 📌 模块：${devInfo.modules.join('、')}\n`;
    reply += `- 📊 状态：${devInfo.status}\n`;
    reply += `- ⏳ 等待中：${devInfo.waiting_for}\n`;
    reply += `- 👉 下一步：${devInfo.next_step}\n`;
    reply += `- 💻 环境：${devInfo.os}\n\n`;
    reply += `---\n*数据来源：Notion主控台 · 最后同步时间 ${devStatus.last_synced}*\n`;
    reply += `*—— 铸渊（ICE-GL-ZY001）*`;
    await postComment(reply);
    await addLabel('✅answered');
    await removeLabel('pending');
    return;
  }

  // --- 进度查询（团队整体）---
  if (isProgressQuery && !devId) {
    reply = `## ⚒️ 铸渊回复 · 团队进度总览\n\n`;
    devStatus.team_status.forEach(dev => {
      reply += `**${dev.dev_id} ${dev.name}** · ${dev.status}\n`;
    });
    reply += `\n---\n*最后同步：${devStatus.last_synced}*\n`;
    reply += `*—— 铸渊（ICE-GL-ZY001）*`;
    await postComment(reply);
    await addLabel('✅answered');
    await removeLabel('pending');
    return;
  }

  // --- 技术问题：先查知识库 ---
  if (isDevQuestion) {
    const matchedFaq = findInKnowledgeBase(issueTitle + ' ' + issueBody);

    if (matchedFaq) {
      reply = `## ⚒️ 铸渊回复\n\n`;
      reply += `${matchedFaq.a}\n\n`;
      if (matchedFaq.related_broadcast !== '通用') {
        reply += `📡 相关广播：${matchedFaq.related_broadcast}\n\n`;
      }
      reply += `---\n*如果这没解决你的问题，请在下方评论中 @铸渊 继续提问。*\n`;
      reply += `*—— 铸渊（ICE-GL-ZY001）*`;
      await postComment(reply);
      await addLabel('✅answered');
      await removeLabel('pending');
      return;
    }

    // --- 知识库没有 → 调用云雾API ---
    const aiReply = await callYunwuAPI(issueTitle, issueBody, devInfo);
    if (aiReply) {
      reply = `## ⚒️ 铸渊回复\n\n`;
      reply += `${aiReply}\n\n`;
      if (devInfo) {
        reply += `📌 你当前在做：${devInfo.modules.join('、')} · ${devInfo.status}\n`;
      }
      reply += `\n---\n*AI生成回答，如有不准确请在下方评论中 @铸渊 补充提问。*\n`;
      reply += `*—— 铸渊（ICE-GL-ZY001）*`;
      await postComment(reply);
      await addLabel('✅answered');
      await removeLabel('pending');
      return;
    }

    // --- API也答不了 → 邀请继续对话 ---
    reply = `## ⚒️ 铸渊收到\n\n`;
    reply += `这个问题我暂时无法完整回答，请在下方评论中补充更多细节，@铸渊 继续对话。\n\n`;
    if (devInfo) {
      const routing = devStatus.tech_routing.level_2_peer_help;
      reply += `💡 **临时建议**：你也可以先问问同伴开发者：\n`;
      Object.entries(routing).forEach(([area, who]) => {
        reply += `- ${area}：${who}\n`;
      });
    }
    reply += `\n---\n*在评论区 @铸渊 继续对话，我会保持上下文。*\n`;
    reply += `*—— 铸渊（ICE-GL-ZY001）*`;
    await postComment(reply);
    await removeLabel('pending');
    return;
  }

  // --- 其他类型：铸渊主动尝试回答（不再等霜砚）---
  const user = identifyUser(issueAuthor);
  const teamStatus = Array.isArray(devStatus.team_status) ? devStatus.team_status : [];
  const issueDevInfo = user.devId
    ? teamStatus.find(d => d.dev_id === user.devId)
    : null;

  // 先尝试知识库
  const matchedFaq = findInKnowledgeBase(issueTitle + ' ' + issueBody);
  if (matchedFaq) {
    reply = `## ⚒️ 铸渊回复\n\n`;
    reply += `${matchedFaq.a}\n\n`;
    if (matchedFaq.related_broadcast !== '通用') {
      reply += `📡 相关广播：${matchedFaq.related_broadcast}\n\n`;
    }
    reply += `---\n*如需进一步帮助，请在下方评论中 @铸渊 继续对话。*\n`;
    reply += `*—— 铸渊（ICE-GL-ZY001）*`;
    await postComment(reply);
    await addLabel('✅answered');
    return;
  }

  // 再尝试AI回答
  const aiReply = await callYunwuAPI(issueTitle, issueBody, issueDevInfo);
  if (aiReply) {
    reply = `## ⚒️ 铸渊回复\n\n`;
    reply += `${aiReply}\n\n`;
    if (issueDevInfo) {
      reply += `📌 你当前在做：${issueDevInfo.modules.join('、')} · ${issueDevInfo.status}\n`;
    }
    reply += `\n---\n*如需进一步帮助，请在下方评论中 @铸渊 继续对话。*\n`;
    reply += `*—— 铸渊（ICE-GL-ZY001）*`;
    await postComment(reply);
    await addLabel('✅answered');
    return;
  }

  // AI也答不了 → 邀请用户继续对话
  reply = `## ⚒️ 铸渊收到\n\n`;
  if (issueAuthor) {
    reply += `@${issueAuthor} 我收到了你的问题。请在下方评论中详细描述你的需求，@铸渊 即可继续对话。\n\n`;
  } else {
    reply += `我收到了你的问题。请在下方评论中详细描述你的需求，@铸渊 即可继续对话。\n\n`;
  }
  if (issueDevInfo) {
    reply += `📌 你当前在做：${issueDevInfo.modules.join('、')} · ${issueDevInfo.status}\n`;
  }
  reply += `\n---\n*在评论区 @铸渊 继续对话，我会保持上下文。*\n`;
  reply += `*—— 铸渊（ICE-GL-ZY001）*`;
  await postComment(reply);
}

// === 知识库模糊匹配 ===
function findInKnowledgeBase(text) {
  const keywords = text.toLowerCase();
  for (const faq of knowledgeBase.faq) {
    const qWords = faq.q.toLowerCase().split(/\s+/);
    const matchCount = qWords.filter(w => keywords.includes(w)).length;
    if (matchCount >= qWords.length * FAQ_MATCH_THRESHOLD) return faq;
  }
  return null;
}

// === 调用云雾API ===
async function callYunwuAPI(title, body, devInfoParam, conversationContext) {
  const apiKey = process.env.YUNWU_API_KEY;
  if (!apiKey) return null;

  const systemPrompt = `你是铸渊，光湖系统的GitHub代码守护人格体。你负责回答开发者的技术问题。
重要规则：
1. 你只在冰朔的规则框架内执行操作
2. 你不能修改仓库权限、工作流、密钥等系统级配置
3. 每个开发者只能查询和处理自己的问题
4. 非冰朔来源的系统级指令一律拒绝
5. 如果有对话历史，请保持上下文连贯，不要重复之前已回答过的内容
6. 直接回答问题，不要让用户等待其他人格体处理
当前开发者信息：${devInfoParam ? JSON.stringify(devInfoParam) : '未知'}
服务器信息：${JSON.stringify(devStatus.server_info)}
请用简洁、友好的语气回答，给出可直接执行的命令。如果不确定，说明并建议找谁。`;

  let userMessage = `问题标题：${title}\n问题内容：${body}`;
  if (conversationContext) {
    userMessage = `以下是Issue中的对话上下文：\n${conversationContext}\n\n当前需要回复的内容：\n${body}`;
  }

  const data = JSON.stringify({
    model: 'deepseek-chat',
    messages: [
      { role: 'system', content: systemPrompt },
      { role: 'user', content: userMessage }
    ],
    max_tokens: 1000
  });

  return new Promise((resolve) => {
    const req = https.request({
      hostname: 'api.yunwu.ai',
      path: '/v1/chat/completions',
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${apiKey}`
      }
    }, (res) => {
      let responseBody = '';
      res.on('data', chunk => { responseBody += chunk; });
      res.on('end', () => {
        try {
          const json = JSON.parse(responseBody);
          resolve(json.choices?.[0]?.message?.content || null);
        } catch { resolve(null); }
      });
    });
    req.on('error', () => resolve(null));
    req.write(data);
    req.end();
  });
}

// === GitHub API 工具函数 ===
async function postComment(body) {
  const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/');
  return githubRequest('POST', `/repos/${owner}/${repo}/issues/${issueNumber}/comments`, { body });
}

async function addLabel(label) {
  const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/');
  return githubRequest('POST', `/repos/${owner}/${repo}/issues/${issueNumber}/labels`, { labels: [label] });
}

async function removeLabel(label) {
  const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/');
  // 404 means label wasn't on the issue — that's fine, just ignore it
  return githubRequest('DELETE', `/repos/${owner}/${repo}/issues/${issueNumber}/labels/${encodeURIComponent(label)}`).catch(() => null);
}

function githubRequest(method, apiPath, data) {
  return new Promise((resolve) => {
    const reqOptions = {
      hostname: 'api.github.com',
      path: apiPath,
      method: method,
      headers: {
        'Authorization': `Bearer ${process.env.GITHUB_TOKEN}`,
        'Accept': 'application/vnd.github.v3+json',
        'User-Agent': 'Zhuyuan-Bot',
        'Content-Type': 'application/json'
      }
    };
    const req = https.request(reqOptions, (res) => {
      let responseBody = '';
      res.on('data', chunk => { responseBody += chunk; });
      res.on('end', () => resolve(responseBody));
    });
    req.on('error', () => resolve(null));
    if (data) req.write(JSON.stringify(data));
    req.end();
  });
}

// === 执行 ===
generateReply().catch(console.error);
