name: Signed macOS Desktop

on:
  workflow_dispatch:
    inputs:
      tauri-target:
        description: "Rust/Tauri target triple"
        required: true
        default: "aarch64-apple-darwin"
        type: choice
        options:
          - aarch64-apple-darwin
          - x86_64-apple-darwin

concurrency:
  group: signed-desktop-${{ github.event_name }}-${{ inputs.tauri-target }}
  cancel-in-progress: false

env:
  CARGO_TERM_COLOR: always

jobs:
  signed-macos:
    name: signed-${{ inputs.tauri-target }}
    runs-on: namespace-profile-mac-medium
    timeout-minutes: 60

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

      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ inputs.tauri-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: signed-desktop-${{ inputs.tauri-target }}

      - name: Install desktop npm dependencies
        working-directory: desktop
        run: npm install

      - name: Build signed and notarized DMG
        shell: bash
        env:
          APPLE_ID: ${{ secrets.APPLE_ID }}
          APPLE_PASSWORD: ${{ secrets.APPLE_PASSWORD }}
          APPLE_TEAM_ID: ${{ secrets.APPLE_TEAM_ID }}
          APPLE_SIGNING_IDENTITY: ${{ secrets.APPLE_SIGNING_IDENTITY }}
          SIGNING_CERTIFICATE_P12_DATA: ${{ secrets.SIGNING_CERTIFICATE_P12_DATA }}
          SIGNING_CERTIFICATE_PASSWORD: ${{ secrets.SIGNING_CERTIFICATE_PASSWORD }}
          KEYCHAIN_PASSWORD: ${{ secrets.KEYCHAIN_PASSWORD }}
          TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY }}
          TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY_PASSWORD }}
          TAURI_TARGET: ${{ inputs.tauri-target }}
        run: ./build-signed.sh

      - name: Gatekeeper check
        shell: bash
        run: ./check-gatekeeper.sh

      - name: Upload desktop artifacts
        uses: actions/upload-artifact@v4
        with:
          name: rho-desktop-signed-${{ inputs.tauri-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
