#!/bin/bash
# Production entrypoint script for ClassroomCopilot API
# Supports running initialization tasks before starting the server

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

print_status() {
    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 should run initialization
RUN_INIT="${RUN_INIT:-false}"
INIT_MODE="${INIT_MODE:-infra}"  # Default to 'infra', can be 'infra', 'full', or comma-separated list

# If RUN_INIT is true, run initialization tasks
if [ "$RUN_INIT" = "true" ]; then
    print_status "Running initialization tasks (mode: $INIT_MODE)..."
    
    # Split INIT_MODE by comma if it contains multiple modes
    IFS=',' read -ra MODES <<< "$INIT_MODE"
    
    for mode in "${MODES[@]}"; do
        mode=$(echo "$mode" | xargs)  # Trim whitespace
        print_status "Running initialization mode: $mode"
        
        case "$mode" in
            "infra")
                print_status "Setting up infrastructure (Neo4j schema, calendar, Supabase buckets)..."
                python3 main.py --mode infra || {
                    print_error "Infrastructure setup failed!"
                    exit 1
                }
                print_success "Infrastructure setup completed"
                ;;
            "demo-school")
                print_status "Creating demo school..."
                python3 main.py --mode demo-school || {
                    print_error "Demo school creation failed!"
                    exit 1
                }
                print_success "Demo school creation completed"
                ;;
            "demo-users")
                print_status "Creating demo users..."
                python3 main.py --mode demo-users || {
                    print_error "Demo users creation failed!"
                    exit 1
                }
                print_success "Demo users creation completed"
                ;;
            "gais-data")
                print_status "Importing GAIS data..."
                python3 main.py --mode gais-data || {
                    print_error "GAIS data import failed!"
                    exit 1
                }
                print_success "GAIS data import completed"
                ;;
            "full")
                print_status "Running full initialization..."
                python3 main.py --mode infra || exit 1
                python3 main.py --mode demo-school || exit 1
                python3 main.py --mode demo-users || exit 1
                python3 main.py --mode gais-data || exit 1
                print_success "Full initialization completed"
                ;;
            *)
                print_warning "Unknown initialization mode: $mode (skipping)"
                ;;
        esac
    done
    
    print_success "All initialization tasks completed"
    
    # If this is the init service (not backend), exit after init
    if [ "$1" = "init-only" ] || [ -n "$INIT_ONLY" ]; then
        print_status "Initialization complete - exiting (init-only mode)"
        exit 0
    fi
fi

# Start the production server (unless init-only mode)
if [ "$1" != "init-only" ] && [ -z "$INIT_ONLY" ]; then
    print_status "Starting production server..."
    exec ./start.sh prod
else
    print_status "Init-only mode - not starting server"
fi

