#!/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', 'seed', 'seed-test', '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"
                ;;
            "seed")
                print_status "Seeding canonical full environment..."
                python3 main.py --mode seed || {
                    print_error "Seed failed!"
                    exit 1
                }
                print_success "Seed completed"
                ;;
            "seed-test")
                print_status "Seeding lightweight test environment..."
                python3 main.py --mode seed-test || {
                    print_error "Seed test failed!"
                    exit 1
                }
                print_success "Seed test 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"
                ;;
            "exam-corpus")
                print_status "Seeding exam-paper corpus (manifest-gated; skips if none configured)..."
                python3 main.py --mode exam-corpus || {
                    print_error "Exam corpus seed failed!"
                    exit 1
                }
                print_success "Exam corpus seed completed"
                ;;
            "full")
                print_status "Running full initialization..."
                python3 main.py --mode infra || exit 1
                python3 main.py --mode seed || 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 server (unless init-only mode). Default remains production, but
# development Compose can set START_MODE=dev so cc-api-dev reports and uses
# development Redis/config instead of silently booting as prod.
START_MODE="${START_MODE:-prod}"
if [ "$1" != "init-only" ] && [ -z "$INIT_ONLY" ]; then
    print_status "Starting ${START_MODE} server..."
    exec ./start.sh "$START_MODE"
else
    print_status "Init-only mode - not starting server"
fi
