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

# Require master
branch=$(git rev-parse --abbrev-ref HEAD)
if [[ "$branch" != "master" ]]; then
  echo "Refusing to release from branch '$branch' (must be 'master')." >&2
  exit 1
fi

# Require clean working tree (optional, but highly recommended)
if [[ -n "$(git status --porcelain)" ]]; then
  echo "Working tree is dirty. Commit or stash changes before releasing." >&2
  exit 1
fi

# Read version from pyproject.toml
VERSION=$(
  python - <<'EOF'
import tomllib, pathlib
data = tomllib.loads(pathlib.Path("pyproject.toml").read_text())
print(data["project"]["version"])
EOF
)

TAG="v$VERSION"

# Create annotated tag and push
git tag -a "$TAG" -m "Magnetron $VERSION"
git push origin "$TAG"

# Create GitHub release (draft, with autogenerated notes)
gh release create "$TAG" --generate-notes --draft