#!/bin/bash
# Automated version bump script for DroidTUI
# Usage: ./scripts/bump_version.sh <new_version>
# Example: ./scripts/bump_version.sh 0.2.5

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Check if version argument is provided
if [ -z "$1" ]; then
    echo -e "${RED}Error: Version number required${NC}"
    echo "Usage: $0 <version>"
    echo "Example: $0 0.2.5"
    exit 1
fi

NEW_VERSION=$1

# Validate version format (semantic versioning)
if ! [[ $NEW_VERSION =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
    echo -e "${RED}Error: Invalid version format${NC}"
    echo "Version must be in format: X.Y.Z (e.g., 0.2.5)"
    exit 1
fi

echo -e "${YELLOW}Bumping version to ${NEW_VERSION}...${NC}"

# Get current version from Cargo.toml
CURRENT_VERSION=$(grep '^version = ' Cargo.toml | head -1 | sed 's/version = "\(.*\)"/\1/')
echo -e "Current version: ${CURRENT_VERSION}"
echo -e "New version: ${NEW_VERSION}"

# Ask for confirmation
read -p "Continue? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo -e "${YELLOW}Aborted${NC}"
    exit 0
fi

# Update Cargo.toml
echo -e "${GREEN}Updating Cargo.toml...${NC}"
sed -i "s/^version = \".*\"/version = \"${NEW_VERSION}\"/" Cargo.toml

# Update README.md version badge
echo -e "${GREEN}Updating README.md...${NC}"
sed -i "s/version-[0-9]*\.[0-9]*\.[0-9]*-blue/version-${NEW_VERSION}-blue/" README.md

# Update Cargo.lock
echo -e "${GREEN}Updating Cargo.lock...${NC}"
cargo build --release > /dev/null 2>&1

# Check formatting
echo -e "${GREEN}Running cargo fmt...${NC}"
cargo fmt

# Check for issues
echo -e "${GREEN}Running cargo clippy...${NC}"
if ! cargo clippy -- -D warnings; then
    echo -e "${RED}Clippy found issues. Please fix them before continuing.${NC}"
    exit 1
fi

# Run tests
echo -e "${GREEN}Running tests...${NC}"
if ! cargo test; then
    echo -e "${RED}Tests failed. Please fix them before continuing.${NC}"
    exit 1
fi

# Git operations
echo -e "${GREEN}Staging changes...${NC}"
git add Cargo.toml Cargo.lock README.md

echo -e "${GREEN}Creating commit...${NC}"
git commit -m "chore: bump version to ${NEW_VERSION}"

echo -e "${GREEN}Creating tag...${NC}"
git tag -a "v${NEW_VERSION}" -m "Release v${NEW_VERSION}"

echo -e "${YELLOW}Changes committed and tagged locally.${NC}"
echo -e "${YELLOW}To push to remote, run:${NC}"
echo -e "  git push origin main"
echo -e "  git push origin v${NEW_VERSION}"
echo ""
echo -e "${GREEN}Version bump complete! 🚀${NC}"
