name: CI

on:
  push:
    branches: [main, develop]
    paths:
      - 'native/wordpress/maple-fonts-wp/**'
  pull_request:
    branches: [main, develop]
    paths:
      - 'native/wordpress/maple-fonts-wp/**'

defaults:
  run:
    working-directory: native/wordpress/maple-fonts-wp

jobs:
  phpcs:
    name: PHP Coding Standards
    runs-on: ubuntu-latest

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

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.2'
          tools: composer, cs2pr
          coverage: none

      - name: Get Composer cache directory
        id: composer-cache
        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('**/composer.lock') }}
          restore-keys: ${{ runner.os }}-composer-

      - name: Install dependencies
        run: composer install --prefer-dist --no-progress

      - name: Run PHP_CodeSniffer
        run: ./vendor/bin/phpcs --report=checkstyle | cs2pr
        continue-on-error: true

  phpcompat:
    name: PHP Compatibility
    runs-on: ubuntu-latest

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

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

      - name: Get Composer cache directory
        id: composer-cache
        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('**/composer.lock') }}
          restore-keys: ${{ runner.os }}-composer-

      - name: Install dependencies
        run: composer install --prefer-dist --no-progress

      - name: Run PHPCompatibility
        run: ./vendor/bin/phpcs -p --standard=PHPCompatibilityWP --runtime-set testVersion 7.4- --extensions=php --ignore=vendor,tests .

  test:
    name: PHP ${{ matrix.php }} / WP ${{ matrix.wordpress }}
    runs-on: ubuntu-latest
    needs: [phpcs, phpcompat]

    strategy:
      fail-fast: false
      matrix:
        php: ['7.4', '8.0', '8.1', '8.2', '8.3']
        wordpress: ['6.5', '6.6', 'latest']
        exclude:
          # PHP 8.3 not fully compatible with older WP versions
          - php: '8.3'
            wordpress: '6.5'

    services:
      mysql:
        image: mysql:8.0
        env:
          MYSQL_ROOT_PASSWORD: root
          MYSQL_DATABASE: wordpress_test
        ports:
          - 3306:3306
        options: >-
          --health-cmd="mysqladmin ping"
          --health-interval=10s
          --health-timeout=5s
          --health-retries=5

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

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, pdo_mysql, mysqli, gd, exif, intl
          tools: composer
          coverage: none

      - name: Get Composer cache directory
        id: composer-cache
        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 }}-php${{ matrix.php }}-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: ${{ runner.os }}-php${{ matrix.php }}-composer-

      - name: Install dependencies
        run: composer install --prefer-dist --no-progress

      - name: Install WordPress test suite
        run: bash bin/install-wp-tests.sh wordpress_test root root 127.0.0.1 ${{ matrix.wordpress }} true

      - name: Run PHPUnit
        run: ./vendor/bin/phpunit --configuration phpunit.xml.dist

  build:
    name: Build Plugin
    runs-on: ubuntu-latest
    needs: test

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

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

      - name: Install dependencies (production)
        run: composer install --prefer-dist --no-dev --optimize-autoloader --no-progress

      - name: Create build directory
        run: mkdir -p build

      - name: Build plugin zip
        run: |
          zip -r build/maple-local-fonts.zip . \
            -x ".git/*" \
            -x ".github/*" \
            -x "tests/*" \
            -x "bin/*" \
            -x "build/*" \
            -x "vendor/bin/*" \
            -x "*.xml" \
            -x "*.xml.dist" \
            -x "composer.*" \
            -x "phpcs.xml*" \
            -x ".editorconfig" \
            -x ".gitignore" \
            -x "CLAUDE.md" \
            -x "SECURITY.md" \
            -x "GOOGLE_FONTS_API.md" \
            -x "WORDPRESS_COMPATIBILITY.md"

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: maple-local-fonts
          path: native/wordpress/maple-fonts-wp/build/maple-local-fonts.zip
          retention-days: 30
