name: "CI and E2E Tests"

on:
  push:
  deployment_status:
    types: [success]

jobs:
  ci:
    if: github.event_name == 'push'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
      - name: Install dependencies
        run: npm i
      - name: Install CI checks
        run: npm ci
      - name: Check types
        run: npm run typecheck
      - name: Lint with Biome
        run: npx @biomejs/biome check src/.
      - name: Lint styles
        run: npx stylelint **/*.{scss,css}
      - name: Test production build
        run: npm run build

  e2e_tests:
    needs: ci
    runs-on: ubuntu-latest
    steps:
      - name: Debug event info
        run: |
          echo "Event Name: ${{ github.event_name }}"
          echo "Deployment Status: ${{ github.event.deployment_status.state }}"
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: '18'
      - name: Install dependencies
        run: npm install
      - name: Wait for deployment to be live
        run: |
          echo "Waiting for Vercel deployment to be live..."
          until curl -sSf https://testing.discours.io > /dev/null; do
            printf '.'
            sleep 10
          done
      - name: Install Playwright and dependencies
        run: npm run e2e:install
      - name: Run Playwright tests
        run: npm run e2e:tests:ci
        env:
          BASE_URL: https://testing.discours.io
        continue-on-error: true
      - name: Report test result if failed
        if: failure()
        run: echo "E2E tests failed. Please review the logs."
