name: End-to-End Tests

on:
  push:
    branches: [ main ]

jobs:
  e2e-tests:
    runs-on: ubuntu-latest
    name: End-to-End Tests

    services:
      firestore:
        image: google/cloud-firestore-emulator
        ports:
          - 8080:8080
        options: >-
          --health-cmd="curl -f http://localhost:8080 || exit 1"
          --health-interval=10s
          --health-timeout=5s
          --health-retries=5

    steps:
      - uses: actions/checkout@v4

      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          run_install: false
          package_json_path: package.json

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

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Build frontend bundle
        run: pnpm --filter=@my-roadtrip/web build

      - name: Run E2E Tests
        run: pnpm test:e2e --reporter=line
        env:
          FIRESTORE_EMULATOR_HOST: localhost:8080
          NODE_ENV: test
          PLAYWRIGHT_HTML_REPORT: playwright-report

      - name: Upload Playwright report
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: playwright-report
          path: playwright-report

      - name: Publish summary
        if: always()
        run: |
          if [ -f playwright-report/index.html ]; then
            echo "## ✅ Rapport Playwright disponible" >> $GITHUB_STEP_SUMMARY
            echo "Rapport exporté dans l'artifact \`playwright-report\`." >> $GITHUB_STEP_SUMMARY
          else
            echo "## ⚠️ Rapport Playwright indisponible" >> $GITHUB_STEP_SUMMARY
          fi

  e2e-performance:
    runs-on: ubuntu-latest
    name: E2E Performance Tests

    steps:
      - uses: actions/checkout@v4

      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          run_install: false
          package_json_path: package.json

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

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Build frontend bundle
        run: pnpm --filter=@my-roadtrip/web build

      - name: Run chromium-only regression sweep
        run: pnpm test:e2e --project=chromium --workers=1 --reporter=line
        env:
          NODE_ENV: performance-test

      - name: Capture execution metrics
        run: |
          echo "## Performance run" >> $GITHUB_STEP_SUMMARY
          echo "- Tests exécutés sur Chromium en mode série" >> $GITHUB_STEP_SUMMARY
          echo "- Rapport HTML disponible dans le job e2e principal" >> $GITHUB_STEP_SUMMARY
        if: always()

  report-summary:
    runs-on: ubuntu-latest
    name: Test Summary
    needs: [ e2e-tests ]
    if: always()

    steps:
      - name: Download Playwright report
        uses: actions/download-artifact@v4
        with:
          name: playwright-report
        continue-on-error: true

      - name: Generate Summary
        run: |
          echo "# Synthèse tests E2E" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          if [ -f playwright-report/index.html ]; then
            echo "✅ Rapport Playwright disponible dans l'artifact \`playwright-report\`." >> $GITHUB_STEP_SUMMARY
          else
            echo "⚠️ Aucun rapport Playwright n'a été généré." >> $GITHUB_STEP_SUMMARY
          fi
