name: tests

on:
  push:
    branches:
      - develop
      - main
  pull_request:
    branches:
      - develop
      - main

jobs:
  ci:
    runs-on: ubuntu-latest

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

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: 8.4
          tools: composer:v2
          coverage: xdebug

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

      - name: Install Node Dependencies
        run: npm ci

      - name: Install Dependencies
        run: composer install --no-interaction --prefer-dist --optimize-autoloader

      - name: Build Assets
        run: npm run build

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

      - name: Generate Application Key
        run: php artisan key:generate

      - name: Tests
        run: ./vendor/bin/phpunit

  ui:
    runs-on: ubuntu-latest
    env:
      APP_URL: http://127.0.0.1:8000
      E2E_GUEST_BASE_URL: http://127.0.0.1:8000
      E2E_GUEST_EVENT_SLUG: pwa-demo-event
      E2E_TENANT_EMAIL: hello@lumen-moments.demo
      E2E_TENANT_PASSWORD: Demo1234!

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

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: 8.4
          tools: composer:v2

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

      - name: Install Node Dependencies
        run: npm ci

      - name: Install Composer Dependencies
        run: composer install --no-interaction --prefer-dist --optimize-autoloader

      - name: Build Assets
        run: npm run build

      - name: Prepare Environment
        run: |
          cp .env.example .env
          php artisan key:generate
          php artisan migrate:fresh --seed --force
          php artisan tenant:add-dummy --email=$E2E_TENANT_EMAIL --password=$E2E_TENANT_PASSWORD --first_name=E2E --last_name=Tenant --address="CI Street 1" --phone="+491234567" --no-interaction

      - name: Start Laravel Server
        run: php artisan serve --host=127.0.0.1 --port=8000 > storage/logs/ci-server.log 2>&1 &

      - name: Wait for Server
        run: npx --yes wait-on@7 http://127.0.0.1:8000

      - name: Install Playwright Browsers
        run: npx playwright install --with-deps

      - name: Provision Guest Demo Event
        run: |
          curl --fail -X POST http://127.0.0.1:8000/api/_testing/guest-events \
            -H "Content-Type: application/json" \
            -d '{"slug":"pwa-demo-event","name":"CI Guest Demo"}'

      - name: UI Purchase Suite
        run: npm run test:ui:purchase -- --reporter=dot

      - name: UI Auth Suite
        run: npm run test:ui:auth -- --reporter=dot

      - name: UI Admin Suite
        run: npm run test:ui:admin -- --reporter=dot

      - name: UI Guest Suite
        run: npm run test:ui:guest -- --reporter=dot
