#!/usr/bin/env bash
# Create a release tag with automatic version bumping.
#
# Usage: scripts/release/release-tag v1.2.0
#
# This script:
# 1. Validates the version format (vX.Y.Z)
# 2. Bumps versions in source plugin and project metadata
# 3. Runs `make build` to regenerate root manifests and dist/
# 4. Updates CHANGELOG.md with a placeholder entry
# 5. Commits the version bump
# 6. Creates an annotated git tag
set -euo pipefail

if [ $# -ne 1 ]; then
	echo "Usage: scripts/release/release-tag <version>"
	echo "Example: scripts/release/release-tag v1.2.0"
	exit 1
fi

TAG="$1"
VERSION="${TAG#v}"

# Validate semver format
if ! [[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
	echo "ERROR: '$TAG' is not a valid semver tag (expected vX.Y.Z)"
	exit 1
fi

# Check for clean working tree
if [ -n "$(git status --porcelain)" ]; then
	echo "ERROR: working tree is not clean — commit or stash changes first"
	exit 1
fi

# Check tag doesn't already exist
if git rev-parse "$TAG" &>/dev/null; then
	echo "ERROR: tag '$TAG' already exists"
	exit 1
fi

echo "Bumping versions to $VERSION..."

# Bump src/plugins/marketplace.yaml (top-level version)
sed -i '' "s/^version:.*/version: $VERSION/" src/plugins/marketplace.yaml

# Bump per-plugin src/plugins/*/plugin.yaml versions
shopt -s nullglob
plugin_yamls=(src/plugins/*/plugin.yaml)
project_version_files=(
	pyproject.toml
	uv.lock
	package.json
	src/skills/playwright-skill/scripts/package.json
	tests/fixtures/golden_skills/playwright-skill/*/scripts/package.json
)
shopt -u nullglob
if [ ${#plugin_yamls[@]} -eq 0 ]; then
	echo "ERROR: no src/plugins/*/plugin.yaml files found"
	exit 1
fi
for f in "${plugin_yamls[@]}"; do
	sed -i '' "s/^version:.*/version: $VERSION/" "$f"
done
for f in "${project_version_files[@]}"; do
	if [[ "$f" == *.toml ]]; then
		sed -i '' "s/^version = .*/version = \"$VERSION\"/" "$f"
	else
		sed -i '' "s/\"version\": \"[^\"]*\"/\"version\": \"$VERSION\"/" "$f"
	fi
done

# Regenerate dist/ and root manifests from the bumped sources
echo "Running 'make build' to regenerate manifests..."
make build

# Add CHANGELOG entry if not already present
if ! grep -q "\[$VERSION\]" CHANGELOG.md; then
	DATE=$(date +%Y-%m-%d)
	python3 - "$VERSION" "$DATE" <<'PY'
from pathlib import Path
import sys

version, date = sys.argv[1:3]
path = Path("CHANGELOG.md")
text = path.read_text()
marker = "## [Unreleased]\n"
entry = f"\n## [{version}] - {date}\n\n"
if marker not in text:
    raise SystemExit("CHANGELOG.md missing ## [Unreleased] marker")
path.write_text(text.replace(marker, marker + entry, 1))
PY
	echo "Added CHANGELOG entry for $VERSION — edit it before pushing."
fi

# Show what changed
echo ""
echo "Version bumped to $VERSION in:"
git diff --name-only | sed 's/^/  /'
echo ""

# Commit and tag
git add \
	src/plugins/marketplace.yaml \
	"${plugin_yamls[@]}" \
	"${project_version_files[@]}" \
	.claude-plugin/marketplace.json \
	.agents/plugins/marketplace.json \
	gemini-extension.json \
	dist/ \
	CHANGELOG.md
git commit -m "chore: bump version to $VERSION"
git tag -a "$TAG" -m "Release $VERSION"

echo ""
echo "Created tag $TAG"
echo "Push with: git push origin master $TAG"
