name: ui-regression

on:
  workflow_dispatch:

jobs:
  playwright-regression:
    runs-on: ubuntu-latest

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

      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          version: 10

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: pnpm
          cache-dependency-path: |
            frontend/pnpm-lock.yaml
            admin/pnpm-lock.yaml
            playwright-smoke/pnpm-lock.yaml

      - name: Install frontend deps
        working-directory: frontend
        run: pnpm install --frozen-lockfile

      - name: Install admin deps
        working-directory: admin
        run: pnpm install --frozen-lockfile

      - name: Install Playwright deps
        working-directory: playwright-smoke
        run: pnpm install --frozen-lockfile

      - name: Cache Playwright browsers
        id: playwright-cache
        uses: actions/cache@v4
        with:
          path: ~/.cache/ms-playwright
          key: ${{ runner.os }}-playwright-chromium-${{ hashFiles('playwright-smoke/pnpm-lock.yaml') }}

      - name: Install Playwright browsers
        if: steps.playwright-cache.outputs.cache-hit != 'true'
        working-directory: playwright-smoke
        run: pnpm exec playwright install --with-deps chromium

      - name: Typecheck Playwright suite
        working-directory: playwright-smoke
        run: pnpm exec tsc -p tsconfig.json --noEmit

      - name: Build frontend package
        working-directory: frontend
        env:
          PUBLIC_API_BASE_URL: http://127.0.0.1:5159/api
        run: pnpm build

      - name: Build admin package
        working-directory: admin
        env:
          VITE_API_BASE: http://127.0.0.1:5159
          VITE_FRONTEND_BASE_URL: http://127.0.0.1:4321
        run: pnpm build

      - name: Run frontend UI regression suite
        id: ui_frontend
        working-directory: playwright-smoke
        continue-on-error: true
        env:
          PLAYWRIGHT_USE_BUILT_APP: "1"
        run: pnpm test:frontend

      - name: Run admin UI regression suite
        id: ui_admin
        working-directory: playwright-smoke
        continue-on-error: true
        env:
          PLAYWRIGHT_USE_BUILT_APP: "1"
        run: pnpm test:admin

      - name: Mark workflow failed when any suite failed
        if: steps.ui_frontend.outcome != 'success' || steps.ui_admin.outcome != 'success'
        run: exit 1
