name: Deploy Staging (terry-wallet-login)

on:
  push:
    branches:
      - terry-wallet-login

jobs:
  deploy:
    runs-on: self-hosted

    steps:
      - name: Free disk space
        run: |
          set +e
          echo "=== Disk before cleanup ==="
          df -h
          du -sh "$HOME/.cache/act" "$HOME/.npm" "$HOME/actions-runner/_work" 2>/dev/null

          # DO NOT touch ~/.cache/act for the current job — only sweep dirs older than 60 min.
          if [ -d "$HOME/.cache/act" ]; then
            find "$HOME/.cache/act" -mindepth 1 -maxdepth 1 -type d -mmin +60 -exec rm -rf {} + 2>/dev/null
          fi

          # Wipe npm and setup-node caches (cache: npm will repopulate from registry).
          rm -rf "$HOME/.npm/_cacache" "$HOME/.npm/_logs" 2>/dev/null
          rm -rf "$HOME/.cache/setup-node" 2>/dev/null

          # Old actions-runner workspaces (>6h)
          if [ -d "$HOME/actions-runner/_work" ]; then
            find "$HOME/actions-runner/_work" -mindepth 1 -maxdepth 2 -mmin +360 -exec rm -rf {} + 2>/dev/null
          fi

          # Docker aggressive prune (all dangling + unused, including volumes)
          if command -v docker >/dev/null 2>&1; then
            docker system prune -af --volumes 2>/dev/null
          fi

          # apt/yum cache
          if command -v apt-get >/dev/null 2>&1; then
            sudo apt-get clean 2>/dev/null
          fi
          if command -v yum >/dev/null 2>&1; then
            sudo yum clean all 2>/dev/null
          fi

          # /tmp leftovers (>30 min) other than active runner state
          find /tmp -mindepth 1 -maxdepth 1 -mmin +30 \
            -not -name 'runner*' -not -name 'act*' -not -name 'tmp.*' \
            -exec rm -rf {} + 2>/dev/null

          # journald logs vacuum to 100M
          if command -v journalctl >/dev/null 2>&1; then
            sudo journalctl --vacuum-size=100M 2>/dev/null
          fi

          echo "=== Disk after cleanup ==="
          df -h
          exit 0

      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "22"
          cache: npm

      - name: Install dependencies
        run: npm ci

      - name: Type check
        run: npx tsc --noEmit

      - name: Format check
        run: npm run format:check

      - name: Test
        run: npm test

      - name: Build
        run: npm run build
        env:
          VITE_API_URL: ""
          VITE_API_PREFIX: "/apnew"
          VITE_DISABLE_ADMIN: "true"

      - name: Setup SSH key
        run: |
          mkdir -p ~/.ssh
          echo "${{ secrets.STAGING_SSH_KEY }}" > ~/.ssh/staging_key
          chmod 600 ~/.ssh/staging_key
          ssh-keyscan -H ${{ secrets.STAGING_HOST }} >> ~/.ssh/known_hosts 2>/dev/null

      - name: Deploy to staging server
        run: |
          set -euo pipefail
          HOST="${{ secrets.STAGING_HOST }}"
          USER="${{ secrets.STAGING_USER }}"
          echo ">>> 部署到 staging $USER@$HOST"
          rsync -avz --delete \
            -e "ssh -i ~/.ssh/staging_key -o StrictHostKeyChecking=no" \
            dist/ \
            "${USER}@${HOST}:/var/www/ark-library-staging/"
          echo ">>> staging 部署完成"

      - name: Verify staging server matches local build
        run: |
          set -euo pipefail
          LOCAL=$(sha256sum dist/index.html | awk '{print $1}')
          REMOTE=$(ssh -i ~/.ssh/staging_key -o StrictHostKeyChecking=no \
            ${{ secrets.STAGING_USER }}@${{ secrets.STAGING_HOST }} \
            "sha256sum /var/www/ark-library-staging/index.html | awk '{print \$1}'")
          echo "local:   $LOCAL"
          echo "staging: $REMOTE"
          if [ "$REMOTE" != "$LOCAL" ]; then
            echo "ERROR: staging 不是本次构建的版本"
            exit 1
          fi
          echo "✓ staging 已经更新到本次构建的版本。"

      - name: Cleanup SSH key
        if: always()
        run: rm -f ~/.ssh/staging_key
