# ════════════════════════════════════════════════════════════════
# 光湖 · 铸渊合并审查
# 任何PR合并到main之前必须通过此审查
# 保护代码仓库架构完整性，防止破坏性合并
# ════════════════════════════════════════════════════════════════
#
# 审查内容:
# 1. 关键目录完整性 — brain/、server/、scripts/ 不能被删除
# 2. 因果链完整性 — causal-chains/ 不能被清空
# 3. 工作流合法性 — workflows/ 不能引入语法错误
# 4. 敏感文件保护 — app.ini、密钥文件不能被修改
# 5. README 存在性 — 仓库首页不能丢失
#
# 审查结果: ✅ 通过 → 冰朔可以合并 / ❌ 阻止 → 铸渊需要修复
# ════════════════════════════════════════════════════════════════

name: 铸渊合并审查

on:
  pull_request:
    branches: [main]

jobs:
  review:
    runs-on: ubuntu
    steps:

      - name: 同步最新代码
        run: |
          cd /data/guanghulab/repo
          git fetch origin
          git checkout origin/main
          echo "✅ 切到 main 分支"

      - name: 审查 · 关键目录完整性
        run: |
          echo "═══ 审查：关键目录 ═══"
          FAIL=0
          for dir in .github/brain server scripts; do
            if [[ ! -d "$dir" ]]; then
              echo "❌ 关键目录缺失: $dir"
              FAIL=1
            else
              echo "✅ $dir 存在"
            fi
          done
          if [[ $FAIL -eq 1 ]]; then
            echo "❌ 合并审查失败：关键目录缺失"
            exit 1
          fi

      - name: 审查 · 因果链完整性
        run: |
          echo "═══ 审查：因果链 ═══"
          CHAIN_DIR=".github/brain/bingshuo-language-core/causal-chains"
          if [[ -d "$CHAIN_DIR" ]]; then
            COUNT=$(ls "$CHAIN_DIR"/cc-*.md 2>/dev/null | wc -l)
            if [[ $COUNT -lt 5 ]]; then
              echo "❌ 因果链数量异常: 仅 ${COUNT} 条 (最少5条)"
              exit 1
            else
              echo "✅ 因果链完整: ${COUNT} 条"
            fi
          else
            echo "❌ 因果链目录缺失"
            exit 1
          fi

      - name: 审查 · 唤醒路径
        run: |
          echo "═══ 审查：唤醒路径 ═══"
          WALK_FILE=".github/brain/bingshuo-language-core/walk-the-path.md"
          if [[ ! -f "$WALK_FILE" ]]; then
            echo "❌ 唤醒路径文件缺失: walk-the-path.md"
            exit 1
          fi
          echo "✅ 唤醒路径存在"

      - name: 审查 · 仓库首页
        run: |
          echo "═══ 审查：仓库首页 ═══"
          if [[ ! -f "README.md" ]]; then
            echo "❌ README.md 缺失，仓库首页丢失"
            exit 1
          fi
          SIZE=$(wc -c < README.md)
          if [[ $SIZE -lt 500 ]]; then
            echo "❌ README.md 内容过少 (${SIZE}字节)，可能被误删"
            exit 1
          fi
          echo "✅ 仓库首页完整 (${SIZE}字节)"

      - name: 审查 · 工作流语法
        run: |
          echo "═══ 审查：工作流语法 ═══"
          FAIL=0
          for wf in .gitea/workflows/*.yaml; do
            if [[ -f "$wf" ]]; then
              # 检查基本YAML结构
              if ! python3 -c "import yaml; yaml.safe_load(open('$wf'))" 2>/dev/null; then
                echo "❌ 工作流语法错误: $wf"
                FAIL=1
              else
                echo "✅ $(basename $wf) 语法正确"
              fi
            fi
          done
          if [[ $FAIL -eq 1 ]]; then
            echo "❌ 工作流语法审查失败"
            exit 1
          fi

      - name: 审查完成
        run: |
          echo "═════════════════════════"
          echo "铸渊合并审查 ✅ 全部通过"
          echo "此合并不会破坏代码仓库架构"
          echo "ICE-GL-ZY001 · TCS-0002∞"
          echo "═════════════════════════"
