name: "experimental"
on:
  workflow_dispatch:

jobs:
  build-tauri:
    strategy:
      fail-fast: false
      matrix:
        include:
          - platform: "macos-latest"
            args: "--target aarch64-apple-darwin"
            artifact-name: macos-aarch64
          - platform: "macos-latest"
            args: "--target x86_64-apple-darwin"
            artifact-name: macos-x86_64
          - platform: "ubuntu-22.04"
            args: ""
            artifact-name: linux
          - platform: "windows-latest"
            args: ""
            artifact-name: windows

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

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

      - name: setup pnpm
        uses: pnpm/action-setup@v4
        with:
          version: 10
      - name: setup node
        uses: actions/setup-node@v4
        with:
          node-version: lts/*
          cache: "pnpm"

      - name: install Rust stable
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.platform == 'macos-latest' && 'aarch64-apple-darwin,x86_64-apple-darwin' || '' }}

      - name: Rust cache
        uses: swatinem/rust-cache@v2
        with:
          workspaces: "./src-tauri -> target"

      - name: install frontend dependencies
        run: pnpm install

      - name: build Tauri app
        run: pnpm run tauri:build -- ${{ matrix.args }}
        env:
          TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY }}
          TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY_PASSWORD }}

      - name: Create PKG with post-install script (macOS only)
        if: matrix.platform == 'macos-latest'
        run: |
          mkdir -p pkg_temp/scripts
          cp scripts/postinstall.sh pkg_temp/scripts/
          MACOS_BUNDLE_DIR=$(find src-tauri/target -path "*/release/bundle/macos" -type d | head -n 1)
          APP_PATH=$(find "$MACOS_BUNDLE_DIR" -name "*.app" -type d | head -n 1)
          if [ -z "$APP_PATH" ]; then
            echo "No .app found in $MACOS_BUNDLE_DIR"
            exit 1
          fi
          APP_NAME=$(basename "$APP_PATH")
          pkgbuild --root "$APP_PATH/.." \
                   --install-location /Applications \
                   --scripts pkg_temp/scripts \
                   --identifier "com.emerald.legacy" \
                   --version "1.0.0" \
                   "Emerald-Legacy-Launcher.pkg"
          mv "Emerald-Legacy-Launcher.pkg" "$MACOS_BUNDLE_DIR/"

      - name: upload build artifacts
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.artifact-name }}
          path: |
            src-tauri/target/*/release/bundle/**/*

  build-flatpak:
    runs-on: ubuntu-latest
    container:
      image: bilelmoussaoui/flatpak-github-actions:freedesktop-23.08
      options: --privileged
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true
      - name: sleep for 10 seconds
        run: sleep 10
      - uses: flatpak/flatpak-github-actions/flatpak-builder@v6
        with:
          bundle: Emerald.Legacy.Launcher.flatpak
          manifest-path: flatpak/io.github.Emerald_Legacy_Launcher.Emerald_Legacy_Launcher.yml
          cache-key: flatpak-builder-${{ github.sha }}
      - name: upload flatpak artifact
        uses: actions/upload-artifact@v4
        with:
          name: flatpak
          path: Emerald.Legacy.Launcher.flatpak
