name: star-cloud-deploy-demo

on:
  push:
    branches:
      - demo

jobs:
  deploy-demo:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v3
        with:
          github-server-url: https://gitea.taiwan-star.com.tw
          repository: ${{ github.repository }}

      - name: Step 1 - Push Code to Demo
        run: |
          apt-get update && apt-get install -y rsync openssh-client
          mkdir -p ~/.ssh
          echo "${{ secrets.DEMO_SSH_KEY }}" > ~/.ssh/id_rsa_demo
          chmod 600 ~/.ssh/id_rsa_demo
          rsync -avz --delete \
            --exclude='/.git' \
            --exclude='/node_modules' \
            --exclude='/vendor' \
            --exclude='/storage' \
            --exclude='/.env' \
            --exclude='/public/build' \
            -e "ssh -p 2227 -i ~/.ssh/id_rsa_demo -o StrictHostKeyChecking=no" \
            ./ root@220.132.7.82:/var/www/star-cloud-demo/
          rm ~/.ssh/id_rsa_demo

      - name: Step 2 - Check if Rebuild Needed
        id: check_rebuild
        uses: appleboy/ssh-action@master
        with:
          host: 220.132.7.82
          port: 2227
          username: root
          key: ${{ secrets.DEMO_SSH_KEY }}
          script: |
            cd /var/www/star-cloud-demo
            if git diff --name-only HEAD~1 HEAD 2>/dev/null | grep -qE '(Dockerfile|compose\.yaml|compose\.demo\.yaml|docker-compose\.yaml)'; then
              echo "REBUILD_NEEDED=true"
            else
              echo "REBUILD_NEEDED=false"
            fi

      - name: Step 3 - Container Up & Health Check
        uses: appleboy/ssh-action@master
        with:
          host: 220.132.7.82
          port: 2227
          username: root
          key: ${{ secrets.DEMO_SSH_KEY }}
          script: |
            cd /var/www/star-cloud-demo
            chown -R 1000:1000 .
            
            if git diff --name-only HEAD~1 HEAD 2>/dev/null | grep -qE '(Dockerfile|compose\.yaml|compose\.demo\.yaml|docker-compose\.yaml)'; then
              echo "🔄 偵測到 Docker 相關檔案變更，執行完整重建..."
              WWWGROUP=1000 WWWUSER=1000 docker compose -f compose.yaml -f compose.demo.yaml up -d --build --wait
            else
              echo "⚡ 無 Docker 檔案變更，僅重載服務..."
              if ! docker ps --format '{{.Names}}' | grep -q 'star-cloud-demo-laravel'; then
                echo "容器未運行，正在啟動..."
                WWWGROUP=1000 WWWUSER=1000 docker compose -f compose.yaml -f compose.demo.yaml up -d --wait
              else
                echo "容器已運行，跳過 docker compose，直接進行程式碼部署..."
              fi
            fi
            
            echo "容器狀態：" && docker ps --filter "name=star-cloud-demo"

      - name: Step 4 - Composer & NPM Build
        uses: appleboy/ssh-action@master
        with:
          host: 220.132.7.82
          port: 2227
          username: root
          key: ${{ secrets.DEMO_SSH_KEY }}
          script: |
           docker exec -u 1000:1000 -w /var/www/html star-cloud-demo-laravel sh -c "
            # 1. 後端依賴
            composer install --no-dev --optimize-autoloader --no-interaction &&
            
            # 2. 前端編譯
            npm install &&
            npm run build &&
            
            # 3. Laravel 初始化與優化
            php artisan migrate --force &&
            php artisan storage:link &&
            php artisan optimize:clear &&
            php artisan optimize &&
            php artisan view:cache &&
            php artisan queue:restart &&
            php artisan db:seed --class=RoleSeeder --force &&
            php artisan db:seed --class=AdminUserSeeder --force
           "
           docker exec star-cloud-demo-laravel chmod -R 775 /var/www/html/storage /var/www/html/bootstrap/cache

      - name: Step 5 - Auto Sync workflows to main
        run: |
          git config --global user.email "bot@taiwan-star.com.tw"
          git config --global user.name "CICD Bot"
          git fetch origin main
          git checkout main
          git checkout ${{ github.ref_name }} -- .gitea/workflows/
          if ! git diff --cached --quiet; then
            git commit -m "[AUTO] Sync workflows from ${{ github.ref_name }} to main"
            GIT_SSH_COMMAND="ssh -p 3222 -o StrictHostKeyChecking=no" git push origin main
          fi
          git checkout ${{ github.ref_name }}

