#!/usr/bin/env nu

# Start the provisioning orchestrator
def main [
    --port (-p): int = 9090        # HTTP server port
    --data-dir (-d): string = "./data"  # Data directory for persistence
    --nu-path: string = "nu"       # Path to Nushell executable
    --provisioning-path: string = "../../core/nulib/provisioning"  # Path to provisioning script
    --background (-b)              # Run in background
    --pid-file: string = "./data/orchestrator.pid"  # PID file location
    --log-file: string = "./data/orchestrator.log"  # Log file location
    --check                        # Check if orchestrator is running
    --stop                         # Stop the orchestrator
] {
    if $check {
        check_orchestrator_status $pid_file $port
        return
    }

    if $stop {
        stop_orchestrator $pid_file
        return
    }

    # Always stop any running orchestrator instances before starting
    print "🔍 Checking for existing orchestrator processes..."
    let existing_processes = (ps | where name =~ "provisioning-orchestrator" | length)
    if $existing_processes > 0 {
        print $"🛑 Found ($existing_processes) existing orchestrator processes. Stopping them..."
        let result = (do {
            bash -c 'pkill -f "provisioning-orchestrator" && echo "✅ Existing orchestrator stopped" || echo "⚠️  No orchestrator process found"'
            sleep 2sec
        } | complete)

        if $result.exit_code != 0 {
            print "⚠️  Could not stop existing orchestrator processes"
        }
    } else {
        print "✅ No existing orchestrator processes found"
    }

    # Clean up stale PID file if it exists
    if ($pid_file | path exists) {
        print "🧹 Removing stale PID file"
        rm -f $pid_file
    }

    let orchestrator_bin = "../target/release/provisioning-orchestrator"

    if not ($orchestrator_bin | path exists) {
        print "🛑 Orchestrator binary not found. Building..."
        cd ..; cargo build --release --bin provisioning-orchestrator; cd orchestrator

        if not ($orchestrator_bin | path exists) {
            print "❌ Failed to build orchestrator"
            exit 1
        }
    }

    if $background {
        start_orchestrator_background $orchestrator_bin $port $data_dir $nu_path $provisioning_path $pid_file $log_file
    } else {
        start_orchestrator_foreground $orchestrator_bin $port $data_dir $nu_path $provisioning_path
    }
}

def start_orchestrator_foreground [
    bin_path: string
    port: int
    data_dir: string
    nu_path: string
    provisioning_path: string
] {
    print $"🚀 Starting orchestrator on port ($port)..."
    print $"📁 Data directory: ($data_dir)"
    print $"🐚 Nushell path: ($nu_path)"
    print $"📜 Provisioning script: ($provisioning_path)"
    print ""

    ^$bin_path --port $port --data-dir $data_dir --nu-path $nu_path --provisioning-path $provisioning_path
}

def start_orchestrator_background [
    bin_path: string
    port: int
    data_dir: string
    nu_path: string
    provisioning_path: string
    pid_file: string
    log_file: string
] {
    # PID file cleanup and process termination already handled in main function

    mkdir ($data_dir | path dirname)
    mkdir ($log_file | path dirname)

    print $"🚀 Starting orchestrator in background..."
    print $"📁 Data directory: ($data_dir)"
    print $"📄 Log file: ($log_file)"
    print $"🆔 PID file: ($pid_file)"

    # Start in background and capture PID
    let cmd = $"($bin_path) --port ($port) --data-dir '($data_dir)' --nu-path '($nu_path)' --provisioning-path '($provisioning_path)' >> '($log_file)' 2>&1 & echo $!"

    let pid = (bash -c $cmd | str trim)
    $pid | save $pid_file

    sleep 1sec

    # Verify it started
    if (ps | where pid == ($pid | into int) | length) > 0 {
        print $"✅ Orchestrator started successfully with PID ($pid)"
        print $"🌐 Health check: http://localhost:($port)/health"
        print $"📊 API docs: http://localhost:($port)/tasks"
        print ""
        print $"To stop: ($env.CURRENT_FILE) --stop"
        print $"To check status: ($env.CURRENT_FILE) --check"
        print $"To view logs: tail -f ($log_file)"
    } else {
        print "❌ Failed to start orchestrator"
        if ($log_file | path exists) {
            print "📋 Last few log lines:"
            tail $log_file
        }
        exit 1
    }
}

def stop_orchestrator [pid_file: string] {
    if not ($pid_file | path exists) {
        print "⚠️  No PID file found - orchestrator may not be running"
        return
    }

    let pid = (open $pid_file | str trim)

    if (ps | where pid == ($pid | into int) | length) == 0 {
        print $"⚠️  Process with PID ($pid) not found"
        rm -f $pid_file
        return
    }

    print $"🛑 Stopping orchestrator (PID: ($pid))..."

    let result = (do {
        kill $pid
        sleep 2sec

        if (ps | where pid == ($pid | into int) | length) > 0 {
            print "🔨 Force killing..."
            kill -9 $pid
            sleep 1sec
        }

        rm -f $pid_file
        print "✅ Orchestrator stopped successfully"
    } | complete)

    if $result.exit_code != 0 {
        print $"❌ Failed to stop process ($pid)"
        exit 1
    }
}

def check_orchestrator_status [pid_file: string, port: int = 9090] {
    if not ($pid_file | path exists) {
        print "❌ Orchestrator is not running (no PID file)"
        return
    }

    let pid = (open $pid_file | str trim)

    if (ps | where pid == ($pid | into int) | length) == 0 {
        print $"❌ Orchestrator is not running (PID ($pid) not found)"
        rm -f $pid_file
        return
    }

    print $"✅ Orchestrator is running with PID ($pid)"

    # Health check
    let health_result = (do {
        http get $"http://localhost:($port)/health"
    } | complete)

    if $health_result.exit_code == 0 {
        let health = ($health_result.stdout | from json)
        if ($health | get success) {
            print "🌐 Health check: OK"
            print $"📊 Status: (($health | get data))"
        } else {
            print "⚠️  Health check failed"
        }
    } else {
        print "⚠️  Could not connect to orchestrator API"
    }
}
