#!/bin/bash
# Release script for northroot-engine
# Usage: ./scripts/release.sh <version>
# Example: ./scripts/release.sh 0.2.1

set -euo pipefail

if [ $# -ne 1 ]; then
    echo "Usage: $0 <version>"
    echo "Example: $0 0.2.1"
    exit 1
fi

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

# Validate version format (semantic versioning)
if [[ ! $VERSION =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.-]+)?(\+[a-zA-Z0-9.-]+)?$ ]]; then
    echo "❌ Error: Invalid version format. Use semantic versioning (e.g., 0.2.1, 1.0.0-beta.1)"
    exit 1
fi

# Check if we're on main branch
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ "$CURRENT_BRANCH" != "main" ]; then
    echo "⚠️  Warning: Not on main branch (currently on $CURRENT_BRANCH)"
    read -p "Continue anyway? (y/N) " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        exit 1
    fi
fi

# Check for uncommitted changes
if ! git diff-index --quiet HEAD --; then
    echo "❌ Error: Uncommitted changes detected. Commit or stash them first."
    exit 1
fi

# Check if tag already exists
if git rev-parse "$TAG" >/dev/null 2>&1; then
    echo "❌ Error: Tag $TAG already exists"
    exit 1
fi

echo "🚀 Preparing release $VERSION..."

# Update version in Cargo.toml
echo "  ✓ Updating Cargo.toml version..."
if [[ "$OSTYPE" == "darwin"* ]]; then
    # macOS
    sed -i '' "s/^version = \".*\"/version = \"${VERSION}\"/" Cargo.toml
else
    # Linux
    sed -i "s/^version = \".*\"/version = \"${VERSION}\"/" Cargo.toml
fi

# Verify the change
CARGO_VERSION=$(grep '^version = ' Cargo.toml | cut -d'"' -f2)
if [ "$CARGO_VERSION" != "$VERSION" ]; then
    echo "❌ Error: Failed to update version in Cargo.toml"
    exit 1
fi

# Run pre-push checks
echo "  ✓ Running validation checks..."
if ! cargo fmt --all -- --check; then
    echo "❌ Code is not formatted. Run 'cargo fmt --all' first."
    exit 1
fi

if ! cargo clippy --all-targets --all-features -- -D warnings; then
    echo "❌ Clippy found issues. Fix them first."
    exit 1
fi

if ! cargo test --all-features; then
    echo "❌ Tests failed. Fix them first."
    exit 1
fi

# Build release
echo "  ✓ Building release..."
if ! cargo build --all-features --release; then
    echo "❌ Build failed."
    exit 1
fi

# Package
echo "  ✓ Packaging crate..."
if ! cargo package --allow-dirty; then
    echo "❌ Packaging failed."
    exit 1
fi

# Commit version change
echo "  ✓ Committing version change..."
git add Cargo.toml Cargo.lock
git commit -m "chore: bump version to ${VERSION}" || {
    echo "⚠️  No changes to commit (version may already be correct)"
}

# Create tag
echo "  ✓ Creating tag $TAG..."
git tag -a "$TAG" -m "Release ${VERSION}"

echo ""
echo "✅ Release prepared successfully!"
echo ""
echo "Next steps:"
echo "  1. Review the changes:"
echo "     git show HEAD"
echo "     git show $TAG"
echo ""
echo "  2. Push the commit and tag:"
echo "     git push origin main"
echo "     git push origin $TAG"
echo ""
echo "  3. GitHub Actions will automatically:"
echo "     - Run full test suite"
echo "     - Publish to crates.io"
echo "     - Create GitHub release"
echo ""
echo "To push now, run:"
echo "  git push origin main && git push origin $TAG"

