name: Build Tauri Clients

on:
  release:
    types: [published]
  workflow_dispatch:
    inputs:
      version:
        description: "Manual override version tag (optional)"
        required: false

jobs:
  compile:
    name: Compile
    strategy:
      matrix:
        os:
          - ubuntu-latest
          - ubuntu-arm64
          - macOS-latest
          - macOS-13
          - windows-latest
        include:
          - os: ubuntu-arm64
            runs-on: ubuntu-24.04-arm

    runs-on: ${{ matrix.runs-on || matrix.os }}

    env:
      DEPENDS_SETUP: ${{ startsWith(matrix.os, 'ubuntu-') && 'true' || 'false' }}

    steps:
      - name: Set Image Tag (Unix)
        if: matrix.os != 'windows-latest'
        run: echo "IMAGE_TAG=${{ github.event.release.tag_name || github.event.inputs.version || 'latest' }}" >> $GITHUB_ENV

      - name: Set Image Tag (Windows)
        if: matrix.os == 'windows-latest'
        run: echo "IMAGE_TAG=${{ github.event.release.tag_name || github.event.inputs.version || 'latest' }}" >> $Env:GITHUB_ENV
        shell: pwsh

      - name: Set environment variables
        run: |
          if [ "${{ matrix.os }}" = "ubuntu-latest" ]; then
            echo "ARTIFACT_NAME1=Pinepods_${{ env.IMAGE_TAG }}_amd64.deb" >> $GITHUB_ENV
            echo "ARTIFACT_NAME2=Pinepods_${{ env.IMAGE_TAG }}_amd64.AppImage" >> $GITHUB_ENV
            echo "ARTIFACT_NAME3=Pinepods-${{ env.IMAGE_TAG }}-1.x86_64.rpm" >> $GITHUB_ENV
          elif [ "${{ matrix.os }}" = "ubuntu-arm64" ]; then
            echo "ARTIFACT_NAME1=Pinepods_${{ env.IMAGE_TAG }}_arm64.deb" >> $GITHUB_ENV
            echo "ARTIFACT_NAME2=Pinepods_${{ env.IMAGE_TAG }}_aarch64.AppImage" >> $GITHUB_ENV
            echo "ARTIFACT_NAME3=Pinepods-${{ env.IMAGE_TAG }}-1.aarch64.rpm" >> $GITHUB_ENV
          # ... rest of conditions ...
          elif [ "${{ matrix.os }}" = "windows-latest" ]; then
            echo "ARTIFACT_NAME1=Pinepods_${{ env.IMAGE_TAG }}_x64-setup.exe" >> $Env:GITHUB_ENV
            echo "ARTIFACT_NAME2=Pinepods_${{ env.IMAGE_TAG }}_x64_en-US.msi" >> $Env:GITHUB_ENV
          elif [ "${{ matrix.os }}" = "macOS-latest" ]; then
            echo "ARTIFACT_NAME1=Pinepods_${{ env.IMAGE_TAG }}_aarch64.dmg" >> $GITHUB_ENV
            echo "ARTIFACT_NAME2=Pinepods.app" >> $GITHUB_ENV
          elif [ "${{ matrix.os }}" = "macOS-13" ]; then
            echo "ARTIFACT_NAME1=Pinepods_${{ env.IMAGE_TAG }}_x64.dmg" >> $GITHUB_ENV
            echo "ARTIFACT_NAME2=Pinepods.app" >> $GITHUB_ENV
          fi
        shell: bash
        if: ${{ matrix.os != 'windows-latest' }}

      - name: Set environment variables (Windows)
        run: |
          echo "ARTIFACT_NAME1=Pinepods_${{ env.IMAGE_TAG }}_x64-setup.exe" >> $Env:GITHUB_ENV
          echo "ARTIFACT_NAME2=Pinepods_${{ env.IMAGE_TAG }}_x64_en-US.msi" >> $Env:GITHUB_ENV
        shell: pwsh
        if: ${{ matrix.os == 'windows-latest' }}

      - name: Setup | Checkout
        uses: actions/checkout@v4

      - uses: hecrj/setup-rust-action@v2
        with:
          rust-version: 1.89
          targets: wasm32-unknown-unknown

      # Install cargo-binstall for Linux/Windows
      - name: Install cargo-binstall
        if: matrix.os != 'macos-latest' && matrix.os != 'macOS-13'
        uses: cargo-bins/cargo-binstall@main

      - name: Depends install
        if: ${{ env.DEPENDS_SETUP == 'true' }}
        run: |
          sudo apt update
          sudo apt install -qy libgtk-3-dev
          sudo apt-get install -y libwebkit2gtk-4.1-dev libappindicator3-dev librsvg2-dev patchelf

      - name: wasm-addition
        run: |
          rustup target add wasm32-unknown-unknown

      - name: Install Trunk (macOS)
        if: matrix.os == 'macos-latest' || matrix.os == 'macOS-13'
        run: |
          brew install trunk

      - name: Install Trunk (Linux/Windows)
        if: matrix.os != 'macos-latest' && matrix.os != 'macOS-13'
        run: |
          cargo binstall trunk -y

      - name: Install Tauri
        run: |
          cargo install tauri-cli@2.0.0-rc.15 --locked

      - name: Update Tauri version (UNIX)
        run: |
          cd web/src-tauri
          # Use different sed syntax for macOS
          if [[ "$OSTYPE" == "darwin"* ]]; then
            sed -i '' "s/\"version\": \".*\"/\"version\": \"${IMAGE_TAG}\"/" tauri.conf.json
          else
            sed -i "s/\"version\": \".*\"/\"version\": \"${IMAGE_TAG}\"/" tauri.conf.json
          fi
          cat tauri.conf.json
        shell: bash
        if: ${{ matrix.os != 'windows-latest' }}

      - name: Setup Python
        if: ${{ matrix.os == 'windows-latest' }}
        uses: actions/setup-python@v2
        with:
          python-version: "3.x"

      - name: Verify directory and update Tauri version (Windows)
        if: ${{ matrix.os == 'windows-latest' }}
        run: |
          cd web/src-tauri
          dir
          python .\change-version.py tauri.conf.json ${{ env.IMAGE_TAG }}
          Get-Content tauri.conf.json
        shell: pwsh

      - name: Build | Compile (UNIX)
        run: |
          cd web
          RUSTFLAGS="--cfg=web_sys_unstable_apis --cfg getrandom_backend=\"wasm_js\"" trunk build --features server_build
          cd src-tauri
          cat tauri.conf.json
          cargo tauri build
          pwd
          ls
          ls -la target/release/bundle
        shell: bash
        if: ${{ matrix.os != 'windows-latest' }}

      - name: Build | Compile (Windows)
        run: |
          cd web
          powershell -ExecutionPolicy Bypass -File .\build.ps1
          cd src-tauri
          Get-Content tauri.conf.json
          cargo tauri build
          ls target/release/bundle
        shell: pwsh
        if: ${{ matrix.os == 'windows-latest' }}

      # Ubuntu (x86_64) builds
      - name: Archive builds (Ubuntu)
        uses: actions/upload-artifact@v4
        with:
          name: ubuntu-latest-builds
          path: |
            ./web/src-tauri/target/release/bundle/deb/${{ env.ARTIFACT_NAME1 }}
            ./web/src-tauri/target/release/bundle/appimage/${{ env.ARTIFACT_NAME2 }}
            ./web/src-tauri/target/release/bundle/rpm/${{ env.ARTIFACT_NAME3 }}
        if: ${{ matrix.os == 'ubuntu-latest' }}

      # Ubuntu ARM64 builds
      - name: Archive builds (Ubuntu ARM)
        uses: actions/upload-artifact@v4
        with:
          name: ubuntu-arm64-builds
          path: |
            ./web/src-tauri/target/release/bundle/deb/${{ env.ARTIFACT_NAME1 }}
            ./web/src-tauri/target/release/bundle/appimage/${{ env.ARTIFACT_NAME2 }}
            ./web/src-tauri/target/release/bundle/rpm/${{ env.ARTIFACT_NAME3 }}
        if: ${{ matrix.os == 'ubuntu-arm64' }}

      # macOS builds - with distinct names
      - name: Archive build (macOS ARM)
        uses: actions/upload-artifact@v4
        with:
          name: macos-arm64-builds
          path: |
            ./web/src-tauri/target/release/bundle/dmg/${{ env.ARTIFACT_NAME1 }}
            ./web/src-tauri/target/release/bundle/macos/${{ env.ARTIFACT_NAME2 }}
        if: ${{ matrix.os == 'macOS-latest' }}

      - name: Archive build (macOS x64)
        uses: actions/upload-artifact@v4
        with:
          name: macos-x64-builds
          path: |
            ./web/src-tauri/target/release/bundle/dmg/${{ env.ARTIFACT_NAME1 }}
            ./web/src-tauri/target/release/bundle/macos/${{ env.ARTIFACT_NAME2 }}
        if: ${{ matrix.os == 'macOS-13' }}

      - name: Archive build (Windows)
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.os }}-build
          path: |
            ./web/src-tauri/target/release/bundle/nsis/${{ env.ARTIFACT_NAME1 }}
            ./web/src-tauri/target/release/bundle/msi/${{ env.ARTIFACT_NAME2 }}
        if: ${{ matrix.os == 'windows-latest' }}

      - name: Upload release asset (Ubuntu - DEB)
        if: github.event_name == 'release' && matrix.os == 'ubuntu-latest'
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: ./web/src-tauri/target/release/bundle/deb/${{ env.ARTIFACT_NAME1 }}
          asset_name: ${{ env.ARTIFACT_NAME1 }}
          asset_content_type: application/vnd.debian.binary-package

      - name: Upload release asset (Ubuntu ARM - DEB)
        if: github.event_name == 'release' && matrix.os == 'ubuntu-arm64'
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: ./web/src-tauri/target/release/bundle/deb/${{ env.ARTIFACT_NAME1 }}
          asset_name: ${{ env.ARTIFACT_NAME1 }}
          asset_content_type: application/vnd.debian.binary-package

      - name: Upload release asset (Ubuntu - AppImage)
        if: github.event_name == 'release' && matrix.os == 'ubuntu-latest'
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: ./web/src-tauri/target/release/bundle/appimage/${{ env.ARTIFACT_NAME2 }}
          asset_name: ${{ env.ARTIFACT_NAME2 }}
          asset_content_type: application/x-executable

      - name: Upload release asset (Ubuntu ARM - AppImage)
        if: github.event_name == 'release' && matrix.os == 'ubuntu-arm64'
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: ./web/src-tauri/target/release/bundle/appimage/${{ env.ARTIFACT_NAME2 }}
          asset_name: ${{ env.ARTIFACT_NAME2 }}
          asset_content_type: application/x-executable

      - name: Upload release asset (Ubuntu - RPM)
        if: github.event_name == 'release' && matrix.os == 'ubuntu-latest'
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: ./web/src-tauri/target/release/bundle/rpm/${{ env.ARTIFACT_NAME3 }}
          asset_name: ${{ env.ARTIFACT_NAME3 }}
          asset_content_type: application/x-rpm

      - name: Upload release asset (Ubuntu ARM - RPM)
        if: github.event_name == 'release' && matrix.os == 'ubuntu-arm64'
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: ./web/src-tauri/target/release/bundle/rpm/${{ env.ARTIFACT_NAME3 }}
          asset_name: ${{ env.ARTIFACT_NAME3 }}
          asset_content_type: application/x-rpm

      - name: Upload release asset (macOS - DMG)
        if: github.event_name == 'release' && (matrix.os == 'macOS-latest' || matrix.os == 'macOS-13')
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: ./web/src-tauri/target/release/bundle/dmg/${{ env.ARTIFACT_NAME1 }}
          asset_name: ${{ env.ARTIFACT_NAME1 }}
          asset_content_type: application/x-apple-diskimage

      - name: Upload release asset (Windows - EXE)
        if: github.event_name == 'release' && matrix.os == 'windows-latest'
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: ./web/src-tauri/target/release/bundle/nsis/${{ env.ARTIFACT_NAME1 }}
          asset_name: ${{ env.ARTIFACT_NAME1 }}
          asset_content_type: application/vnd.microsoft.portable-executable

      - name: Upload release asset (Windows - MSI)
        if: github.event_name == 'release' && matrix.os == 'windows-latest'
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: ./web/src-tauri/target/release/bundle/msi/${{ env.ARTIFACT_NAME2 }}
          asset_name: ${{ env.ARTIFACT_NAME2 }}
          asset_content_type: application/x-msi

  # release:
  #   needs: compile
  #   runs-on: ubuntu-latest
  #   steps:
  #     - name: Checkout code
  #       uses: actions/checkout@v2

  #     - name: Download artifacts
  #       uses: actions/download-artifact@v2
  #       with:
  #         name: ubuntu-latest-build
  #         path: artifacts/ubuntu-latest
  #     - name: Download artifacts
  #       uses: actions/download-artifact@v2
  #       with:
  #         name: macOS-latest-build
  #         path: artifacts/macOS-latest
  #     - name: Download artifacts
  #       uses: actions/download-artifact@v2
  #       with:
  #         name: windows-latest-build
  #         path: artifacts/windows-latest

  #     - name: Create Release
  #       id: create_release
  #       uses: actions/create-release@v1
  #       env:
  #         GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  #       with:
  #         tag_name: release-${{ github.run_id }}-beta
  #         release_name: Release-${{ github.run_id }}-beta
  #         draft: false
  #         prerelease: true

  #     - name: Upload Release Asset
  #       id: upload-release-asset-ubuntu
  #       uses: actions/upload-release-asset@v1
  #       env:
  #         GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  #       with:
  #         upload_url: ${{ steps.create_release.outputs.upload_url }}
  #         asset_path: ./artifacts/ubuntu-latest/PinePods
  #         asset_name: PinePods-ubuntu-latest
  #         asset_content_type: application/octet-stream

  #     - name: Upload Release Asset
  #       id: upload-release-asset-macos
  #       uses: actions/upload-release-asset@v1
  #       env:
  #         GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  #       with:
  #         upload_url: ${{ steps.create_release.outputs.upload_url }}
  #         asset_path: ./artifacts/macOS-latest/PinePods
  #         asset_name: PinePods-macOS-latest
  #         asset_content_type: application/octet-stream

  #     - name: Upload Release Asset
  #       id: upload-release-asset-windows
  #       uses: actions/upload-release-asset@v1
  #       env:
  #         GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  #       with:
  #         upload_url: ${{ steps.create_release.outputs.upload_url }}
  #         asset_path: ./artifacts/windows-latest/PinePods.exe
  #         asset_name: PinePods-windows-latest.exe
  #         asset_content_type: application/octet-stream
