name: Create macOS DMG
on:
  workflow_dispatch:

jobs:
  build:
    name: Build ${{ matrix.arch }}
    runs-on: macos-latest
    strategy:
      matrix:
        # This creates two parallel jobs
        arch: [arm64, amd64]

    steps:
      - name: Checkout code
        uses: actions/checkout@v6
        with:
          ref: main

      # 1. Install Go from go.mod
      - name: Setup Go
        uses: actions/setup-go@v6
        with:
          go-version-file: go.mod

      - name: Setup pnpm
        uses: pnpm/action-setup@v6
        with:
          version: 10.33.0
          run_install: false

      - name: Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version: 22
          cache: pnpm
          cache-dependency-path: web/frontend/pnpm-lock.yaml

      # 3. Build the application bundle
      - name: Build with Make
        run: make build ARCH=${{ matrix.arch }} && make build-macos-app ARCH=${{ matrix.arch }}

      # 4. Apply ad-hoc signing
      - name: Ad-hoc Sign
        run: codesign --force --deep --sign - "build/PicoClaw Launcher.app"

      # 5. Install the DMG packaging tool
      - name: Install create-dmg
        run: brew install create-dmg

      # 6. Create the DMG
      - name: Create DMG
        run: |
          mkdir -p dist
          create-dmg \
            --volname "PicoClaw Installer" \
            --window-pos 200 120 \
            --window-size 800 400 \
            --icon-size 100 \
            --icon "PicoClaw Launcher.app" 200 190 \
            --hide-extension "PicoClaw Launcher.app" \
            --app-drop-link 600 185 \
            "dist/picoclaw-${{ matrix.arch }}.dmg" \
            "build/PicoClaw Launcher.app"

      # 7. Upload the DMG as a GitHub artifact
      - name: Upload DMG
        uses: actions/upload-artifact@v7
        with:
          name: macos-dmg-${{ matrix.arch }}
          path: dist/*.dmg
