#!/usr/bin/env bash
set -euo pipefail

if [[ "${GUARDEX_DISABLE_POST_MERGE_CLEANUP:-0}" == "1" ]]; then
  exit 0
fi

repo_root="$(git rev-parse --show-toplevel 2>/dev/null || true)"
if [[ -z "$repo_root" ]]; then
  exit 0
fi
guardex_env_helper="${repo_root}/scripts/guardex-env.sh"
if [[ -f "$guardex_env_helper" ]]; then
  # shellcheck source=/dev/null
  source "$guardex_env_helper"
fi
if declare -F guardex_repo_is_enabled >/dev/null 2>&1 && ! guardex_repo_is_enabled "$repo_root"; then
  exit 0
fi

branch="$(git -C "$repo_root" rev-parse --abbrev-ref HEAD 2>/dev/null || true)"
if [[ -z "$branch" || "$branch" == "HEAD" ]]; then
  exit 0
fi

base_branch="${GUARDEX_BASE_BRANCH:-$(git -C "$repo_root" config --get multiagent.baseBranch || true)}"
if [[ -z "$base_branch" ]]; then
  base_branch="dev"
fi

if [[ "$branch" != "$base_branch" ]]; then
  exit 0
fi

if [[ -n "${GUARDEX_CLI_ENTRY:-}" ]]; then
  node_bin="${GUARDEX_NODE_BIN:-node}"
  if command -v "$node_bin" >/dev/null 2>&1; then
    "$node_bin" "$GUARDEX_CLI_ENTRY" cleanup \
      --target "$repo_root" \
      --base "$base_branch" \
      --include-pr-merged \
      --keep-clean-worktrees >/dev/null 2>&1 || true
  fi
  exit 0
fi

cli_bin="${GUARDEX_CLI_BIN:-}"
if [[ -z "$cli_bin" ]]; then
  if command -v gx >/dev/null 2>&1; then
    cli_bin="gx"
  elif command -v gitguardex >/dev/null 2>&1; then
    cli_bin="gitguardex"
  else
    exit 0
  fi
fi

"$cli_bin" cleanup \
  --target "$repo_root" \
  --base "$base_branch" \
  --include-pr-merged \
  --keep-clean-worktrees >/dev/null 2>&1 || true

exit 0
