# ════════════════════════════════════════════════════════
# 部署大桌子小桌子演示页面 /desk/
# ────────────────────────────────────────────────────────
# 触发：手动 dispatch 或 push 到 main
# 作用：配置 Nginx /desk/ location + 部署静态文件
# ════════════════════════════════════════════════════════
name: 部署 Desk Demo

on:
  workflow_dispatch:
  push:
    branches: [main]
    paths:
      - 'desk-demo/**'
      - '.gitea/workflows/deploy-desk-demo.yaml'

jobs:
  deploy-desk:
    runs-on: ubuntu
    steps:
      - name: 同步最新代码
        run: |
          cd /data/guanghulab/repo
          git fetch origin
          git reset --hard origin/main
          echo "✅ 代码已同步到最新"

      - name: 部署 desk-demo 静态文件
        run: |
          echo "═══ 部署 desk-demo 静态文件 ═══"
          DESK_DIR="/var/www/guanghulab/desk"
          mkdir -p "$DESK_DIR"
          cp /data/guanghulab/repo/desk-demo/index.html "$DESK_DIR/index.html"
          chown -R www-data:www-data "$DESK_DIR" 2>/dev/null || true
          chmod 755 "$DESK_DIR"
          chmod 644 "$DESK_DIR/index.html"
          echo "✅ desk-demo 文件已部署到 $DESK_DIR"
          ls -la "$DESK_DIR"

      - name: 配置 Nginx /desk/ location
        run: |
          echo "═══ 配置 Nginx /desk/ location ═══"
          
          NGINX_CONF="/etc/nginx/sites-available/guanghulab-cvm"
          
          # 检查 /desk/ location 是否已存在
          if grep -q "location /desk/" "$NGINX_CONF" 2>/dev/null; then
            echo "⚠️  /desk/ location 已存在，跳过添加"
          else
            echo "📝 添加 /desk/ location 到 Nginx 配置..."
            
            # 在 Forgejo location / 之前插入 /desk/ location
            # 找到 "location / {" 这一行，在它前面插入 desk location
            sed -i '/# ─── Forgejo 代码仓库 (\/)/i\
    # ─── Desk Demo 大桌子小桌子 (\/desk\/) ───\
    location /desk/ {\
        alias /var/www/guanghulab/desk/;\
        index index.html;\
        try_files $uri $uri/ /desk/index.html;\
    }\
' "$NGINX_CONF"
            
            echo "✅ /desk/ location 已添加"
          fi
          
          echo "═══ 当前 Nginx 配置中 /desk/ 相关内容 ═══"
          grep -A5 "desk" "$NGINX_CONF" || echo "(未找到 desk 相关内容)"

      - name: 测试并重载 Nginx
        run: |
          echo "═══ 测试 Nginx 配置 ═══"
          if nginx -t; then
            echo "✅ Nginx 配置测试通过"
            echo "═══ 重载 Nginx ═══"
            systemctl reload nginx
            echo "✅ Nginx 已重载"
          else
            echo "❌ Nginx 配置测试失败"
            exit 1
          fi

      - name: 验证部署
        run: |
          sleep 2
          echo "═══ 验证 /desk/ 部署 ═══"
          
          HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1/desk/)
          echo "HTTP 状态码: $HTTP_CODE"
          
          if [ "$HTTP_CODE" = "200" ]; then
            TITLE=$(curl -s http://127.0.0.1/desk/ | grep -o '<title>[^<]*</title>' | head -1)
            echo "页面标题: $TITLE"
            
            if echo "$TITLE" | grep -q "大桌子小桌子"; then
              echo "✅ Desk Demo 部署成功！页面标题正确"
            else
              echo "⚠️  页面标题不匹配，可能仍指向首页"
            fi
          else
            echo "❌ /desk/ 返回非200状态码"
            exit 1
          fi
          
          echo "═══ 部署完成 ✅ $(date '+%Y-%m-%d %H:%M:%S') ═══"
