#!/bin/bash
set -e

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

echo "🚀 Django FileIndex Release Script"
echo "=================================="

# Check for uncommitted changes
if ! git diff-index --quiet HEAD --; then
    echo -e "${RED}❌ Error: You have uncommitted changes. Please commit or stash them first.${NC}"
    exit 1
fi

# Get current version from hatch
CURRENT_VERSION=$(uv run hatch version)
echo -e "Current version: ${YELLOW}$CURRENT_VERSION${NC}"

# Bump version using hatch (patch by default)
echo "Bumping version (patch)..."
uv run hatch version patch

# Get new version from hatch
NEW_VERSION=$(uv run hatch version)
echo -e "New version: ${GREEN}$NEW_VERSION${NC}"

# Commit the version change
echo "Committing version bump..."
git add fileindex/__init__.py
git commit -m "Release v$NEW_VERSION"

# Create and push tag
TAG="v$NEW_VERSION"
echo "Creating tag $TAG..."
git tag -a "$TAG" -m "Release version $NEW_VERSION"

echo -e "\n${GREEN}✅ Release prepared successfully!${NC}"
echo -e "\nTo publish this release, run:"
echo -e "  ${YELLOW}git push origin main${NC}"
echo -e "  ${YELLOW}git push origin $TAG${NC}"
echo -e "\nOr push everything at once:"
echo -e "  ${YELLOW}git push origin main --tags${NC}"
