name: Desktop Builds

on:
  pull_request:
    branches: [main, develop]
  workflow_dispatch:

concurrency:
  group: desktop-builds-${{ github.event_name }}-${{ github.ref }}
  cancel-in-progress: true

env:
  CARGO_TERM_COLOR: always
  APPIMAGE_EXTRACT_AND_RUN: 1

jobs:
  desktop:
    name: ${{ matrix.target }}
    runs-on: ${{ matrix.runs-on }}
    timeout-minutes: 60
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: macos-arm64
            runs-on: namespace-profile-mac-medium
            rust-target: aarch64-apple-darwin
          - target: macos-x64
            runs-on: namespace-profile-mac-medium
            rust-target: x86_64-apple-darwin
          - target: windows-x64
            runs-on: namespace-profile-windows-medium
            rust-target: x86_64-pc-windows-msvc
          - target: linux-x64
            runs-on: namespace-profile-linux-medium
            rust-target: x86_64-unknown-linux-gnu

    steps:
      - name: Enable Windows long paths
        if: runner.os == 'Windows'
        run: git config --system core.longpaths true

      - name: Checkout code
        uses: actions/checkout@v4
        with:
          submodules: false
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.rust-target }}

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version-file: .nvmrc

      - name: Cache Rust
        uses: Swatinem/rust-cache@v2
        with:
          workspaces: |
            desktop/src-tauri
          key: desktop-build-${{ matrix.rust-target }}

      - name: Install Linux desktop dependencies
        if: runner.os == 'Linux'
        run: |
          sudo apt-get update
          sudo apt-get install -y \
            build-essential \
            curl \
            desktop-file-utils \
            file \
            libfuse2 \
            libayatana-appindicator3-dev \
            librsvg2-dev \
            libsoup-3.0-dev \
            libssl-dev \
            libwebkit2gtk-4.1-dev \
            libgtk-3-dev \
            libxdo-dev \
            patchelf \
            pkg-config \
            rpm \
            squashfs-tools \
            wget \
            xdg-utils \
            zsync \
            appstream

      - name: Install desktop npm dependencies
        working-directory: desktop
        run: npm install
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Build desktop frontend
        working-directory: desktop
        run: npm run build

      - name: Build Tauri package
        shell: bash
        run: TAURI_TARGET="${{ matrix.rust-target }}" ./build.sh

      - name: List package outputs
        shell: bash
        run: find desktop/src-tauri/target -maxdepth 8 -path "*/release/bundle/*" -type f -print | sort

      - name: Upload desktop artifacts
        uses: actions/upload-artifact@v4
        with:
          name: rho-desktop-${{ matrix.target }}
          path: |
            desktop/src-tauri/target/**/release/bundle/**/*.dmg
            desktop/src-tauri/target/**/release/bundle/**/*.app.tar.gz
            desktop/src-tauri/target/**/release/bundle/**/*.sig
            desktop/src-tauri/target/**/release/bundle/**/*.msi
            desktop/src-tauri/target/**/release/bundle/**/*.exe
            desktop/src-tauri/target/**/release/bundle/**/*.AppImage
            desktop/src-tauri/target/**/release/bundle/**/*.deb
            desktop/src-tauri/target/**/release/bundle/**/*.rpm
