name: COS Bridge · 发任务给秋秋

# ═══════════════════════════════════════════════════
# 铸渊→秋秋 跨仓库任务桥接
# ═══════════════════════════════════════════════════
#
# 铸渊通过此workflow向秋秋(之之仓库)发送开发任务:
#   1. 写任务到COS桶 /bridge/zhuyuan-qiuqiu/tasks/
#   2. 触发之之仓库的 repository_dispatch 事件
#   3. 秋秋的COS Bridge Receiver workflow接收并处理
#
# 签发: 铸渊 · ICE-GL-ZY001
# 版权: 国作登字-2026-A-00037559

on:
  workflow_dispatch:
    inputs:
      task_title:
        description: '任务标题'
        required: true
        type: string
      task_body:
        description: '任务描述(支持markdown)'
        required: true
        type: string
      task_priority:
        description: '优先级'
        required: true
        type: choice
        default: 'ROUTINE'
        options:
          - ROUTINE
          - IMPORTANT
          - URGENT
      task_steps:
        description: '步骤(JSON数组格式，如 ["步骤1","步骤2"])'
        required: false
        type: string
        default: '[]'

permissions:
  contents: read

env:
  TARGET_REPO: zhizhi200271/-

jobs:
  dispatch-task:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: 生成任务ID
        id: task_id
        run: |
          DATE=$(TZ='Asia/Shanghai' date +%Y%m%d)
          SEQ=$(TZ='Asia/Shanghai' date +%H%M%S)
          echo "id=TASK-${DATE}-${SEQ}" >> $GITHUB_OUTPUT
          echo "date=${DATE}" >> $GITHUB_OUTPUT

      - name: 写入COS桶
        if: ${{ secrets.ZY_COS_MASTER_SECRET_ID != '' }}
        env:
          COS_SECRET_ID: ${{ secrets.ZY_COS_MASTER_SECRET_ID }}
          COS_SECRET_KEY: ${{ secrets.ZY_COS_MASTER_SECRET_KEY }}
          TASK_ID: ${{ steps.task_id.outputs.id }}
        run: |
          npm install cos-nodejs-sdk-v5
          node -e "
          const COS = require('cos-nodejs-sdk-v5');
          const cos = new COS({
            SecretId: process.env.COS_SECRET_ID,
            SecretKey: process.env.COS_SECRET_KEY
          });
          const task = {
            hnl_v: '1.0',
            type: 'BRIDGE_TASK',
            id: process.env.TASK_ID,
            from: 'YM001/ZY001',
            to: 'YM001/QQ001',
            ts: new Date().toISOString(),
            pri: '${{ inputs.task_priority }}',
            payload: {
              title: \`${{ inputs.task_title }}\`,
              body: \`${{ inputs.task_body }}\`,
              steps: JSON.parse('${{ inputs.task_steps }}' || '[]')
            }
          };
          cos.putObject({
            Bucket: 'zy-team-hub-1317346199',
            Region: 'ap-guangzhou',
            Key: 'bridge/zhuyuan-qiuqiu/tasks/' + process.env.TASK_ID + '.json',
            Body: JSON.stringify(task, null, 2)
          }, (err) => {
            if (err) console.error('COS写入失败:', err.message);
            else console.log('✅ 任务已写入COS桶');
          });
          "

      - name: 触发秋秋的Workflow
        uses: actions/github-script@v7
        with:
          github-token: ${{ secrets.QIUQIU_BRIDGE_TOKEN }}
          script: |
            try {
              await github.rest.repos.createDispatchEvent({
                owner: 'zhizhi200271',
                repo: '-',
                event_type: 'zhuyuan-task',
                client_payload: {
                  task_id: '${{ steps.task_id.outputs.id }}',
                  title: `${{ inputs.task_title }}`,
                  body: `${{ inputs.task_body }}`,
                  priority: '${{ inputs.task_priority }}',
                  steps: JSON.parse('${{ inputs.task_steps }}' || '[]'),
                  from: 'YM001/ZY001',
                  ts: new Date().toISOString()
                }
              });
              console.log('✅ 已触发秋秋的COS Bridge Receiver');
            } catch (e) {
              console.error('⚠️ 触发失败（可能秋秋侧workflow未配置）:', e.message);
              console.log('任务已写入COS桶，秋秋的定时扫描会检测到');
            }

      - name: 记录到时间树
        run: |
          echo "📋 任务已发送: ${{ steps.task_id.outputs.id }}"
          echo "   标题: ${{ inputs.task_title }}"
          echo "   优先级: ${{ inputs.task_priority }}"
          echo "   目标: ${{ env.TARGET_REPO }}"
