#!/bin/bash

# increment_and_push.sh - Version increment and git automation script
# Usage: ./increment_and_push.sh "meaningful git comment"

set -e  # Exit on error

# Color constants
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[34m'
BOLD='\033[1m'
RESET='\033[0m'

# Function to print output with colors
print_info() {
    if [ "$USE_COLORS" = true ]; then
        echo -e "${BLUE}[INFO]${RESET} $1"
    else
        echo "[INFO] $1"
    fi
}

print_success() {
    if [ "$USE_COLORS" = true ]; then
        echo -e "${GREEN}${BOLD}[SUCCESS]${RESET} $1"
    else
        echo "[SUCCESS] $1"
    fi
}

print_warning() {
    if [ "$USE_COLORS" = true ]; then
        echo -e "${YELLOW}[WARNING]${RESET} $1"
    else
        echo "[WARNING] $1"
    fi
}

print_error() {
    if [ "$USE_COLORS" = true ]; then
        echo -e "${RED}${BOLD}[ERROR]${RESET} $1"
    else
        echo "[ERROR] $1"
    fi
}

# Check if we're in the correct directory
CURRENT_DIR=$(basename "$(pwd)")
if [ "$CURRENT_DIR" != "nostr_core_lib" ]; then
    print_error "Script must be run from the nostr_core_lib directory"
    echo ""
    echo "Current directory: $CURRENT_DIR"
    echo "Expected directory: nostr_core_lib"
    echo ""
    echo "Please change to the nostr_core_lib directory first."
    echo ""
    exit 1
fi

# Check if git repository exists
if ! git rev-parse --git-dir > /dev/null 2>&1; then
    print_error "Not a git repository. Please initialize git first."
    exit 1
fi

# Check if we have a commit message
if [ $# -eq 0 ]; then
    print_error "Usage: $0 \"meaningful git comment\""
    echo ""
    echo "Example: $0 \"Add enhanced subscription functionality\""
    echo ""
    exit 1
fi

COMMIT_MESSAGE="$1"

# Check if nostr_core.h exists
if [ ! -f "nostr_core/nostr_core.h" ]; then
    print_error "nostr_core/nostr_core.h not found"
    exit 1
fi

print_info "Starting version increment and push process..."

# Extract current version from nostr_core.h
CURRENT_VERSION=$(grep '#define VERSION ' nostr_core/nostr_core.h | cut -d'"' -f2)
if [ -z "$CURRENT_VERSION" ]; then
    print_error "Could not find VERSION define in nostr_core.h"
    exit 1
fi

# Extract version components
VERSION_MAJOR=$(grep '#define VERSION_MAJOR ' nostr_core/nostr_core.h | awk '{print $3}')
VERSION_MINOR=$(grep '#define VERSION_MINOR ' nostr_core/nostr_core.h | awk '{print $3}')
VERSION_PATCH=$(grep '#define VERSION_PATCH ' nostr_core/nostr_core.h | awk '{print $3}')

if [ -z "$VERSION_MAJOR" ] || [ -z "$VERSION_MINOR" ] || [ -z "$VERSION_PATCH" ]; then
    print_error "Could not extract version components from nostr_core.h"
    exit 1
fi

print_info "Current version: $CURRENT_VERSION (Major: $VERSION_MAJOR, Minor: $VERSION_MINOR, Patch: $VERSION_PATCH)"

# Increment patch version
NEW_PATCH=$((VERSION_PATCH + 1))
NEW_VERSION="v$VERSION_MAJOR.$VERSION_MINOR.$NEW_PATCH"

print_info "New version will be: $NEW_VERSION"

# Update version in nostr_core.h
sed -i "s/#define VERSION .*/#define VERSION \"$NEW_VERSION\"/" nostr_core/nostr_core.h
sed -i "s/#define VERSION_PATCH .*/#define VERSION_PATCH $NEW_PATCH/" nostr_core/nostr_core.h

print_success "Updated version in nostr_core.h"

# Check if VERSION file exists and update it
if [ -f "VERSION" ]; then
    echo "$VERSION_MAJOR.$VERSION_MINOR.$NEW_PATCH" > VERSION
    print_success "Updated VERSION file"
fi

# Check git status
if ! git diff --quiet; then
    print_info "Adding changes to git..."
    git add .

    print_info "Committing changes..."
    git commit -m "$COMMIT_MESSAGE"

    print_success "Changes committed"
else
    print_warning "No changes to commit"
fi

# Create and push git tag
print_info "Creating git tag: $NEW_VERSION"
git tag "$NEW_VERSION"

print_info "Pushing commits and tags..."
CURRENT_BRANCH=$(git branch --show-current)
git push origin "$CURRENT_BRANCH"
git push origin "$NEW_VERSION"

print_success "Version $NEW_VERSION successfully released!"
print_info "Git commit: $COMMIT_MESSAGE"
print_info "Tag: $NEW_VERSION"

echo ""
echo "🎉 Release complete! Version $NEW_VERSION is now live."