name: Update Homebrew Tap

on:
  workflow_run:
    workflows: ["Release"]
    types: [completed]

jobs:
  update-tap:
    runs-on: ubuntu-latest
    if: ${{ github.event.workflow_run.conclusion == 'success' }}
    steps:
      - name: Extract version
        id: version
        run: |
          TAG="${{ github.event.workflow_run.head_branch }}"
          VERSION="${TAG#v}"
          echo "tag=$TAG" >> "$GITHUB_OUTPUT"
          echo "version=$VERSION" >> "$GITHUB_OUTPUT"

      - name: Download SHA256 files
        run: |
          TAG="${{ steps.version.outputs.tag }}"
          BASE="https://github.com/backmeupplz/superharness/releases/download/$TAG"
          for target in x86_64-unknown-linux-gnu aarch64-unknown-linux-gnu x86_64-apple-darwin aarch64-apple-darwin; do
            curl -fSL -o "superharness-${target}.sha256" "$BASE/superharness-${target}.sha256"
          done

      - name: Parse SHA256 sums
        id: sha
        run: |
          echo "linux_amd64=$(awk '{print $1}' superharness-x86_64-unknown-linux-gnu.sha256)" >> "$GITHUB_OUTPUT"
          echo "linux_arm64=$(awk '{print $1}' superharness-aarch64-unknown-linux-gnu.sha256)" >> "$GITHUB_OUTPUT"
          echo "macos_amd64=$(awk '{print $1}' superharness-x86_64-apple-darwin.sha256)" >> "$GITHUB_OUTPUT"
          echo "macos_arm64=$(awk '{print $1}' superharness-aarch64-apple-darwin.sha256)" >> "$GITHUB_OUTPUT"

      - name: Checkout Homebrew tap
        uses: actions/checkout@v4
        with:
          repository: backmeupplz/homebrew-superharness
          token: ${{ secrets.HOMEBREW_TAP_TOKEN }}
          path: homebrew-tap

      - name: Generate formula
        run: |
          VERSION="${{ steps.version.outputs.version }}"
          TAG="${{ steps.version.outputs.tag }}"
          BASE="https://github.com/backmeupplz/superharness/releases/download/$TAG"

          mkdir -p homebrew-tap/Formula
          cat > homebrew-tap/Formula/superharness.rb <<RUBY
          class Superharness < Formula
            desc "Autonomous multi-agent orchestrator for AI coding agents via tmux"
            homepage "https://github.com/backmeupplz/superharness"
            version "$VERSION"
            license "MIT"

            on_macos do
              on_intel do
                url "$BASE/superharness-x86_64-apple-darwin"
                sha256 "${{ steps.sha.outputs.macos_amd64 }}"
              end
              on_arm do
                url "$BASE/superharness-aarch64-apple-darwin"
                sha256 "${{ steps.sha.outputs.macos_arm64 }}"
              end
            end

            on_linux do
              on_intel do
                url "$BASE/superharness-x86_64-unknown-linux-gnu"
                sha256 "${{ steps.sha.outputs.linux_amd64 }}"
              end
              on_arm do
                url "$BASE/superharness-aarch64-unknown-linux-gnu"
                sha256 "${{ steps.sha.outputs.linux_arm64 }}"
              end
            end

            def install
              bin.install stable.url.split("/").last => "superharness"
            end

            test do
              assert_match "superharness", shell_output("#{bin}/superharness --help")
            end
          end
          RUBY
          sed -i 's/^          //' homebrew-tap/Formula/superharness.rb

      - name: Commit and push
        working-directory: homebrew-tap
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add Formula/superharness.rb
          git commit -m "Update superharness to ${{ steps.version.outputs.version }}"
          git push
