name: Publish PHP SDK

on:
  workflow_dispatch:
  push:
    branches:
      - main
    paths:
      - 'apps/php-sdk/**'

jobs:
  publish:
    name: Publish to Packagist
    runs-on: blacksmith-2vcpu-ubuntu-2404
    steps:
      - name: Checkout repository
        uses: actions/checkout@v5
        with:
          fetch-depth: 0

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.x'

      - name: Install version check dependencies
        run: pip install requests packaging

      - name: Run version check script
        id: version_check
        run: |
          VERSION_INCREMENTED=$(python .github/scripts/check_version_has_incremented.py php ./apps/php-sdk firecrawl/firecrawl-sdk)
          echo "VERSION_INCREMENTED=$VERSION_INCREMENTED" >> $GITHUB_ENV

      - name: Set up PHP
        if: ${{ env.VERSION_INCREMENTED == 'true' }}
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.3'
          tools: composer:v2

      - name: Install dependencies
        if: ${{ env.VERSION_INCREMENTED == 'true' }}
        working-directory: ./apps/php-sdk
        run: composer install --no-dev --prefer-dist --no-interaction

      - name: Validate composer.json
        if: ${{ env.VERSION_INCREMENTED == 'true' }}
        working-directory: ./apps/php-sdk
        run: composer validate --strict

      - name: Set up SSH for firecrawl-php
        if: ${{ env.VERSION_INCREMENTED == 'true' }}
        uses: webfactory/ssh-agent@v0.9.0
        with:
          ssh-private-key: ${{ secrets.PHP_SDK_DEPLOY_KEY }}

      - name: Subtree split and push to firecrawl-php
        if: ${{ env.VERSION_INCREMENTED == 'true' }}
        run: |
          git subtree split --prefix=apps/php-sdk -b php-sdk-split
          git remote add php-sdk git@github.com:firecrawl/firecrawl-php.git
          git push php-sdk php-sdk-split:main --force

      - name: Notify Packagist
        if: ${{ env.VERSION_INCREMENTED == 'true' }}
        run: |
          curl -sfS -X POST "https://packagist.org/api/update-package?username=${{ secrets.PACKAGIST_USERNAME }}&apiToken=${{ secrets.PACKAGIST_TOKEN }}" \
            -H "Content-Type: application/json" \
            -d '{"repository":{"url":"https://github.com/firecrawl/firecrawl-php"}}'
