name: E2E Tests

on:
  pull_request:
    branches: [main]

# Minimal permissions for security
permissions:
  contents: read

jobs:
  changes:
    name: Detect E2E relevance
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: read
    outputs:
      e2e_relevant: ${{ steps.filter.outputs.e2e_relevant }}
    steps:
      - uses: dorny/paths-filter@v3
        id: filter
        with:
          filters: |
            e2e_relevant:
              - 'frontend/**'
              - 'backend/**'

  e2e:
    name: Playwright E2E
    needs: changes
    if: needs.changes.outputs.e2e_relevant == 'true'
    runs-on: ubuntu-latest
    timeout-minutes: 15
    permissions:
      contents: read

    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version: '22'
          cache: 'npm'
          cache-dependency-path: |
            backend/package-lock.json
            frontend/package-lock.json

      - name: Install backend dependencies
        working-directory: backend
        run: npm ci

      - name: Install frontend dependencies
        working-directory: frontend
        run: npm ci

      - name: Install Playwright browsers
        working-directory: frontend
        run: npx playwright install --with-deps chromium

      - name: Run E2E tests (Chromium only)
        working-directory: frontend
        run: npx playwright test --project=chromium
        env:
          CI: true
          PLAYWRIGHT_WORKERS: 1
          PLAYWRIGHT_HTML_OPEN: never
          JWT_SECRET: e2e-test-secret-that-is-long-enough
          SESSION_SECRET: e2e-test-session-secret-long-enough

      - name: Upload Playwright report
        uses: actions/upload-artifact@v7
        if: always()
        with:
          name: playwright-report
          path: frontend/playwright-report/
          retention-days: 7

      - name: Upload test results
        uses: actions/upload-artifact@v7
        if: always()
        with:
          name: playwright-results
          path: frontend/test-results/
          retention-days: 7
