name: CI

on:
  pull_request:
    branches: [main]

jobs:
  test:
    runs-on: ubuntu-22.04

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

      - name: Set up Bun
        uses: oven-sh/setup-bun@v2
        with:
          bun-version: 1.3.10

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

      - name: Install frontend dependencies
        run: cd frontend && bun install --frozen-lockfile

      - name: Lint
        run: bun run lint

      - name: Type check
        run: bun run build

      - name: Run tests
        run: bun test

      - name: Install Playwright Chromium
        run: cd frontend && bunx playwright install --with-deps chromium

      - name: Print visual stack versions
        run: cd frontend && bun --version && bunx playwright --version && bunx playwright install --list

      - name: Install deterministic CJK fonts for visual snapshots
        run: sudo apt-get update && sudo apt-get install -y fonts-noto-cjk fonts-noto-color-emoji fonts-liberation

      - name: Run visual regression
        run: bun run ui:visual

      - name: Upload Playwright artifacts on failure
        if: failure()
        uses: actions/upload-artifact@v4
        with:
          name: playwright-artifacts
          path: |
            frontend/playwright-report/
            frontend/test-results/
