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

# Self-contained pre-push hook for automatic semver patch version bumping
# Triggers on push to main/master, creates version bump commit + tag
#
# Setup: Copy to .githooks/pre-push and add to package.json:
#   "scripts": { "prepare": "git config core.hooksPath .githooks" }
#
# Skip version bump: Include [skip-version] in your commit message

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

log_info() { echo -e "${GREEN}[version-bump]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[version-bump]${NC} $1"; }
log_error() { echo -e "${RED}[version-bump]${NC} $1" >&2; }

# Get the branch being pushed
BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "")

# Only bump on main/master
if [[ "$BRANCH" != "main" && "$BRANCH" != "master" ]]; then
    exit 0
fi

# Get last commit message
LAST_MSG=$(git log -1 --format=%s 2>/dev/null || echo "")

# Skip if this is already a version bump commit (prevents infinite loop)
if [[ "$LAST_MSG" == chore:\ bump\ version* ]]; then
    log_info "Skipping: already a version bump commit"
    exit 0
fi

# Skip if commit message contains [skip-version]
if [[ "$LAST_MSG" == *"[skip-version]"* ]]; then
    log_info "Skipping: [skip-version] found in commit message"
    exit 0
fi

# Find git root and package.json
GIT_ROOT=$(git rev-parse --show-toplevel 2>/dev/null || echo "")
if [[ -z "$GIT_ROOT" ]]; then
    log_error "Not in a git repository"
    exit 1
fi

PACKAGE_JSON="$GIT_ROOT/package.json"
if [[ ! -f "$PACKAGE_JSON" ]]; then
    log_warn "No package.json found at git root, skipping version bump"
    exit 0
fi

# Extract current version using portable tools
CURRENT_VERSION=$(grep -o '"version"[[:space:]]*:[[:space:]]*"[^"]*"' "$PACKAGE_JSON" | head -1 | sed 's/.*"\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)".*/\1/')

if [[ -z "$CURRENT_VERSION" ]]; then
    log_error "Could not parse version from package.json"
    exit 1
fi

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

# Bump patch version
NEW_PATCH=$((PATCH + 1))
NEW_VERSION="${MAJOR}.${MINOR}.${NEW_PATCH}"

log_info "Pushing to $BRANCH - bumping version: $CURRENT_VERSION -> $NEW_VERSION"

# Update package.json (preserve formatting, just replace version)
if [[ "$(uname)" == "Darwin" ]]; then
    # macOS sed requires empty string for -i
    sed -i '' "s/\"version\"[[:space:]]*:[[:space:]]*\"$CURRENT_VERSION\"/\"version\": \"$NEW_VERSION\"/" "$PACKAGE_JSON"
else
    # GNU sed
    sed -i "s/\"version\"[[:space:]]*:[[:space:]]*\"$CURRENT_VERSION\"/\"version\": \"$NEW_VERSION\"/" "$PACKAGE_JSON"
fi

log_info "Updated $PACKAGE_JSON"

# Git commit
git add "$PACKAGE_JSON"
COMMIT_MSG="chore: bump version to $NEW_VERSION"
git commit -m "$COMMIT_MSG"
log_info "Created commit: $COMMIT_MSG"

# Git tag
TAG_NAME="v$NEW_VERSION"
if git tag -l "$TAG_NAME" | grep -q "$TAG_NAME"; then
    log_warn "Tag $TAG_NAME already exists, skipping"
else
    git tag -a "$TAG_NAME" -m "Release $NEW_VERSION"
    log_info "Created tag: $TAG_NAME"
fi

log_info "Version bump complete!"
exit 0
