#!/usr/bin/env bash
# Update the version number
set -Eeuo pipefail
trap 'echo >&2 "${BASH_SOURCE[0]}: line $LINENO: $BASH_COMMAND: exitcode $?"' ERR
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR/.."

PROJECT_PATH="./lib/config.sh"

# Function to display usage
show_usage() {
    cat << EOF
Usage: bump-version [TYPE]

Bump the version number, commit to a new branch, push, and open a PR
with auto-merge (squash) enabled. CI tags v<NEW> on merge to main.

Arguments:
  TYPE        Version bump type: major, minor, or patch (default: patch)
              
              major: X.0.0 (increment major version, reset minor and patch)
              minor: X.Y.0 (increment minor version, reset patch)
              patch: X.Y.Z (increment patch version)

Options:
  -h, --help  Show this help message and exit

Examples:
  bump-version        # Bumps patch version (e.g., 1.2.3 -> 1.2.4)
  bump-version minor  # Bumps minor version (e.g., 1.2.3 -> 1.3.0)
  bump-version major  # Bumps major version (e.g., 1.2.3 -> 2.0.0)

Note: Requires a clean git workspace (no uncommitted changes or untracked files).
EOF
}

# Check for help flag
if [[ "${1:-}" == "-h" ]] || [[ "${1:-}" == "--help" ]]; then
    show_usage
    exit 0
fi

# Check the project file exists
if [[ ! -f "$PROJECT_PATH" ]]; then
  echo "ERROR: Cannot find $PROJECT_PATH"
  exit 1
fi

# Check for clean git workspace
if ! git diff-index --quiet HEAD -- || [[ -n "$(git ls-files --others --exclude-standard)" ]]; then
    echo "Error: Git workspace has uncommitted changes or untracked files."
    echo "Please commit or stash your changes before bumping the version."
    git status --short
    exit 1
fi

# Get current version
CURRENT_VERSION=$(awk -F'"' '/^[[:space:]]*VERSION="[0-9.]*"/ {print $2; exit}' "$PROJECT_PATH")
echo "Current version: $CURRENT_VERSION"

# Parse version components
IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT_VERSION"

# Default to patch bump
BUMP_TYPE="${1:-patch}"

case "$BUMP_TYPE" in
  major)
    MAJOR=$((MAJOR + 1))
    MINOR=0
    PATCH=0
    ;;
  minor)
    MINOR=$((MINOR + 1))
    PATCH=0
    ;;
  patch)
    PATCH=$((PATCH + 1))
    ;;
  *)
    echo "Invalid bump type. Use: major, minor, or patch"
    exit 1
    ;;
esac

NEW_VERSION="$MAJOR.$MINOR.$PATCH"
echo "New version: $NEW_VERSION"

BRANCH="bump-version-$NEW_VERSION"
if git show-ref --verify --quiet "refs/heads/$BRANCH"; then
    echo "Error: branch $BRANCH already exists locally"
    exit 1
fi

# Update version
sed -i '' "s/VERSION=\"$CURRENT_VERSION\"/VERSION=\"$NEW_VERSION\"/g" "$PROJECT_PATH"

git switch -c "$BRANCH"
git commit -am "Bump version to $NEW_VERSION"
git push -u origin "$BRANCH"

# Tag is created by CI on merge to main — see .github/workflows/ci.yml
gh pr create --fill --title "Bump version to $NEW_VERSION"
gh pr merge --auto --squash --delete-branch

echo "✓ Version bumped to $NEW_VERSION on branch $BRANCH"
echo "✓ PR opened with auto-merge — squash-merges once CI passes; tag created automatically"
