name: Build Release ZIP

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: write

jobs:
  # Ensure CI passes before releasing
  ci:
    uses: ./.github/workflows/ci.yml

  release:
    name: Create Release
    runs-on: ubuntu-latest
    needs: ci

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

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.1'
          tools: composer:v2

      - name: Install dependencies (production only)
        run: |
          composer install --no-dev --optimize-autoloader --no-interaction
          # Fix platform_check.php to not enforce build machine's PHP version
          if [ -f vendor/composer/platform_check.php ]; then
            sed -i 's/PHP_VERSION_ID >= 80100/PHP_VERSION_ID >= 70400/g' vendor/composer/platform_check.php
            sed -i 's/">= 8.1.0"/">= 7.4.0"/g' vendor/composer/platform_check.php
          fi

      - name: Build plugin ZIP
        run: |
          VERSION=${GITHUB_REF#refs/tags/v}
          PLUGIN_SLUG="wp-open-claw"

          # Create a clean directory for the ZIP
          mkdir -p build/$PLUGIN_SLUG

          # Copy only plugin files (exclude dev/docs files)
          rsync -a \
            --exclude='.git' \
            --exclude='.github' \
            --exclude='.gitignore' \
            --exclude='docs' \
            --exclude='node_modules' \
            --exclude='tests' \
            --exclude='*.zip' \
            --exclude='.idea' \
            --exclude='.vscode' \
            --exclude='composer.lock' \
            ./ build/$PLUGIN_SLUG/

          # Create ZIP
          cd build
          zip -r ../$PLUGIN_SLUG-$VERSION.zip $PLUGIN_SLUG/

          echo "📦 Release ZIP: $PLUGIN_SLUG-$VERSION.zip ($(du -h ../$PLUGIN_SLUG-$VERSION.zip | cut -f1))"

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          files: wp-open-claw-*.zip
          generate_release_notes: true
          draft: false
