#!/bin/bash
# Automated version tagging script
# Creates git tags based on semantic versioning
# Usage: ./version-tag.sh [major|minor|patch]

set -e

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

BUMP_TYPE="${1:-patch}"

echo "========================================="
echo "GuruConnect Version Tagging"
echo "========================================="
echo ""

# Validate bump type
if [[ ! "$BUMP_TYPE" =~ ^(major|minor|patch)$ ]]; then
    echo -e "${RED}ERROR: Invalid bump type: $BUMP_TYPE${NC}"
    echo "Usage: $0 [major|minor|patch]"
    exit 1
fi

# Get current version from latest tag
CURRENT_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")
echo "Current version: $CURRENT_TAG"

# Parse version
if [[ $CURRENT_TAG =~ ^v([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
    MAJOR="${BASH_REMATCH[1]}"
    MINOR="${BASH_REMATCH[2]}"
    PATCH="${BASH_REMATCH[3]}"
else
    echo -e "${YELLOW}No valid version tag found, starting from v0.1.0${NC}"
    MAJOR=0
    MINOR=1
    PATCH=0
fi

# Bump version
case $BUMP_TYPE in
    major)
        MAJOR=$((MAJOR + 1))
        MINOR=0
        PATCH=0
        ;;
    minor)
        MINOR=$((MINOR + 1))
        PATCH=0
        ;;
    patch)
        PATCH=$((PATCH + 1))
        ;;
esac

NEW_TAG="v${MAJOR}.${MINOR}.${PATCH}"

echo "New version: $NEW_TAG"
echo ""

# Check if tag already exists
if git rev-parse "$NEW_TAG" >/dev/null 2>&1; then
    echo -e "${RED}ERROR: Tag $NEW_TAG already exists${NC}"
    exit 1
fi

# Show changes since last tag
echo "Changes since $CURRENT_TAG:"
echo "-------------------------------------------"
git log --oneline "${CURRENT_TAG}..HEAD" | head -20
echo "-------------------------------------------"
echo ""

# Confirm
read -p "Create tag $NEW_TAG? (y/N) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo "Cancelled."
    exit 0
fi

# Update Cargo.toml versions
echo ""
echo "Updating Cargo.toml versions..."
if [ -f "server/Cargo.toml" ]; then
    sed -i.bak "s/^version = .*/version = \"${MAJOR}.${MINOR}.${PATCH}\"/" server/Cargo.toml
    rm server/Cargo.toml.bak 2>/dev/null || true
    echo -e "${GREEN}Updated server/Cargo.toml${NC}"
fi

if [ -f "agent/Cargo.toml" ]; then
    sed -i.bak "s/^version = .*/version = \"${MAJOR}.${MINOR}.${PATCH}\"/" agent/Cargo.toml
    rm agent/Cargo.toml.bak 2>/dev/null || true
    echo -e "${GREEN}Updated agent/Cargo.toml${NC}"
fi

# Commit version bump
echo ""
echo "Committing version bump..."
git add server/Cargo.toml agent/Cargo.toml 2>/dev/null || true
git commit -m "chore: bump version to ${NEW_TAG}" || echo "No changes to commit"

# Create tag
echo ""
echo "Creating tag $NEW_TAG..."
git tag -a "$NEW_TAG" -m "Release $NEW_TAG"

echo -e "${GREEN}Tag created successfully${NC}"
echo ""
echo "To push tag to remote:"
echo "  git push origin $NEW_TAG"
echo ""
echo "To push all changes and tag:"
echo "  git push origin main && git push origin $NEW_TAG"
echo ""
echo "This will trigger the deployment workflow in CI/CD"
echo ""
