#!/usr/bin/env bash
# Run benchlab comparisons and post (or print) a markdown PR comment.
#
# Required env vars:
#   BEFORE_SHA   base commit
#   AFTER_SHA    head commit
#   BENCH_PKGS   JSON array of Go package paths, e.g. '["./topdown","./ast"]'
#
# Optional (needed to post a PR comment):
#   GH_TOKEN     GitHub token with pull-requests:write
#   COMMIT_SHA   the merge commit SHA used to look up the PR
set -euo pipefail

: "${BEFORE_SHA:?}"
: "${AFTER_SHA:?}"
: "${BENCH_PKGS:?}"

for pkg in $(echo "$BENCH_PKGS" | jq -r '.[]'); do
  benchlab \
    -commit "$BEFORE_SHA","$AFTER_SHA" \
    -pkg "$pkg" \
    -host local:tags=opa_wasm \
    -reps 3 \
    -benchtime 300ms \
    -run '^$'
done

body() {
  echo "<details><summary>Benchmark Comparison (\`${BEFORE_SHA}\` vs \`${AFTER_SHA}\`)</summary>"
  echo ""
  for f in .benchlab/benchstat.*.txt; do
    awk '
      /^benchmark \\ host/ { found=1; print "| benchmark | delta |"; print "| --- | --- |"; next }
      found && $2 ~ /^[+-]/ {
        val = $2; for (i = 3; i <= NF; i++) val = val " " $i
        print "| " $1 " | " val " |"
      }
    ' "$f"
    echo ""
  done
  echo "</details>"
  echo ""
  echo "_This comment was automatically generated by the benchmarks workflow._"
}

if [ -n "${GH_TOKEN:-}" ] && [ -n "${COMMIT_SHA:-}" ]; then
  PR_NUMBER=$(gh pr list --search "${COMMIT_SHA}" --state merged --json number --jq '.[0].number')
  if [ -z "$PR_NUMBER" ] || [ "$PR_NUMBER" = "null" ]; then
    echo "Could not find originating PR for commit ${COMMIT_SHA}"
    exit 0
  fi
  body > body.md
  gh pr comment "${PR_NUMBER}" --body-file body.md
else
  body
fi
