name: ERP-Deploy-Production

on:
  push:
    branches:
      - main

jobs:
  deploy-production:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v3
        with:
          repository: ${{ github.repository }}

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

      - name: Step 2 - Check if Rebuild Needed
        id: check_rebuild_prod
        uses: appleboy/ssh-action@master
        with:
          host: 220.132.7.82
          port: 2224
          username: root
          key: ${{ secrets.PROD_SSH_KEY }}
          script: |
            cd /var/www/star-erp

            if git diff --name-only HEAD~1 HEAD 2>/dev/null | grep -qE '(Dockerfile|compose\.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: 2224
          username: root
          key: ${{ secrets.PROD_SSH_KEY }}
          script: |
            cd /var/www/star-erp
            chown -R 1000:1000 .
            
            if git diff --name-only HEAD~1 HEAD 2>/dev/null | grep -qE '(Dockerfile|compose\.yaml|compose\.prod\.yaml|docker-compose\.yaml)'; then
              echo "🔄 偵測到 Docker 相關檔案變更，執行完整重建..."
              WWWGROUP=1000 WWWUSER=1000 docker compose -f compose.yaml -f compose.prod.yaml up -d --build --wait
            else
              echo "⚡ 無 Docker 檔案變更，僅重載服務..."
              if ! docker ps --format '{{.Names}}' | grep -q 'star-erp-laravel'; then
                echo "容器未運行，正在啟動..."
                WWWGROUP=1000 WWWUSER=1000 docker compose -f compose.yaml -f compose.prod.yaml up -d --wait
              else
                echo "容器已運行，跳過 docker compose，直接進行程式碼部署..."
              fi
            fi
            
            echo "容器狀態：" && docker ps --filter "name=star-erp-laravel"

            docker exec -u 1000:1000 -w /var/www/html star-erp-laravel sh -c "
              composer install --no-dev --optimize-autoloader &&
              npm install &&
              npm run build &&
              rm -f public/hot

              php artisan storage:link &&
              php artisan migrate --force &&
              php artisan tenants:migrate --force &&
              php artisan db:seed --force &&
              php artisan tenants:run db:seed --option=\"class=PermissionSeeder\" --option=\"force=true\" &&
              php artisan tenants:run db:seed --option=\"class=SystemSettingSeeder\" --option=\"force=true\" &&
              php artisan permission:cache-reset &&
              php artisan optimize:clear &&
              php artisan optimize &&
              php artisan view:cache
            "
            docker exec star-erp-laravel chmod -R 775 /var/www/html/storage /var/www/html/bootstrap/cache
