#!/bin/bash

# Rustelo Configuration Wizard Runner
# This script runs the configuration wizard to generate config.toml and update Cargo.toml

set -e

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

# Function to print colored output
print_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# Check if we're in the right directory
if [ ! -f "Cargo.toml" ]; then
    print_error "Cargo.toml not found. Please run this script from the project root."
    exit 1
fi

# Check if server directory exists
if [ ! -d "server" ]; then
    print_error "server directory not found. Please run this script from the project root."
    exit 1
fi

print_info "Starting Rustelo Configuration Wizard..."

# Create backup of existing config files
if [ -f "config.toml" ]; then
    print_warning "Backing up existing config.toml to config.toml.bak"
    cp config.toml config.toml.bak
fi

if [ -f "server/Cargo.toml" ]; then
    print_warning "Backing up existing server/Cargo.toml to server/Cargo.toml.bak"
    cp server/Cargo.toml server/Cargo.toml.bak
fi

# Check if Rhai is available and run the appropriate wizard
if grep -q "rhai" server/Cargo.toml; then
    print_info "Rhai scripting engine detected. Running advanced wizard..."

    # Check if the Rhai script exists
    if [ -f "scripts/config_wizard.rhai" ]; then
        print_info "Running Rhai-based configuration wizard..."
        print_info "loading ..."
        cd server && cargo run --bin config_wizard --quiet
    else
        print_warning "Rhai script not found. Falling back to simple wizard..."
        cd server && cargo run --bin simple_config_wizard --quiet
    fi
else
    print_info "Running simple configuration wizard..."
    cd server && cargo run --bin simple_config_wizard --quiet
fi

if [ $? -eq 0 ]; then
    print_success "Configuration wizard completed successfully!"

    # Verify the generated files
    if [ -f "config.toml" ]; then
        print_success "config.toml generated successfully"
    else
        print_error "config.toml was not generated"
    fi

    # Show what was generated
    print_info "Generated configuration summary:"
    echo "================================"

    if [ -f "config.toml" ]; then
        echo "📄 config.toml:"
        head -n 20 config.toml
        echo "..."
        echo ""
    fi

    if [ -f "server/Cargo.toml" ]; then
        echo "📦 Default features in server/Cargo.toml:"
        grep -A 5 "default = \[" server/Cargo.toml || echo "Default features not found"
        echo ""
    fi

    # Optional: Run a quick validation
    print_info "Validating configuration..."

    # Check if the project builds with the selected features
    if cd server && cargo check --quiet; then
        print_success "Project builds successfully with selected features!"
    else
        print_warning "Project build check failed. You may need to review the configuration."
    fi
    cd ..

    echo ""
    print_info "Next steps:"
    echo "1. Review the generated config.toml file"
    echo "2. Update any placeholder values (like OAuth secrets, database URLs, etc.)"
    echo "3. Set environment variables for sensitive data"
    echo "4. Run 'cargo build' to build with the selected features"
    echo "5. Start your application with 'cargo run'"
    echo ""
    print_info "Configuration files backed up with .bak extension"

else
    print_error "Configuration wizard failed!"

    # Restore backups if they exist
    if [ -f "config.toml.bak" ]; then
        print_info "Restoring config.toml from backup..."
        mv config.toml.bak config.toml
    fi

    if [ -f "server/Cargo.toml.bak" ]; then
        print_info "Restoring server/Cargo.toml from backup..."
        mv server/Cargo.toml.bak server/Cargo.toml
    fi

    exit 1
fi
