name: Release Desktop Client

on:
  push:
    tags: ['v*']

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        include:
          - platform: macos-latest
            target: aarch64-apple-darwin
          - platform: macos-latest
            target: x86_64-apple-darwin
          - platform: windows-latest
            target: x86_64-pc-windows-msvc
          - platform: ubuntu-22.04
            target: x86_64-unknown-linux-gnu

    runs-on: ${{ matrix.platform }}
    steps:
      - uses: actions/checkout@v4

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

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

      - name: Install Linux dependencies
        if: matrix.platform == 'ubuntu-22.04'
        run: |
          sudo apt-get update
          sudo apt-get install -y libwebkit2gtk-4.1-dev build-essential curl wget file libssl-dev libgtk-3-dev libayatana-appindicator3-dev librsvg2-dev patchelf

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'

      - name: Install Python dependencies
        run: |
          pip install -e ".[server]" pyinstaller

      - name: Build Python sidecar
        shell: bash
        run: |
          mkdir -p src-tauri/binaries
          if [ "${{ matrix.platform }}" = "windows-latest" ]; then
            python -m PyInstaller --onefile \
              --name "agentkit-server-${{ matrix.target }}.exe" \
              --hidden-import agentkit.server \
              --hidden-import agentkit.server.app \
              --hidden-import agentkit.server.routes \
              --hidden-import agentkit.server.config \
              --hidden-import agentkit.cli.main \
              --hidden-import uvicorn.logging \
              --hidden-import uvicorn.lifespan.on \
              --hidden-import uvicorn.lifespan.off \
              --hidden-import uvicorn.protocols.websockets.auto \
              --hidden-import uvicorn.protocols.http.auto \
              --hidden-import sse_starlette \
              --distpath src-tauri/binaries \
              --workpath build/pyinstaller-work \
              --specpath build \
              src/agentkit/__main__.py
          else
            python -m PyInstaller --onefile \
              --name "agentkit-server-${{ matrix.target }}" \
              --hidden-import agentkit.server \
              --hidden-import agentkit.server.app \
              --hidden-import agentkit.server.routes \
              --hidden-import agentkit.server.config \
              --hidden-import agentkit.cli.main \
              --hidden-import uvicorn.logging \
              --hidden-import uvicorn.lifespan.on \
              --hidden-import uvicorn.lifespan.off \
              --hidden-import uvicorn.protocols.websockets.auto \
              --hidden-import uvicorn.protocols.http.auto \
              --hidden-import sse_starlette \
              --distpath src-tauri/binaries \
              --workpath build/pyinstaller-work \
              --specpath build \
              src/agentkit/__main__.py
          fi

      - name: Install frontend dependencies
        working-directory: src/agentkit/server/frontend
        run: npm ci

      - name: Install Rust dependencies
        working-directory: src-tauri
        run: cargo fetch

      - name: Build Tauri app
        uses: tauri-apps/tauri-action@v0
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tagName: ${{ github.ref_name }}
          releaseName: 'Fischer AgentKit ${{ github.ref_name }}'
          releaseBody: 'See the assets below to download and install.'
          releaseDraft: true
          prerelease: false
