#!/bin/bash
#
# Comprehensive Version Update Script for Media Downloader
# Updates ALL version references across the entire codebase
#
# Usage: ./scripts/update-all-versions.sh <new_version>
# Example: ./scripts/update-all-versions.sh 6.11.0
#

set -e

if [ -z "$1" ]; then
    echo "Error: Version number required"
    echo "Usage: $0 <version>"
    echo "Example: $0 6.11.0"
    exit 1
fi

NEW_VERSION="$1"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"

echo "╔════════════════════════════════════════════════╗"
echo "║   Media Downloader Version Update              ║"
echo "╠════════════════════════════════════════════════╣"
echo "║ New Version: $NEW_VERSION"
echo "╚════════════════════════════════════════════════╝"
echo ""

# Read current version
CURRENT_VERSION=$(cat "$PROJECT_ROOT/VERSION" 2>/dev/null || echo "unknown")
echo "Current version: $CURRENT_VERSION"
echo "New version: $NEW_VERSION"
echo ""

# Function to update file with sed
update_file() {
    local file=$1
    local pattern=$2
    local replacement=$3
    local description=$4
    
    if [ -f "$file" ]; then
        sed -i "$pattern" "$file"
        echo "✓ Updated: $description"
    else
        echo "⚠ Skipped: $file (not found)"
    fi
}

echo "Updating version files..."
echo "─────────────────────────────────────────────────"

# 1. VERSION file
echo "$NEW_VERSION" > "$PROJECT_ROOT/VERSION"
echo "✓ Updated: VERSION file"

# 2. README.md
update_file "$PROJECT_ROOT/README.md" \
    "s/\*\*Version:\*\* [0-9.]\+/**Version:** $NEW_VERSION/g" \
    "README.md (header)"

update_file "$PROJECT_ROOT/README.md" \
    "s/VERSION.*# Version number ([0-9.]\+)/VERSION                       # Version number ($NEW_VERSION)/g" \
    "README.md (directory structure comment)"

# 3. Frontend files
echo ""
echo "Updating frontend files..."
echo "─────────────────────────────────────────────────"

update_file "$PROJECT_ROOT/web/frontend/src/pages/Login.tsx" \
    "s/<p>v[0-9.]\+<\/p>/<p>v$NEW_VERSION<\/p>/g" \
    "Login.tsx"

update_file "$PROJECT_ROOT/web/frontend/src/App.tsx" \
    "s/v[0-9.]\+<\/p>/v$NEW_VERSION<\/p>/g" \
    "App.tsx (all occurrences)"

update_file "$PROJECT_ROOT/web/frontend/src/pages/Configuration.tsx" \
    "s/Version [0-9.]\+/Version $NEW_VERSION/g" \
    "Configuration.tsx"

update_file "$PROJECT_ROOT/web/frontend/src/pages/Configuration.tsx" \
    "s/v[0-9.]\+)/v$NEW_VERSION)/g" \
    "Configuration.tsx (comments)"

update_file "$PROJECT_ROOT/web/frontend/package.json" \
    "s/\"version\": \"[0-9.]\+\"/\"version\": \"$NEW_VERSION\"/g" \
    "package.json"

# 4. Backend API version
echo ""
echo "Updating backend files..."
echo "─────────────────────────────────────────────────"

update_file "$PROJECT_ROOT/web/backend/api.py" \
    "s/version=\"[0-9.]\+\"/version=\"$NEW_VERSION\"/g" \
    "api.py"

update_file "$PROJECT_ROOT/web/backend/core/config.py" \
    "s/API_VERSION: str = \"[0-9.]\+\"/API_VERSION: str = \"$NEW_VERSION\"/g" \
    "core/config.py (API_VERSION)"

# 5. Installer script
echo ""
echo "Updating installer..."
echo "─────────────────────────────────────────────────"

update_file "$PROJECT_ROOT/scripts/install.sh" \
    "s/# Version: [0-9.]\+/# Version: $NEW_VERSION/g" \
    "install.sh (header comment)"

update_file "$PROJECT_ROOT/scripts/install.sh" \
    "s/Installer v[0-9.]\+/Installer v$NEW_VERSION/g" \
    "install.sh (banner)"

echo ""
echo "╔════════════════════════════════════════════════╗"
echo "║            Version Update Complete             ║"
echo "╠════════════════════════════════════════════════╣"
echo "║ Version: $NEW_VERSION"
echo "║ Files Updated: 10"
echo "╚════════════════════════════════════════════════╝"
echo ""
echo "Next steps:"
echo "1. Update data/changelog.json with new version"
echo "2. Update docs/CHANGELOG.md with release notes"
echo "3. Rebuild frontend (automatically if dev server running)"
echo "4. Run: ./scripts/create-version-backup.sh"
echo ""
