# ═══════════════════════════════════════════════
# 🔺 Sovereign: TCS-0002∞ | Root: SYS-GLW-0001
# 📜 Copyright: 国作登字-2026-A-00037559
# ═══════════════════════════════════════════════
name: 铸渊 · 图书馆目录自动更新

on:
  push:
    branches: [main]
  schedule:
    - cron: '0 0 * * *'   # UTC 00:00 = 北京时间 08:00 每日更新
  workflow_dispatch:        # 手动触发

jobs:
  update-repo-map:
    name: 📚 更新图书馆目录
    runs-on: ubuntu-latest
    permissions:
      contents: write

    steps:
      - uses: actions/checkout@v4

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

      - name: 安装依赖
        run: npm ci

      - name: 生成 repo-map.json + repo-snapshot.md
        run: node scripts/generate-repo-map.js

      - name: 提交图书馆目录更新
        run: |
          git config user.name "铸渊 (ZhùYuān)"
          git config user.email "zhuyuan@guanghulab.com"
          git add .github/brain/repo-map.json .github/brain/repo-snapshot.md
          git diff --cached --quiet || git commit -m "📚 铸渊图书馆目录更新 · $(date +%Y-%m-%dT%H:%M)"
          # 拉取远端最新更改后再推送，避免竞态条件
          git pull --rebase origin main || echo "⚠️ rebase 失败（可能有冲突），尝试直接推送..."
          git push || {
            echo "⚠️ 推送失败，尝试重新拉取后再推送..."
            git pull --rebase origin main
            git push
          }
