#!/bin/bash

# Rustelo Build Examples Script
# This script demonstrates building the application with different feature combinations

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_color() {
    printf "${1}${2}${NC}\n"
}

# Function to print section header
print_section() {
    echo ""
    print_color "$BLUE" "================================================"
    print_color "$BLUE" "$1"
    print_color "$BLUE" "================================================"
}

# Function to build with features
build_with_features() {
    local name="$1"
    local features="$2"
    local description="$3"

    print_color "$YELLOW" "Building: $name"
    print_color "$YELLOW" "Features: $features"
    print_color "$YELLOW" "Description: $description"

    if [ -z "$features" ]; then
        cargo build --no-default-features --release
    else
        cargo build --release --features "$features"
    fi

    if [ $? -eq 0 ]; then
        print_color "$GREEN" "✓ Build successful"

        # Get binary size
        local binary_size=$(du -h target/release/server | cut -f1)
        print_color "$GREEN" "Binary size: $binary_size"
    else
        print_color "$RED" "✗ Build failed"
        exit 1
    fi

    echo ""
}

# Function to clean build artifacts
clean_build() {
    print_color "$YELLOW" "Cleaning build artifacts..."
    cargo clean
    print_color "$GREEN" "✓ Clean complete"
}

# Function to show help
show_help() {
    echo "Usage: $0 [OPTIONS]"
    echo ""
    echo "Options:"
    echo "  -h, --help     Show this help message"
    echo "  -c, --clean    Clean build artifacts first"
    echo "  -a, --all      Build all example configurations"
    echo "  -m, --minimal  Build minimal configuration only"
    echo "  -f, --full     Build full-featured configuration only"
    echo "  -p, --prod     Build production configuration only"
    echo "  -q, --quick    Build common configurations only"
    echo ""
    echo "Examples:"
    echo "  $0 --all       Build all configurations"
    echo "  $0 --minimal   Build minimal setup"
    echo "  $0 --clean     Clean and build all"
}

# Parse command line arguments
BUILD_ALL=false
BUILD_MINIMAL=false
BUILD_FULL=false
BUILD_PROD=false
BUILD_QUICK=false
CLEAN_FIRST=false

while [[ $# -gt 0 ]]; do
    case $1 in
        -h|--help)
            show_help
            exit 0
            ;;
        -c|--clean)
            CLEAN_FIRST=true
            shift
            ;;
        -a|--all)
            BUILD_ALL=true
            shift
            ;;
        -m|--minimal)
            BUILD_MINIMAL=true
            shift
            ;;
        -f|--full)
            BUILD_FULL=true
            shift
            ;;
        -p|--prod)
            BUILD_PROD=true
            shift
            ;;
        -q|--quick)
            BUILD_QUICK=true
            shift
            ;;
        *)
            print_color "$RED" "Unknown option: $1"
            show_help
            exit 1
            ;;
    esac
done

# Default to build all if no specific option provided
if [ "$BUILD_ALL" = false ] && [ "$BUILD_MINIMAL" = false ] && [ "$BUILD_FULL" = false ] && [ "$BUILD_PROD" = false ] && [ "$BUILD_QUICK" = false ]; then
    BUILD_ALL=true
fi

# Clean build artifacts if requested
if [ "$CLEAN_FIRST" = true ]; then
    clean_build
fi

print_section "Rustelo Build Examples"

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

# Build configurations
if [ "$BUILD_MINIMAL" = true ] || [ "$BUILD_ALL" = true ]; then
    print_section "1. MINIMAL CONFIGURATION"
    build_with_features "Minimal Static Website" "" "Basic Leptos SSR with static content only"
fi

if [ "$BUILD_QUICK" = true ] || [ "$BUILD_ALL" = true ]; then
    print_section "2. TLS ONLY CONFIGURATION"
    build_with_features "Secure Static Website" "tls" "Static website with HTTPS support"
fi

if [ "$BUILD_QUICK" = true ] || [ "$BUILD_ALL" = true ]; then
    print_section "3. AUTHENTICATION ONLY CONFIGURATION"
    build_with_features "Authentication App" "auth" "User authentication without database content"
fi

if [ "$BUILD_QUICK" = true ] || [ "$BUILD_ALL" = true ]; then
    print_section "4. CONTENT MANAGEMENT ONLY CONFIGURATION"
    build_with_features "Content Management System" "content-db" "Database-driven content without authentication"
fi

if [ "$BUILD_FULL" = true ] || [ "$BUILD_ALL" = true ]; then
    print_section "5. FULL-FEATURED CONFIGURATION (DEFAULT)"
    build_with_features "Complete Web Application" "auth,content-db" "Authentication + Content Management"
fi

if [ "$BUILD_PROD" = true ] || [ "$BUILD_ALL" = true ]; then
    print_section "6. PRODUCTION CONFIGURATION"
    build_with_features "Production Ready" "tls,auth,content-db" "All features with TLS for production"
fi

if [ "$BUILD_ALL" = true ]; then
    print_section "7. SPECIALIZED CONFIGURATIONS"

    build_with_features "TLS + Auth" "tls,auth" "Secure authentication app"
    build_with_features "TLS + Content" "tls,content-db" "Secure content management"
fi

print_section "BUILD SUMMARY"

# Show final binary sizes comparison
print_color "$GREEN" "Build completed successfully!"
print_color "$BLUE" "Binary location: target/release/server"

if [ -f "target/release/server" ]; then
    print_color "$BLUE" "Final binary size: $(du -h target/release/server | cut -f1)"
fi

print_color "$YELLOW" "Next steps:"
echo "1. Choose your configuration based on your needs"
echo "2. Set up your .env file with appropriate settings"
echo "3. Configure database if using auth or content-db features"
echo "4. Run: ./target/release/server"

print_section "CONFIGURATION QUICK REFERENCE"

echo "Minimal (no database needed):"
echo "  cargo build --release --no-default-features"
echo ""
echo "With TLS (requires certificates):"
echo "  cargo build --release --features tls"
echo ""
echo "With Authentication (requires database):"
echo "  cargo build --release --features auth"
echo ""
echo "With Content Management (requires database):"
echo "  cargo build --release --features content-db"
echo ""
echo "Full Featured (default):"
echo "  cargo build --release"
echo ""
echo "Production (all features):"
echo "  cargo build --release --features \"tls,auth,content-db\""

print_color "$GREEN" "Build examples completed!"
