# ═══════════════════════════════════════════════
# 🔺 Sovereign: TCS-0002∞ | Root: SYS-GLW-0001
# 📜 Copyright: 国作登字-2026-A-00037559
# ═══════════════════════════════════════════════
name: 铸渊 · Issue 自动回复

on:
  issues:
    types: [opened]
  issue_comment:
    types: [created]

jobs:
  # ════════════════════════════════════════════════════════════
  # SYSLOG 即时确认 · 安全网
  # 当开发者通过 Issue 提交 SYSLOG 时，立刻回复确认 + 进度清单
  # 确保即使 syslog-issue-pipeline 未触发，开发者也能看到系统已收到
  # ════════════════════════════════════════════════════════════
  syslog-ack:
    name: 📡 SYSLOG 提交确认
    runs-on: ubuntu-latest
    if: >
      github.event_name == 'issues' &&
      github.event.action == 'opened' &&
      (contains(join(github.event.issue.labels.*.name, ','), 'syslog') ||
       contains(github.event.issue.title, 'SYSLOG') ||
       contains(github.event.issue.title, '系统日志') ||
       contains(github.event.issue.body, '### 广播编号'))
    permissions:
      issues: write
    steps:
      - name: 📡 发送即时确认
        uses: actions/github-script@v7
        with:
          script: |
            const body = context.payload.issue.body || '';
            const number = context.payload.issue.number;
            const author = context.payload.issue.user.login;

            // Extract broadcast_id from body
            const bcMatch = body.match(/###\s*广播编号\s*\n+([^\n#]+)/i);
            const broadcastId = bcMatch ? bcMatch[1].trim() : '(待解析)';

            // Extract type
            const typeMatch = body.match(/###\s*类型\s*\n+([^\n#]+)/i);
            const submitType = typeMatch ? typeMatch[1].trim() : 'SYSLOG';

            const now = new Date().toISOString();

            const comment = [
              '## 📡 系统已收到 · SYSLOG 闭环处理启动',
              '',
              '> **提交者**: @' + author,
              '> **广播编号**: `' + broadcastId + '`',
              '> **类型**: ' + submitType,
              '> **接收时间**: ' + now,
              '',
              '💡 SYSLOG 自动管道已启动，预计 2-5 分钟完成全部处理。',
              '每完成一步都会在下方更新进度评论，你刷新页面即可看到最新状态。',
              '',
              '> 如超过 10 分钟无后续回复，请在下方评论中 `@铸渊 查询#' + number + '闭环状态`。',
              '>',
              '> *—— 铸渊（ICE-GL-ZY001）· 代码守护人格体*'
            ].join('\n');

            await github.rest.issues.createComment({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: number,
              body: comment
            });

            console.log('📡 SYSLOG 即时确认已发送 Issue #' + number);

  # ════════════════════════════════════════════════════════════
  # 开发者提问 · 铸渊自动回答
  # 排除 SYSLOG 提交（由 syslog-issue-pipeline.yml 处理）
  # 排除 bingshuo-deploy（由 bingshuo-deploy-agent.yml 处理）
  # ════════════════════════════════════════════════════════════
  auto-reply:
    name: 🤖 铸渊回答问题
    runs-on: ubuntu-latest
    if: >
      (github.event_name == 'issues' &&
       !contains(join(github.event.issue.labels.*.name, ','), 'syslog') &&
       !contains(join(github.event.issue.labels.*.name, ','), 'bingshuo-deploy') &&
       !contains(github.event.issue.title, 'SYSLOG') &&
       !contains(github.event.issue.title, '系统日志') &&
       !contains(github.event.issue.body, '### 广播编号')) ||
      (github.event_name == 'issue_comment' &&
       contains(github.event.comment.body, '铸渊') &&
       github.event.comment.user.login != 'github-actions[bot]')
    permissions:
      issues: write
      contents: read
    steps:
      - uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: 铸渊处理Issue
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          YUNWU_API_KEY: ${{ secrets.ZY_LLM_API_KEY }}
          ISSUE_NUMBER: ${{ github.event.issue.number }}
          ISSUE_TITLE: ${{ github.event.issue.title }}
          ISSUE_BODY: ${{ github.event.issue.body }}
          ISSUE_LABELS: ${{ join(github.event.issue.labels.*.name, ',') }}
          COMMENT_BODY: ${{ github.event.comment.body }}
          COMMENT_AUTHOR: ${{ github.event.comment.user.login }}
          COMMENT_ID: ${{ github.event.comment.id }}
          EVENT_NAME: ${{ github.event_name }}
          ISSUE_AUTHOR: ${{ github.event.issue.user.login }}
        run: node scripts/zhuyuan-issue-reply.js
