name: UI Frontend

on:
  pull_request:
    paths:
      - "client/ui/frontend/**"
      - "client/ui/i18n/**"
      - "client/ui/**/*.go"
      - ".github/workflows/frontend-ui.yml"
  push:
    branches:
      - main
    paths:
      - "client/ui/frontend/**"
      - "client/ui/i18n/**"
      - "client/ui/**/*.go"

permissions:
  contents: read

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

jobs:
  lint-and-build:
    name: Lint & Build
    runs-on: ubuntu-latest
    timeout-minutes: 15
    defaults:
      run:
        working-directory: client/ui/frontend
    steps:
      - name: Checkout repository
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "22"

      - name: Set up pnpm
        uses: pnpm/action-setup@a3252b78c470c02df07e9d59298aecedc3ccdd6d # v3.0.0
        with:
          version: 11

      # Bindings are generated by wails3 from the Go service definitions and
      # are not checked in (see client/ui/frontend/bindings/). Without them,
      # typecheck/build fail on missing module imports.
      - name: Set up Go
        uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 # v6.3.0
        with:
          go-version-file: "go.mod"
          cache: false

      # wails3 CLI links against GTK4 / WebKitGTK 6.0 via its internal/operatingsystem
      # package, so the dev libraries must be present before `go install`.
      - name: Install Wails Linux system dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y --no-install-recommends \
            pkg-config \
            libgtk-4-dev \
            libwebkitgtk-6.0-dev

      - name: Install wails3 CLI
        # Version derived from go.mod so the binding generator always matches
        # the wails runtime the daemon links against.
        working-directory: ${{ github.workspace }}
        run: |
          WAILS_VERSION=$(go list -m -f '{{.Version}}' github.com/wailsapp/wails/v3)
          go install github.com/wailsapp/wails/v3/cmd/wails3@$WAILS_VERSION

      - name: Get pnpm store directory
        id: pnpm-store
        run: echo "path=$(pnpm store path --silent)" >> "$GITHUB_OUTPUT"

      - name: Cache pnpm store
        uses: actions/cache@v4
        with:
          path: ${{ steps.pnpm-store.outputs.path }}
          key: ${{ runner.os }}-pnpm-${{ hashFiles('client/ui/frontend/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-

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

      - name: Generate Wails bindings
        run: pnpm run bindings

      - name: Lint, typecheck, format
        run: pnpm check

      - name: Build
        run: pnpm build
