name: Desktop Build

on:
  workflow_dispatch:

permissions:
  contents: write

jobs:
  build_desktop:
    runs-on: ${{ matrix.os }}
    permissions:
      contents: write
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    env:
      GH_TOKEN: ${{ secrets.GH_TOKEN || github.token }}
      GITHUB_TOKEN: ${{ secrets.GH_TOKEN || github.token }}
    defaults:
      run:
        shell: bash
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Bun
        uses: oven-sh/setup-bun@v1
        with:
          bun-version: latest

      - name: Install dependencies
        run: bun install

      - name: Sync version from tag
        run: |
          if [[ "${{ github.ref }}" == refs/tags/v* ]]; then
            VERSION=${GITHUB_REF#refs/tags/}
          else
            VERSION="v$(cat package.json | grep '\"version\"' | head -1 | sed 's/.*\"version\": \"\(.*\)\".*/\1/')"
          fi
          echo "Syncing version to ${VERSION}"
          bun run bump ${VERSION}

      - name: Build Desktop
        run: bun run build:electron

      - name: Upload Desktop Artifacts
        uses: actions/upload-artifact@v4
        with:
          name: desktop-${{ matrix.os }}
          path: release/mtranserver-desktop-*
