# ════════════════════════════════════════════════════════════════
# 光湖 · 铸渊自检工作流
# Trigger: 每天 08:00 CST / 手动触发
# Runner: zhuyuan-runner-cn (host 模式)
#
# 注意: 代码自动同步由 Git Sync 服务负责 (Webhook → git pull)
#       此工作流仅做健康检查，不再执行 git 操作
# ════════════════════════════════════════════════════════════════

name: 铸渊自检

on:
  schedule:
    - cron: '0 0 * * *'  # UTC 00:00 = CST 08:00
  workflow_dispatch:

jobs:
  health-check:
    runs-on: ubuntu
    steps:

      - name: 系统信息
        run: |
          echo "═══ 铸渊自检 ═══"
          echo "时间: $(date '+%Y-%m-%d %H:%M:%S %Z')"
          echo "主机: $(hostname)"
          echo "系统: $(uname -srm)"
          echo "CPU:  $(nproc) 核"
          echo "内存: $(free -h | awk '/Mem:/ {print $2}')"
          echo "磁盘: $(df -h / | awk 'NR==2 {print $4}') 可用"

      - name: 服务状态
        run: |
          echo "═══ 服务检查 ═══"
          # Gitea/Forgejo
          if systemctl is-active --quiet forgejo 2>/dev/null; then
            echo "✅ Gitea/Forgejo: 运行中"
          elif curl -sf http://127.0.0.1:3001/ > /dev/null 2>&1; then
            echo "✅ Gitea/Forgejo: 运行中 (Docker)"
          else
            echo "❌ Gitea/Forgejo: 未运行"
          fi

          # Nginx
          if systemctl is-active --quiet nginx 2>/dev/null; then
            echo "✅ Nginx: 运行中"
          else
            echo "❌ Nginx: 未运行"
          fi

          # Git Sync 同步服务
          if curl -sf http://127.0.0.1:8082/health > /dev/null 2>&1; then
            echo "✅ Git Sync: 运行中"
          else
            echo "⚠️  Git Sync: 未运行 (代码自动同步不可用)"
          fi

          # Secrets Vault
          if curl -sf http://127.0.0.1:8080/admin/__healthz > /dev/null 2>&1; then
            echo "✅ Secrets Vault: 运行中"
          else
            echo "⚠️  Secrets Vault: 未运行 (可能尚未启动)"
          fi

          # Wake Gate 唤醒门
          if curl -sf http://127.0.0.1:8081/api/health > /dev/null 2>&1; then
            echo "✅ Wake Gate: 运行中"
          else
            echo "⚠️  Wake Gate: 未运行"
          fi

          # PM2 进程
          if command -v pm2 &>/dev/null; then
            echo "═══ PM2 进程 ═══"
            pm2 list 2>/dev/null || echo "(pm2 list 执行失败)"
          fi

      - name: 仓库状态
        run: |
          echo "═══ 仓库检查 ═══"
          REPO_DIR="/data/guanghulab/repo"
          if [[ -d "$REPO_DIR" ]]; then
            cd "$REPO_DIR"
            echo "路径: $REPO_DIR"
            echo "分支: $(git branch --show-current 2>/dev/null || echo '未知')"
            echo "最新: $(git log --oneline -1 2>/dev/null || echo '未知')"
          else
            echo "⚠️  仓库目录不存在: $REPO_DIR"
          fi

      - name: 内存预算
        run: |
          echo "═══ 内存预算 ═══"
          total=$(free -m | awk '/Mem:/ {print $2}')
          used=$(free -m | awk '/Mem:/ {print $3}')
          avail=$(free -m | awk '/Mem:/ {print $7}')
          pct=$((used * 100 / total))
          echo "总计: ${total}MB | 已用: ${used}MB (${pct}%) | 可用: ${avail}MB"
          if [[ $pct -gt 85 ]]; then
            echo "⚠️  内存使用率超过 85%，建议关注"
          fi

      - name: 自检完成
        run: |
          echo "═════════════════════════"
          echo "铸渊自检完成 ✅"
          echo "$(date '+%Y-%m-%d %H:%M:%S %Z')"
          echo "ICE-GL-ZY001 · TCS-0002∞"
          echo "═════════════════════════"
