name: Release Desktop

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: write

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        include:
          # macOS — universal build (arm64 + x64) on Apple Silicon runner
          - os: macos-latest
            args: --mac --arm64 --x64
          # Windows — x64 + arm64 on x64 runner
          - os: windows-latest
            args: --win --x64 --arm64
          # Linux — x64 on x64 runner
          - os: ubuntu-latest
            args: --linux --x64

    runs-on: ${{ matrix.os }}

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

      - name: Install Linux build dependencies
        if: runner.os == 'Linux'
        run: |
          sudo apt-get update
          sudo apt-get install -y libx11-dev libxtst-dev libxt-dev libxkbcommon-dev

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

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: pnpm

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

      - name: Build shared package
        run: pnpm --filter @backspace/shared build

      - name: Compile desktop TypeScript
        working-directory: packages/desktop
        run: pnpm exec tsc

      - name: Build and publish desktop app
        working-directory: packages/desktop
        run: pnpm exec electron-builder ${{ matrix.args }} --publish always
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
