name: Release

on:
  push:
    tags:
      - "v*"

permissions:
  contents: write

env:
  CARGO_TERM_COLOR: always

jobs:
  build-linux:
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4

      - name: Install system dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y \
            libwebkit2gtk-4.1-dev \
            libgtk-3-dev \
            libayatana-appindicator3-dev \
            librsvg2-dev \
            libssl-dev \
            build-essential \
            pkg-config \
            curl \
            wget \
            libfuse2

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: npm
          cache-dependency-path: v2/package-lock.json

      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Cache Rust dependencies
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            v2/src-tauri/target
          key: ${{ runner.os }}-cargo-${{ hashFiles('v2/src-tauri/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-

      - name: Install npm dependencies
        working-directory: v2
        run: npm ci --legacy-peer-deps

      - name: Build Tauri app
        working-directory: v2
        env:
          TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY }}
          TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY_PASSWORD }}
        run: npx tauri build

      - name: List build artifacts
        run: |
          find v2/src-tauri/target/release/bundle -type f \( -name "*.deb" -o -name "*.AppImage" -o -name "*.sig" \) | head -20

      - name: Generate updater latest.json
        run: |
          VERSION="${GITHUB_REF_NAME#v}"
          DEB_NAME=$(basename v2/src-tauri/target/release/bundle/deb/*.deb)
          APPIMAGE_NAME=$(basename v2/src-tauri/target/release/bundle/appimage/*.AppImage)
          SIG=""
          if [ -f "v2/src-tauri/target/release/bundle/appimage/${APPIMAGE_NAME}.sig" ]; then
            SIG=$(cat "v2/src-tauri/target/release/bundle/appimage/${APPIMAGE_NAME}.sig")
          fi
          cat > latest.json << EOF
          {
            "version": "${VERSION}",
            "notes": "Release ${VERSION}",
            "pub_date": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
            "platforms": {
              "linux-x86_64": {
                "signature": "${SIG}",
                "url": "https://github.com/agents-orchestrator/agents-orchestrator/releases/download/${GITHUB_REF_NAME}/${APPIMAGE_NAME}"
              }
            }
          }
          EOF

      - name: Upload .deb
        uses: actions/upload-artifact@v4
        with:
          name: agor-deb
          path: v2/src-tauri/target/release/bundle/deb/*.deb

      - name: Upload AppImage
        uses: actions/upload-artifact@v4
        with:
          name: agor-appimage
          path: v2/src-tauri/target/release/bundle/appimage/*.AppImage

      - name: Upload latest.json
        uses: actions/upload-artifact@v4
        with:
          name: updater-json
          path: latest.json

  release:
    needs: build-linux
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4

      - name: Download .deb
        uses: actions/download-artifact@v4
        with:
          name: agor-deb
          path: artifacts/

      - name: Download AppImage
        uses: actions/download-artifact@v4
        with:
          name: agor-appimage
          path: artifacts/

      - name: Download latest.json
        uses: actions/download-artifact@v4
        with:
          name: updater-json
          path: artifacts/

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          generate_release_notes: true
          files: |
            artifacts/*.deb
            artifacts/*.AppImage
            artifacts/latest.json
