name: PHP SDK Test Suite

on:
  pull_request:
    branches:
      - main
    paths:
      - apps/php-sdk/**
      - .github/workflows/test-php-sdk.yml
  push:
    branches:
      - main
    paths:
      - apps/php-sdk/**
  workflow_dispatch:

jobs:
  build-and-test:
    name: Build and Test
    runs-on: blacksmith-4vcpu-ubuntu-2404
    env:
      HAS_API_KEY: ${{ secrets.FIRECRAWL_API_KEY != '' }}
    if: >-
      github.event_name == 'workflow_dispatch' ||
      github.event_name == 'pull_request' ||
      (github.event_name == 'push' && github.ref == 'refs/heads/main')
    steps:
      - uses: actions/checkout@v5

      - name: Set up PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.3'
          tools: composer:v2
          coverage: none

      - name: Get Composer cache directory
        id: composer-cache
        working-directory: ./apps/php-sdk
        run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

      - name: Cache Composer dependencies
        uses: actions/cache@v4
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: ${{ runner.os }}-composer-${{ hashFiles('apps/php-sdk/composer.json') }}
          restore-keys: |
            ${{ runner.os }}-composer-

      - name: Install dependencies
        working-directory: ./apps/php-sdk
        run: composer install --prefer-dist --no-interaction

      - name: Validate composer.json
        working-directory: ./apps/php-sdk
        run: composer validate --strict

      - name: Run PHPStan
        working-directory: ./apps/php-sdk
        run: vendor/bin/phpstan analyse --no-progress

      - name: Run unit tests
        working-directory: ./apps/php-sdk
        run: vendor/bin/pest --ci

      - name: Run E2E tests
        if: env.HAS_API_KEY == 'true'
        working-directory: ./apps/php-sdk
        env:
          FIRECRAWL_API_KEY: ${{ secrets.FIRECRAWL_API_KEY }}
        run: vendor/bin/pest --ci --group=e2e
