#!/usr/bin/env bash
#
# Bump version, commit to a new branch, push it, and return to main.
# You then create a PR, review, and merge. After merging, use
# scripts/release to tag and trigger the publish workflow.
#
# Usage:
#   scripts/bump-version <package> <version>
#
# Arguments:
#   package   One of: ipso, pytest-ipso, all
#   version   Semantic version without "v" prefix (e.g. 0.2.0)
#
# Examples:
#   scripts/bump-version all 0.2.0
#   scripts/bump-version ipso 0.2.0
#   scripts/bump-version pytest-ipso 0.3.0

set -euo pipefail

if [ "$#" -ne 2 ]; then
    echo "Usage: scripts/bump-version <package> <version>"
    echo "  package: ipso | pytest-ipso | all"
    echo "  version: e.g. 0.2.0"
    exit 1
fi

package="$1"
version="$2"

# Strip leading "v" if present
version="${version#v}"

if ! [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
    echo "Error: version must be in semver format (e.g. 0.2.0), got: $version"
    exit 1
fi

case "$package" in
    ipso|pytest-ipso|all) ;;
    *)
        echo "Error: unknown package '$package'. Use: ipso | pytest-ipso | all"
        exit 1
        ;;
esac

REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"

# Ensure working tree is clean
if ! git diff --quiet || ! git diff --cached --quiet; then
    echo "Error: working tree is not clean. Commit or stash changes first."
    exit 1
fi

# --- Version bump functions ---

bump_ipso() {
    echo "Bumping ipso to $version"
    sed -i "s/^version = \".*\"/version = \"$version\"/" "$REPO_ROOT/Cargo.toml"
    sed -i "s/^version = \".*\"/version = \"$version\"/" "$REPO_ROOT/ipso/pyproject.toml"
    sed -i "s/__version__ = \".*\"/__version__ = \"$version\"/" "$REPO_ROOT/ipso/src/ipso/__about__.py"
    echo "  Updated Cargo.toml"
    echo "  Updated ipso/pyproject.toml"
    echo "  Updated ipso/src/ipso/__about__.py"
    echo "  Updating Cargo.lock"
    cargo update --manifest-path "$REPO_ROOT/Cargo.toml" --package ipso || exit 1
    echo "  Updating ipso/uv.lock"
    uv lock --project "$REPO_ROOT/ipso" || exit 1
}

bump_pytest_ipso() {
    echo "Bumping pytest-ipso to $version"
    sed -i "s/^version = \".*\"/version = \"$version\"/" "$REPO_ROOT/pytest-ipso/pyproject.toml"
    sed -i "s/__version__ = \".*\"/__version__ = \"$version\"/" "$REPO_ROOT/pytest-ipso/src/pytest_ipso/__about__.py"
    echo "  Updated pytest-ipso/pyproject.toml"
    echo "  Updated pytest-ipso/src/pytest_ipso/__about__.py"
    echo "  Updating pytest-ipso/uv.lock"
    uv lock --project "$REPO_ROOT/pytest-ipso" || exit 1
}

# --- Determine branch name ---

if [ "$package" = "all" ]; then
    branch_name="release/v${version}"
else
    branch_name="${package}/v${version}"
fi

# --- Remove stale release branch (local + origin) if it already exists ---

cleanup_existing_branch() {
    local bn="$1"
    git fetch origin --prune || exit 1

    if [ -n "$(git ls-remote --heads origin "$bn" 2>/dev/null)" ]; then
        echo "==> Deleting existing remote branch: origin/$bn"
        git push origin --delete "$bn" || exit 1
    fi

    if git show-ref --verify --quiet "refs/heads/$bn"; then
        echo "==> Deleting existing local branch: $bn"
        local current
        current="$(git rev-parse --abbrev-ref HEAD)"
        if [ "$current" = "$bn" ]; then
            git checkout main || exit 1
        fi
        git branch -D "$bn" || exit 1
    fi
}

cleanup_existing_branch "$branch_name"

# --- Create branch, bump, commit, push, return to main ---

echo "==> Creating branch: $branch_name"
git checkout -b "$branch_name" || exit 1

case "$package" in
    ipso)       bump_ipso ;;
    pytest-ipso) bump_pytest_ipso ;;
    all)             bump_ipso; bump_pytest_ipso ;;
esac

echo ""
echo "==> Committing version bump"
git add -A || exit 1
git commit -m "bump: ${package} v${version}" || exit 1

echo "==> Pushing branch to origin"
git push -u origin "$branch_name" || exit 1

echo "==> Returning to main"
git checkout main || exit 1

echo ""
echo "Done! Branch '$branch_name' has been pushed."
echo "Next steps:"
echo "  1. Create a PR from '$branch_name' into main"
echo "  2. Merge the PR"
echo "  3. Run: scripts/release $package"
