name: Build macOS Release Artifacts

on:
  workflow_dispatch:
    inputs:
      release_version:
        description: 'Version tag for the release (defaults to YYYYMMDD)'
        required: false
        type: string

permissions:
  contents: write  # Needed to create releases

jobs:
  build-macos:
    runs-on: macos-latest

    steps:
      - name: Set release version
        id: version
        run: |
          if [ -z "${{ github.event.inputs.release_version }}" ]; then
            echo "release_version=$(date +%Y%m%d)" >> $GITHUB_OUTPUT
          else
            echo "release_version=${{ github.event.inputs.release_version }}" >> $GITHUB_OUTPUT
          fi

      - name: Checkout code
        uses: actions/checkout@v4

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

      - name: Setup Go
        uses: actions/setup-go@v5
        with:
          go-version-file: 'hld/go.mod'

      - name: Setup Bun
        uses: oven-sh/setup-bun@v2
        with:
          bun-version: latest

      - name: Run repository setup
        run: make setup

      - name: Install WUI dependencies
        working-directory: humanlayer-wui
        run: bun install

      - name: Build Tauri app (including DMG)
        working-directory: humanlayer-wui
        run: bun run tauri build
        env:
          APPLE_SIGNING_IDENTITY: "-"  # Ad-hoc signing to prevent "damaged" error

      - name: Build daemon for macOS ARM
        working-directory: hld
        run: GOOS=darwin GOARCH=arm64 go build -o hld-darwin-arm64 ./cmd/hld

      - name: Upload DMG artifact
        uses: actions/upload-artifact@v4
        with:
          name: humanlayer-wui-macos-dmg
          path: humanlayer-wui/src-tauri/target/release/bundle/dmg/*.dmg
          if-no-files-found: error

      - name: Upload daemon artifact
        uses: actions/upload-artifact@v4
        with:
          name: hld-darwin-arm64
          path: hld/hld-darwin-arm64
          if-no-files-found: error

      # Create GitHub Release with artifacts
      - name: Create Release
        if: github.event_name == 'workflow_dispatch'
        id: create_release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ steps.version.outputs.release_version }}
          name: HumanLayer ${{ steps.version.outputs.release_version }} - macOS Release
          body: |
            ## HumanLayer ${{ steps.version.outputs.release_version }} - macOS Release

            This release includes:
            - **CodeLayer** - Desktop application (DMG installer)
            - **HumanLayer Daemon (hld)** - Command-line daemon (ARM64 binary)

            ### Installation Instructions

            - Install the CLI: `npm install -g humanlayer@0.11.0`
            - Download daemon binary (hld-darwin-arm64)
            - Run it in a terminal e.g. `chmod +x ~/Downloads/hld-darwin-arm64 && ~/Downloads/hld-darwin-arm64`
            - Dismiss the security modal
            - Go to System Settings > Privacy & Security and scroll to the bottom, find the "allow" button and click it
            - Run it again `~/Downloads/hld-darwin-arm64`
            - Download CodeLayer (CodeLayer.dmg)
            - Copy the app to Applications
            - Open CodeLayer with your preferred app launcher
            - Dismiss security modal
            - Go to System Settings > Privacy & Security and allow
            - Open CodeLayer again

            ### Requirements
            - macOS (Apple Silicon/M-series)
            - Node.js installed

          draft: true
          prerelease: false

          files: |
            humanlayer-wui/src-tauri/target/release/bundle/dmg/*.dmg
            hld/hld-darwin-arm64
