name: Build macOS

on:
  workflow_dispatch:
    inputs:
      version:
        description: "Version to build (e.g. 0.4.0)"
        required: true

permissions:
  contents: write

jobs:
  frontend:
    name: Build frontend
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: pnpm/action-setup@v4
        with:
          version: latest

      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: pnpm

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Build
        run: pnpm build

      - name: Upload dist
        uses: actions/upload-artifact@v4
        with:
          name: frontend-dist
          path: dist/
          retention-days: 1

  tauri:
    name: Tauri (macOS)
    needs: frontend
    runs-on: macos-latest
    steps:
      - uses: actions/checkout@v4

      - name: Download frontend dist
        uses: actions/download-artifact@v4
        with:
          name: frontend-dist
          path: dist/

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: aarch64-apple-darwin,x86_64-apple-darwin

      - name: Rust cache
        uses: Swatinem/rust-cache@v2
        with:
          workspaces: src-tauri

      - uses: pnpm/action-setup@v4
        with:
          version: latest

      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: pnpm

      - name: Install JS dependencies
        run: pnpm install --frozen-lockfile

      - name: Download Suwayomi binaries
        run: |
          download_suwayomi() {
            local asset="$1" sha="$2" outdir="$3"
            curl -fsSL \
              "https://github.com/Suwayomi/Suwayomi-Server-preview/releases/download/v2.1.2087/${asset}" \
              -o "${outdir}.tar.gz"
            echo "${sha}  ${outdir}.tar.gz" | shasum -a 256 -c -
            mkdir -p "${outdir}"
            tar -xzf "${outdir}.tar.gz" -C "${outdir}" --strip-components=1
          }

          download_suwayomi \
            "Suwayomi-Server-v2.1.2087-macOS-arm64.tar.gz" \
            "59f73a53a139d5d843e16cab4f3ac425a410add6bee0a60920fa26eb0a4b8a5c" \
            "suwayomi-arm64"

          download_suwayomi \
            "Suwayomi-Server-v2.1.2087-macOS-x64.tar.gz" \
            "da7e664e4c2615a0b9eac09ee38fe979feee1d6c0b266e19dba1ceea8ae3795c" \
            "suwayomi-x64"

      - name: Stage Suwayomi sidecars
        run: |
          mkdir -p src-tauri/binaries

          stage_arch() {
            local srcdir="$1"
            local arch="$2"
            local sidecar="src-tauri/binaries/suwayomi-server-${arch}"
            local bundle_dest="src-tauri/binaries/suwayomi-bundle-${arch}"

            JAR=$(find "$srcdir" -name "Suwayomi-Server.jar" | head -1)
            JAVA=$(find "$srcdir" -path "*/jre/bin/java" | head -1)

            if [ -z "$JAR" ]; then
              echo "ERROR: Suwayomi-Server.jar not found in $srcdir"
              find "$srcdir" -type f | head -30
              exit 1
            fi
            if [ -z "$JAVA" ]; then
              echo "ERROR: jre/bin/java not found in $srcdir"
              find "$srcdir" -type f | head -30
              exit 1
            fi

            echo "${arch}: jar=${JAR}  java=${JAVA}"

            cp -r "$srcdir" "$bundle_dest"

            # The launcher script is committed at src-tauri/binaries/suwayomi-launcher.sh
            # to avoid embedding a heredoc in YAML (which breaks GitHub Actions parsing).
            cp src-tauri/binaries/suwayomi-launcher.sh "$sidecar"
            chmod +x "$sidecar"
            echo "Staged sidecar: $sidecar"
          }

          stage_arch suwayomi-arm64 aarch64-apple-darwin
          stage_arch suwayomi-x64   x86_64-apple-darwin

      - name: Patch tauri.conf.json for CI
        run: |
          sed -i '' 's/"beforeBuildCommand": "pnpm build"/"beforeBuildCommand": ""/' src-tauri/tauri.conf.json

      - name: Swap bundle for aarch64
        run: |
          rm -rf src-tauri/binaries/suwayomi-bundle
          cp -r src-tauri/binaries/suwayomi-bundle-aarch64-apple-darwin \
                src-tauri/binaries/suwayomi-bundle

      - name: Build Tauri app (aarch64)
        run: pnpm tauri build --target aarch64-apple-darwin --config src-tauri/tauri.macos.conf.json
        env:
          APPLE_SIGNING_IDENTITY: ${{ secrets.APPLE_SIGNING_IDENTITY || '-' }}

      - name: Swap bundle for x86_64
        run: |
          rm -rf src-tauri/binaries/suwayomi-bundle
          cp -r src-tauri/binaries/suwayomi-bundle-x86_64-apple-darwin \
                src-tauri/binaries/suwayomi-bundle

      - name: Build Tauri app (x86_64)
        run: pnpm tauri build --target x86_64-apple-darwin --config src-tauri/tauri.macos.conf.json
        env:
          APPLE_SIGNING_IDENTITY: ${{ secrets.APPLE_SIGNING_IDENTITY || '-' }}

      - name: Upload macOS artifacts to release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          VERSION: ${{ github.event.inputs.version }}
        run: |
          # Wait for the Windows workflow to have created the draft release
          for i in $(seq 1 12); do
            RELEASE_ID=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN"               "https://api.github.com/repos/moku-project/Moku/releases"               | jq -r '.[] | select(.tag_name == "v'"$VERSION"'") | .id' | head -1)
            if [ -n "$RELEASE_ID" ]; then break; fi
            echo "Waiting for release to exist... attempt $i"
            sleep 15
          done

          if [ -z "$RELEASE_ID" ]; then
            echo "ERROR: Could not find release for v$VERSION after waiting"
            exit 1
          fi

          echo "Found release ID: $RELEASE_ID"

          upload_asset() {
            local file="$1"
            local name="$2"
            echo "Uploading $name..."
            curl -s -X POST               -H "Authorization: Bearer $GITHUB_TOKEN"               -H "Content-Type: application/octet-stream"               --data-binary @"$file"               "https://uploads.github.com/repos/moku-project/Moku/releases/$RELEASE_ID/assets?name=$name"
          }

          ARM64_DMG=$(find src-tauri/target/aarch64-apple-darwin/release/bundle/dmg -name "*.dmg" | head -1)
          X64_DMG=$(find src-tauri/target/x86_64-apple-darwin/release/bundle/dmg -name "*.dmg" | head -1)

          [ -n "$ARM64_DMG" ] && upload_asset "$ARM64_DMG" "moku-macos-arm64-${VERSION}.dmg"
          [ -n "$X64_DMG"   ] && upload_asset "$X64_DMG"   "moku-macos-x64-${VERSION}.dmg"