name: Build and lint web apps

on:
  workflow_dispatch:
  pull_request:
    branches:
      - "preview"
    types:
      - "opened"
      - "synchronize"
      - "ready_for_review"
      - "review_requested"
      - "reopened"

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  # Format check has no build dependencies - run immediately in parallel
  check-format:
    name: check:format
    runs-on: ubuntu-latest
    timeout-minutes: 10
    if: |
      github.event.pull_request.draft == false &&
      github.event.pull_request.requested_reviewers != null
    env:
      TURBO_SCM_BASE: ${{ github.event.pull_request.base.sha }}
      TURBO_SCM_HEAD: ${{ github.sha }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v6
        with:
          fetch-depth: 50
          filter: blob:none

      - name: Set up Node.js
        uses: actions/setup-node@v6

      - name: Enable Corepack and pnpm
        run: corepack enable pnpm

      - name: Get pnpm store directory
        shell: bash
        run: echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV

      - name: Cache pnpm store
        uses: actions/cache@v4
        with:
          path: ${{ env.STORE_PATH }}
          key: pnpm-store-${{ runner.os }}-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            pnpm-store-${{ runner.os }}-

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

      - name: Check formatting
        run: pnpm turbo run check:format --affected

  # Build packages - required for lint and type checks
  build:
    name: Build packages
    runs-on: ubuntu-latest
    timeout-minutes: 15
    if: |
      github.event.pull_request.draft == false &&
      github.event.pull_request.requested_reviewers != null
    env:
      TURBO_SCM_BASE: ${{ github.event.pull_request.base.sha }}
      TURBO_SCM_HEAD: ${{ github.sha }}
      NODE_OPTIONS: "--max-old-space-size=4096"
    steps:
      - name: Checkout code
        uses: actions/checkout@v6
        with:
          fetch-depth: 50
          filter: blob:none

      - name: Set up Node.js
        uses: actions/setup-node@v6

      - name: Enable Corepack and pnpm
        run: corepack enable pnpm

      - name: Get pnpm store directory
        shell: bash
        run: echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV

      - name: Cache pnpm store
        uses: actions/cache@v4
        with:
          path: ${{ env.STORE_PATH }}
          key: pnpm-store-${{ runner.os }}-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            pnpm-store-${{ runner.os }}-

      - name: Restore Turbo cache
        uses: actions/cache/restore@v4
        with:
          path: .turbo
          key: turbo-${{ runner.os }}-${{ github.event.pull_request.base.sha }}-${{ github.sha }}
          restore-keys: |
            turbo-${{ runner.os }}-${{ github.event.pull_request.base.sha }}-
            turbo-${{ runner.os }}-

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

      - name: Build packages
        run: pnpm turbo run build --affected

      - name: Save Turbo cache
        uses: actions/cache/save@v4
        with:
          path: .turbo
          key: turbo-${{ runner.os }}-${{ github.event.pull_request.base.sha }}-${{ github.sha }}

  # Lint and type checks depend on build artifacts
  check:
    name: ${{ matrix.task }}
    runs-on: ubuntu-latest
    needs: build
    timeout-minutes: 15
    strategy:
      fail-fast: false
      matrix:
        task: [check:lint, check:types]
    env:
      TURBO_SCM_BASE: ${{ github.event.pull_request.base.sha }}
      TURBO_SCM_HEAD: ${{ github.sha }}
      NODE_OPTIONS: "--max-old-space-size=4096"
    steps:
      - name: Checkout code
        uses: actions/checkout@v6
        with:
          fetch-depth: 50
          filter: blob:none

      - name: Set up Node.js
        uses: actions/setup-node@v6

      - name: Enable Corepack and pnpm
        run: corepack enable pnpm

      - name: Get pnpm store directory
        shell: bash
        run: echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV

      - name: Cache pnpm store
        uses: actions/cache@v4
        with:
          path: ${{ env.STORE_PATH }}
          key: pnpm-store-${{ runner.os }}-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            pnpm-store-${{ runner.os }}-

      - name: Restore Turbo cache
        uses: actions/cache/restore@v4
        with:
          path: .turbo
          key: turbo-${{ runner.os }}-${{ github.event.pull_request.base.sha }}-${{ github.sha }}

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

      - name: Run ${{ matrix.task }}
        run: pnpm turbo run ${{ matrix.task }} --affected
