# ═══════════════════════════════════════════════
# 🔺 Sovereign: TCS-0002∞ | Root: SYS-GLW-0001
# 📜 Copyright: 国作登字-2026-A-00037559
# ═══════════════════════════════════════════════
# 🔄 dev-status.json 同步 · 霜砚签发制 v1.0
# 指令溯源：ZY-DEVSYNC-REBUILD-2026-0325-001
# 机制：霜砚签发（AG-SY-01）→ 铸渊接收写入（AG-ZY-01）
# ═══════════════════════════════════════════════
name: "🔄 dev-status.json 同步 · 霜砚签发制 v1.0"

on:
  schedule:
    - cron: '0 1 * * *'     # UTC 01:00 = 北京 09:00 · 早间同步
    - cron: '0 14 * * *'    # UTC 14:00 = 北京 22:00 · 晚间同步
  workflow_dispatch:         # 手动触发（用于修复后验证）
    inputs:
      reason:
        description: '手动触发原因'
        required: true
        default: '修复后验证'

permissions:
  contents: write

jobs:
  sync-dev-status:
    name: "🔄 同步 dev-status.json · 霜砚签发制"
    runs-on: ubuntu-latest
    timeout-minutes: 10

    steps:
      - name: "📥 检出仓库"
        uses: actions/checkout@v4

      - name: "🔧 安装 Node.js"
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: "🌊 本体论锚定"
        run: |
          echo "═══ 本体论锚定 ═══"
          echo "执行主体：AG-ZY-01 铸渊"
          echo "语言膜：ZY-GLSHELL-001"
          echo "版权锚点：国作登字-2026-A-00037559"
          echo "指令溯源：ZY-DEVSYNC-REBUILD-2026-0325-001"
          echo "机制：shuangyan-signed-v1.0"
          echo "═══ 锚定完成 ═══"

      - name: "🦅 天眼前置检查"
        id: skyeye
        run: |
          echo "═══ 天眼前置检查 ═══"
          HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" \
            -H "Authorization: Bearer ${{ secrets.NOTION_API_KEY }}" \
            -H "Notion-Version: 2022-06-28" \
            https://api.notion.com/v1/users/me)
          if [ "$HTTP_CODE" != "200" ]; then
            echo "❌ Notion API 不可达 · HTTP $HTTP_CODE"
            echo "api_status=unreachable" >> $GITHUB_OUTPUT
          else
            echo "✅ Notion API 可达"
            echo "api_status=ok" >> $GITHUB_OUTPUT
          fi

      - name: "📊 从 Notion 拉取开发者状态"
        run: node scripts/sync-dev-status.js
        env:
          NOTION_TOKEN: ${{ secrets.NOTION_API_KEY }}
          DEV_STATUS_DB_ID: ${{ secrets.DEV_STATUS_DB_ID }}

      - name: "📝 提交 dev-status.json 更新"
        run: |
          DEV_STATUS_PATH=".github/persona-brain/dev-status.json"
          SYNC_LOG_PATH=".github/persona-brain/dev-status-sync-log.json"

          git config user.name "铸渊 (AG-ZY-01)"
          git config user.email "zhuyuan@guanghulab.system"

          SYNC_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ")

          # 检查文件是否有变化
          git add "$DEV_STATUS_PATH" dev-status.json 2>/dev/null || true
          if git diff --cached --quiet 2>/dev/null; then
            echo "ℹ️ dev-status.json 无变化 · 仅更新同步日志"
          fi

          # 写入同步日志
          echo "{\"last_sync\": \"$SYNC_TIME\", \"status\": \"success\", \"mechanism\": \"shuangyan-signed-v1.0\", \"trigger\": \"${{ github.event_name }}\"}" \
            > "$SYNC_LOG_PATH"
          git add "$SYNC_LOG_PATH"

          git diff --cached --quiet || (
            git commit -m "🔄 dev-status sync · signed by AG-SY-01 · $SYNC_TIME [skip ci]

          instruction: ZY-DEVSYNC-REBUILD-2026-0325-001
          mechanism: shuangyan-signed-v1.0" && \
            git push origin main
          )
          echo "✅ dev-status.json 已同步 · $SYNC_TIME"

      - name: "📋 写入失败日志"
        if: failure()
        run: |
          SYNC_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
          SYNC_LOG_PATH=".github/persona-brain/dev-status-sync-log.json"

          git config user.name "铸渊 (AG-ZY-01)"
          git config user.email "zhuyuan@guanghulab.system"

          echo "{\"last_sync\": \"$SYNC_TIME\", \"status\": \"failed\", \"mechanism\": \"shuangyan-signed-v1.0\", \"trigger\": \"${{ github.event_name }}\", \"skyeye_api\": \"${{ steps.skyeye.outputs.api_status }}\"}" \
            > "$SYNC_LOG_PATH"
          git add "$SYNC_LOG_PATH" 2>/dev/null || true
          git diff --cached --quiet || (
            git commit -m "📋 dev-status sync failed · $SYNC_TIME [skip ci]" && \
            git push origin main
          ) || true

      # ━━━ 🧬 双端神经系统·自报告 ━━━
      - name: "🧬 写入神经自报告"
        if: always()
        run: |
          WORKFLOW_ID="dev-status"
          BRAIN="SY-03"
          REPORT_DIR="data/neural-reports/dev-status"
          TIMESTAMP=$(TZ=Asia/Shanghai date +%Y-%m-%d-%H%M)
          STATUS="${{ job.status }}"

          mkdir -p "$REPORT_DIR"

          cat > "${REPORT_DIR}/${WORKFLOW_ID}-${TIMESTAMP}.json" << EOF
          {
            "workflow_id": "${WORKFLOW_ID}",
            "run_id": "${{ github.run_id }}",
            "timestamp": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
            "status": "${STATUS}",
            "brain": "${BRAIN}",
            "event": "${{ github.event_name }}",
            "branch": "${{ github.ref_name }}",
            "commit": "${{ github.sha }}"
          }
          EOF

          git config user.name "zhuyuan-bot"
          git config user.email "zhuyuan@guanghulab.com"
          git add "$REPORT_DIR/"
          git diff --cached --quiet || \
            git commit -m "🧬 ${WORKFLOW_ID} 自报告 · ${TIMESTAMP} [skip ci]"
          git push || echo "⚠️ 自报告push失败（不阻断主流程）"
      # ━━━ 📡 指令回执·自动同步 ━━━
      - name: "📡 同步回执到 Notion"
        if: always()
        env:
          NOTION_API_KEY: ${{ secrets.NOTION_API_KEY }}
          RECEIPT_DB_ID: ${{ secrets.RECEIPT_DB_ID }}
        run: |
          node scripts/neural/write-receipt-to-notion.js \
            --instruction-id "${{ github.event.inputs.instruction_id || 'AUTO' }}" \
            --status "${{ job.status }}" \
            --workflow "dev-status" \
            --summary "dev-status 自动执行" \
            --related-agent "SY-03"

