name: "SkyEye Weekly Full Scan"

on:
  schedule:
    - cron: '0 12 * * 6'  # 20:00 CST (UTC+8) every Saturday
  repository_dispatch:
    types: [skyeye-full-scan]  # 手动触发

permissions:
  contents: write

jobs:
  weekly-scan:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: "🧐 Wake up SkyEye Core Brain"
        run: echo "[SkyEye] Core brain awakened for weekly full scan at $(date -u '+%Y-%m-%dT%H:%M:%SZ')"

      - name: "🔍 Phase 1 - Infrastructure Scan"
        run: node skyeye/scripts/scan-engine.js --mode=full

      - name: "💰 Phase 2 - Quota Audit"
        run: node skyeye/scripts/quota-tracker.js --audit

      - name: "⚙️ Phase 3 - Optimize Guards"
        run: node skyeye/scripts/optimizer.js --apply

      - name: "🩹 Phase 4 - Self-Heal"
        run: node skyeye/scripts/self-healer.js

      - name: "📝 Phase 5 - Generate Report"
        run: node skyeye/scripts/weekly-scan.js --report

      - name: "📊 Phase 6 - Architecture Summary → README"
        run: node scripts/skyeye/generate-arch-summary.js

      - name: "Commit scan results"
        run: |
          git config user.name "天眼 · SkyEye Core"
          git config user.email "skyeye@guanghu.system"
          git add skyeye/ buffer/config/ README.md
          git diff --cached --quiet || git commit -m "🦅 [skyeye-weekly] $(date -u +%Y%m%d) full scan + arch summary [skip ci]"
          git push

  # Notion 同步 · 架构汇总推送到 Notion（通过 Bridge E）
  notify-notion:
    needs: weekly-scan
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          ref: main  # 确保拉到刚 push 的最新版

      - name: "📡 Sync arch summary to Notion via Bridge E"
        env:
          NOTION_TOKEN:     ${{ secrets.NOTION_API_KEY }}
          CHANGES_DB_ID:    ${{ secrets.NOTION_CHANGE_LOG_DB_ID }}
          EVENT_TYPE:       commit
          COMMIT_SHA:       ${{ github.sha }}
          COMMIT_MSG:       "🦅 天眼周报 · 系统架构汇总自动同步"
          COMMITTER:        "天眼 · SkyEye Core"
          COMMIT_TIMESTAMP: ${{ github.event.repository.updated_at }}
          BRANCH:           main
          CHANGED_FILES:    "README.md\nskyeye/scan-report/"
        run: node scripts/notion-bridge.js changes

      # ━━━ 🧬 双端神经系统·自报告 ━━━
      - name: "🧬 写入神经自报告"
        if: always()
        run: |
          WORKFLOW_ID="weekly-scan"
          BRAIN="AG-TY-01"
          REPORT_DIR="data/neural-reports/weekly-scan"
          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 "weekly-scan" \
            --summary "weekly-scan 自动执行" \
            --related-agent "AG-TY-01"

