name: PHP Tests

on:
  push:
    branches: ['**']
  pull_request:

jobs:
  php-tests:
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres:18-alpine
        env:
          POSTGRES_DB: testing
          POSTGRES_USER: root
          POSTGRES_PASSWORD: ''
          POSTGRES_HOST_AUTH_METHOD: trust
        ports:
          - 5432:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    env:
      DB_CONNECTION: pgsql
      DB_HOST: postgres
      DB_PORT: 5432
      DB_DATABASE: testing
      DB_USERNAME: root
      DB_PASSWORD: ''
      CACHE_STORE: array
      CACHE_DRIVER: array
      SESSION_DRIVER: array
      QUEUE_CONNECTION: sync

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

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.5'
          extensions: mbstring, bcmath, intl, gd, zip, pdo, pdo_pgsql, redis, pcntl
          coverage: none

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: 22

      - name: Copy .env
        run: cp .env.example .env

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

      - name: Install npm dependencies
        run: npm ci

      - name: Build frontend assets
        run: npm run build

      - name: Generate app key
        run: php artisan key:generate

      - name: Install postgresql-client
        run: |
          apt-get update
          apt-get install -y postgresql-client

      - name: Wait for Postgres
        timeout-minutes: 1
        run: |
          until pg_isready -h postgres -p 5432 -U root; do
            echo "Waiting for postgres..."
            sleep 2
          done

      - name: Run migrations
        run: php artisan migrate --force

      - name: Run tests
        run: php artisan test --compact
