#!/usr/bin/env bash
# Project release step. Invoked by orchestration/finalize AFTER it has merged the
# feature branch into the base branch. Any args passed to `finalize` after the
# slug are forwarded here as the release bump (e.g. patch | minor | major | X.Y.Z).
#
# Optional: with no argument it is a no-op, so an ordinary finalize never cuts a
# release. Pass a bump to tag + push, which triggers the GitHub release workflow.
# See docs/RELEASING.md for the full flow.
set -euo pipefail

bump="${1:-}"
if [[ -z "$bump" ]]; then
  echo "finalize-release: no version bump given; skipping release." >&2
  exit 0
fi

cd "$(git rev-parse --show-toplevel)"

# Bump the version in pyproject.toml + uv.lock (accepts patch/minor/major or an
# explicit X.Y.Z version). uv keeps the lockfile in sync; hatch's CLI cannot write
# the version because it is defined statically under [project].version.
if [[ "$bump" =~ ^[0-9]+\.[0-9]+\.[0-9]+([.+-].*)?$ ]]; then
  ./run.sh uv version "$bump"
else
  ./run.sh uv version --bump "$bump"
fi
version="$(./run.sh uv version --short)"

git add pyproject.toml uv.lock
git commit -m "chore: bump version to ${version}"
git tag -a "v${version}" -m "Release v${version}"

# Push commit + tag; the tag push triggers CI + the release workflow (PyPI via OIDC).
git push origin HEAD --follow-tags
