name: Desktop Release (Tauri)

on:
  workflow_dispatch:
  push:
    tags:
      - 'desktop-v*'

permissions:
  contents: write

jobs:
  build:
    name: Build ${{ matrix.platform }}
    runs-on: ${{ matrix.runner }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - platform: linux
            runner: ubuntu-latest
          - platform: windows
            runner: windows-latest
          - platform: macos
            runner: macos-latest
    defaults:
      run:
        shell: bash
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: 20

      - name: Enable Corepack
        run: corepack enable && corepack prepare pnpm@10.20.0 --activate

      - name: Install Rust (stable)
        uses: dtolnay/rust-toolchain@stable

      - name: Install Linux deps
        if: matrix.platform == 'linux'
        run: |
          sudo apt-get update
          sudo apt-get install -y libwebkit2gtk-4.1-dev libayatana-appindicator3-dev librsvg2-dev libxdo-dev libssl-dev build-essential curl wget file

      - name: Install pnpm deps
        run: pnpm -C apps/desktop install --frozen-lockfile


      - name: Build desktop
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          TAURI_PRIVATE_KEY: ${{ secrets.TAURI_PRIVATE_KEY }}
          TAURI_KEY_PASSWORD: ${{ secrets.TAURI_KEY_PASSWORD }}
          VITE_APP_URL: https://tickets.esdrasrenan.com.br
          VITE_API_BASE_URL: https://tickets.esdrasrenan.com.br
        run: pnpm -C apps/desktop tauri build

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: desktop-${{ matrix.platform }}
          path: apps/desktop/src-tauri/target/release/bundle
