name: CI
permissions:
  contents: read

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  ci:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6

      - uses: pnpm/action-setup@v4
        with:
          version: latest

      - uses: actions/setup-node@v4
        with:
          node-version: 24
          cache: pnpm

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

      - name: Format check
        run: pnpm format:check

      - name: Lint
        run: pnpm lint

      - name: Type check
        run: pnpm typecheck

      - name: Build
        run: pnpm build

      - name: Install Playwright browsers
        run: pnpm exec playwright install chromium --with-deps

      - name: Test
        id: test
        run: |
          start=$(date +%s)
          pnpm test
          echo "duration=$(( $(date +%s) - start ))" >> "$GITHUB_OUTPUT"

      - name: Record test suite duration
        if: always()
        run: |
          duration="${{ steps.test.outputs.duration }}"
          echo "## Test suite duration" >> "$GITHUB_STEP_SUMMARY"
          echo "" >> "$GITHUB_STEP_SUMMARY"
          echo "| Suite | Duration |" >> "$GITHUB_STEP_SUMMARY"
          echo "| ----- | -------- |" >> "$GITHUB_STEP_SUMMARY"
          echo "| unit + e2e | ${duration}s |" >> "$GITHUB_STEP_SUMMARY"
          if [ "${duration:-0}" -gt 300 ]; then
            echo "" >> "$GITHUB_STEP_SUMMARY"
            echo "> [!WARNING]" >> "$GITHUB_STEP_SUMMARY"
            echo "> Suite exceeded 300 s — possible performance regression." >> "$GITHUB_STEP_SUMMARY"
          fi
