name: CI - Tests

on:
  push:
    branches:
      - master
      - develop
  pull_request:
    branches:
      - master
      - develop

jobs:
  tests:
    runs-on: splendid-bear

    services:
      postgres:
        image: postgres:18-alpine
        env:
          POSTGRES_USER: mineseeker_test
          POSTGRES_PASSWORD: test_password
          POSTGRES_DB: mineseeker_test
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 5432:5432

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

      - name: Setup PHP 8.3
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.3'
          extensions: pdo_pgsql, gd, intl, zip, sodium
          coverage: none
          tools: composer:v2

      - name: Validate composer.json
        run: composer validate --strict

      - name: Cache Composer dependencies
        uses: actions/cache@v4
        with:
          path: vendor
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
            ${{ runner.os }}-composer-

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

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Cache node modules
        uses: actions/cache@v4
        with:
          path: node_modules
          key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node-

      - name: Install Node dependencies
        run: npm ci

      - name: Build assets
        run: npm run build

      - name: Create .env.test file
        run: |
          cat > .env.test << 'ENVEOF'
          APP_ENV=test
          APP_SECRET=test-secret-key-for-ci-testing-only
          DATABASE_URL="postgresql://mineseeker_test:test_password@localhost:5432/mineseeker_test?serverVersion=18&charset=utf8"
          KERNEL_CLASS='App\Kernel'
          SYMFONY_DEPRECATIONS_HELPER=999999
          PANTHER_APP_ENV=panther
          PANTHER_ERROR_SCREENSHOT_DIR=./var/error-screenshots
          ENVEOF

      - name: Setup test database
        run: make test-db-setup

      - name: Run PHPUnit tests
        run: vendor/bin/phpunit --testdox --colors=always

      - name: Run PHPUnit tests with coverage (optional)
        if: github.event_name == 'pull_request'
        run: |
          XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-text --coverage-clover=coverage.xml

      - name: Upload coverage reports (optional)
        if: github.event_name == 'pull_request'
        uses: codecov/codecov-action@v4
        with:
          file: ./coverage.xml
          flags: unittests
          name: phpunit-coverage
          fail_ci_if_error: false

  lint:
    runs-on: splendid-bear

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

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

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

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Install Node dependencies
        run: npm ci

      - name: Run ESLint
        run: npm run lint || true

      - name: Check code style (PHP)
        run: |
          if [ -f "vendor/bin/php-cs-fixer" ]; then
            vendor/bin/php-cs-fixer fix --dry-run --diff
          else
            echo "PHP-CS-Fixer not installed, skipping..."
          fi
