#!/bin/bash
# DanceLessonsCoach Version Bump Script
# Usage: ./scripts/version-bump.sh [major|minor|patch|pre|release]

set -e

# Load current version
VERSION_FILE="VERSION"
if [ ! -f "$VERSION_FILE" ]; then
    echo "❌ Version file not found: $VERSION_FILE"
    exit 1
fi

# Source version variables
source "$VERSION_FILE"

# Validate bump type
BUMP_TYPE="${1:-patch}"
case "$BUMP_TYPE" in
    major|minor|patch|pre|release)
        echo "📋 Bumping version: $BUMP_TYPE"
        ;;
    *)
        echo "❌ Invalid bump type: $BUMP_TYPE"
        echo "Usage: $0 [major|minor|patch|pre|release]"
        exit 1
        ;;
esac

# Bump version function
bump_version() {
    local type="$1"
    
    case "$type" in
        major)
            MAJOR=$((MAJOR + 1))
            MINOR=0
            PATCH=0
            PRERELEASE=""
            ;;
        minor)
            MINOR=$((MINOR + 1))
            PATCH=0
            PRERELEASE=""
            ;;
        patch)
            PATCH=$((PATCH + 1))
            PRERELEASE=""
            ;;
        pre)
            if [ -z "$PRERELEASE" ]; then
                PRERELEASE="alpha.1"
            else
                # Extract number and increment
                if [[ "$PRERELEASE" =~ ^(alpha|beta|rc)\.([0-9]+)$ ]]; then
                    PRE_TYPE="${BASH_REMATCH[1]}"
                    PRE_NUM="${BASH_REMATCH[2]}"
                    PRERELEASE="$PRE_TYPE.$((PRE_NUM + 1))"
                else
                    PRERELEASE="alpha.1"
                fi
            fi
            ;;
        release)
            PRERELEASE=""
            ;;
    esac
}

# Bump the version
bump_version "$BUMP_TYPE"

# Calculate new version
NEW_VERSION="$MAJOR.$MINOR.$PATCH"
if [ -n "$PRERELEASE" ]; then
    NEW_VERSION="$NEW_VERSION-$PRERELEASE"
fi

echo "🔢 Current version: $MAJOR.$MINOR.$PATCH${PRERELEASE:+-$PRERELEASE}"
echo "🔜 New version: $NEW_VERSION"

# Update VERSION file
cat > "$VERSION_FILE" << VERSION_EOF
# DanceLessonsCoach Version

# Current Version (Semantic Versioning)
MAJOR=$MAJOR
MINOR=$MINOR
PATCH=$PATCH
PRERELEASE="$PRERELEASE"

# Auto-generated fields (do not edit manually)
BUILD_DATE=""
GIT_COMMIT=""
GIT_TAG=""

# Version Format: {MAJOR}.{MINOR}.{PATCH}-{PRERELEASE}
# Example: 1.0.0, 1.0.0-alpha.1, 2.3.4-beta.2

# Semantic Versioning Rules:
# - MAJOR: Breaking changes, major features
# - MINOR: Backwards-compatible features
# - PATCH: Backwards-compatible bug fixes
# - PRERELEASE: alpha, beta, rc (pre-release versions)

# Changelog Reference:
# See AGENT_CHANGELOG.md for version history
VERSION_EOF

echo "✅ Updated VERSION file"

# Update main.go Swagger version (cross-platform)
MAIN_GO="cmd/server/main.go"
if [ -f "$MAIN_GO" ]; then
    # Create temporary file
    TMP_FILE=$(mktemp)
    
    # Use awk for cross-platform sed replacement
    awk -v new_version="$NEW_VERSION" '
    {
        if ($0 ~ /^\/\/ @version [0-9.]+/) {
            print "// @version " new_version
        } else {
            print $0
        }
    }' "$MAIN_GO" > "$TMP_FILE"
    
    # Replace original file
    mv "$TMP_FILE" "$MAIN_GO"
    echo "✅ Updated Swagger version in main.go"
fi

# Update README.md version badge
README_MD="README.md"
if [ -f "$README_MD" ]; then
    # Create temporary file
    TMP_FILE=$(mktemp)
    
    # Use awk to update version badge
    awk -v new_version="$NEW_VERSION" '{
        if ($0 ~ /Version.*badge.*version/) {
            print "[![Version](https://img.shields.io/badge/version-" new_version "-blue.svg)](https://gitea.arcodange.fr/arcodange/DanceLessonsCoach/releases)"
        } else {
            print $0
        }
    }' "$README_MD" > "$TMP_FILE"
    
    # Replace original file
    mv "$TMP_FILE" "$README_MD"
    echo "✅ Updated version badge in README.md"
fi

echo "🎉 Version bump complete: $NEW_VERSION"
