name: Release Desktop Apps

on:
  push:
    tags:
      - 'desktop-v*'  # Trigger on tags like desktop-v1.0.0
  workflow_dispatch:
    inputs:
      version:
        description: 'Version number (e.g., 1.0.0)'
        required: true

jobs:
  create-release:
    runs-on: ubuntu-latest
    outputs:
      upload_url: ${{ steps.create_release.outputs.upload_url }}
      version: ${{ steps.get_version.outputs.version }}
    steps:
      - name: Get version
        id: get_version
        run: |
          if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
            echo "version=${{ github.event.inputs.version }}" >> $GITHUB_OUTPUT
          else
            echo "version=${GITHUB_REF#refs/tags/desktop-v}" >> $GITHUB_OUTPUT
          fi

      - name: Create Release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: desktop-v${{ steps.get_version.outputs.version }}
          release_name: AeThex OS Desktop v${{ steps.get_version.outputs.version }}
          draft: false
          prerelease: false
          body: |
            # AeThex OS Desktop v${{ steps.get_version.outputs.version }}

            ## Downloads
            - **Windows**: Download the `.msi` installer
            - **macOS**: Download the `.dmg` file
            - **Linux**: Download the `.AppImage`, `.deb`, or `.rpm` file

            ## What's New
            - Desktop application release
            - Cross-platform support (Windows, macOS, Linux)
            - Native performance with Tauri

            ## Installation
            - **Windows**: Run the MSI installer
            - **macOS**: Open the DMG and drag to Applications
            - **Linux**: Make AppImage executable with `chmod +x` and run, or install DEB/RPM

  build-desktop:
    needs: create-release
    strategy:
      fail-fast: false
      matrix:
        include:
          - platform: 'macos-latest'
            args: '--target universal-apple-darwin'
          - platform: 'ubuntu-22.04'
            args: ''
          - platform: 'windows-latest'
            args: ''

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

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

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

      - name: Install dependencies (Ubuntu only)
        if: matrix.platform == 'ubuntu-22.04'
        run: |
          sudo apt-get update
          sudo apt-get install -y libwebkit2gtk-4.0-dev libwebkit2gtk-4.1-dev \
            libappindicator3-dev librsvg2-dev patchelf

      - name: Install frontend dependencies
        run: npm install

      - name: Install Tauri dependencies
        working-directory: shell/aethex-shell
        run: npm install

      - name: Build Tauri app
        working-directory: shell/aethex-shell
        run: npm run tauri build -- ${{ matrix.args }}

      - name: Upload Windows MSI
        if: matrix.platform == 'windows-latest'
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.create-release.outputs.upload_url }}
          asset_path: ./shell/aethex-shell/src-tauri/target/release/bundle/msi/aethex-os_${{ needs.create-release.outputs.version }}_x64_en-US.msi
          asset_name: AeThex-OS-${{ needs.create-release.outputs.version }}-Windows-x64.msi
          asset_content_type: application/x-msi

      - name: Upload macOS DMG
        if: matrix.platform == 'macos-latest'
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.create-release.outputs.upload_url }}
          asset_path: ./shell/aethex-shell/src-tauri/target/universal-apple-darwin/release/bundle/dmg/AeThex OS_${{ needs.create-release.outputs.version }}_universal.dmg
          asset_name: AeThex-OS-${{ needs.create-release.outputs.version }}-macOS-universal.dmg
          asset_content_type: application/x-apple-diskimage

      - name: Upload Linux AppImage
        if: matrix.platform == 'ubuntu-22.04'
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.create-release.outputs.upload_url }}
          asset_path: ./shell/aethex-shell/src-tauri/target/release/bundle/appimage/aethex-os_${{ needs.create-release.outputs.version }}_amd64.AppImage
          asset_name: AeThex-OS-${{ needs.create-release.outputs.version }}-Linux-x86_64.AppImage
          asset_content_type: application/x-executable

      - name: Upload Linux DEB
        if: matrix.platform == 'ubuntu-22.04'
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.create-release.outputs.upload_url }}
          asset_path: ./shell/aethex-shell/src-tauri/target/release/bundle/deb/aethex-os_${{ needs.create-release.outputs.version }}_amd64.deb
          asset_name: AeThex-OS-${{ needs.create-release.outputs.version }}-Linux-amd64.deb
          asset_content_type: application/vnd.debian.binary-package
