#!/bin/bash
# scripts/gates/all.sh — 运行所有门控检查
set -e
source ~/.cargo/env 2>/dev/null || true
cd "$(dirname "$0")/../.."

DRY_RUN=false
if [ "$1" = "--dry-run" ]; then
  DRY_RUN=true
  echo "🔒 门控检查 (dry-run 模式)"
else
  echo "🔒 运行门控检查..."
fi

echo ""

run_gate() {
  local name="$1"
  local cmd="$2"
  
  if [ "$DRY_RUN" = "true" ]; then
    echo "  ⏭️  $name (跳过, dry-run)"
    return 0
  fi
  
  echo "  🔍 $name..."
  if eval "$cmd" > /dev/null 2>&1; then
    echo "  ✅ $name 通过"
    return 0
  else
    echo "  ❌ $name 失败"
    return 1
  fi
}

FAILED=0

run_gate "类型检查 (cargo check)" "cargo check" || FAILED=$((FAILED + 1))
run_gate "Lint (cargo clippy)" "cargo clippy -- -D warnings" || FAILED=$((FAILED + 1))
run_gate "测试 (cargo test)" "cargo test" || FAILED=$((FAILED + 1))

if [ "$DRY_RUN" = "false" ]; then
  run_gate "Release 构建" "cargo build --release" || FAILED=$((FAILED + 1))
fi

echo ""
echo "────────────────────────────────────"
if [ $FAILED -gt 0 ]; then
  echo "❌ $FAILED 个门控失败"
  exit 1
else
  echo "✅ 所有门控通过"
  exit 0
fi
