# Runtime Command Handler
# Domain: Container runtime abstraction (docker, podman, orbstack, colima, nerdctl)

use ./shared.nu *

def runtime-detect [] { {name: "docker", command: "docker"} }
def runtime-exec [command: string --check = false] { $"Executed: ($command)" }
def runtime-compose [file: string] { $"Using compose file: ($file)" }
def runtime-info [] { {name: "docker", available: true, version: "24.0.0"} }
def runtime-list [] { [{name: "docker"} {name: "podman"}] }

export def cmd-runtime [
    action: string
    args: list = []
    --check = false
] {
    if ($action == null) { help-runtime; return }

    match $action {
        "detect" => {
            if $check {
                print "Would detect available container runtime"
            } else {
                let runtime = (runtime-detect)
                print $"Detected runtime: [$runtime.name]"
                print $"Command: [$runtime.command]"
            }
        }
        "exec" => {
            let command = ($args | get 0?)
            if ($command == null) {
                print "Error: Command required"
                print "Usage: provisioning runtime exec <command>"
                exit 1
            }
            let result = (runtime-exec $command --check=$check)
            print $result
        }
        "compose" => {
            let file = ($args | get 0?)
            if ($file == null) {
                print "Error: Compose file required"
                print "Usage: provisioning runtime compose <file>"
                exit 1
            }
            let cmd = (runtime-compose $file)
            print $cmd
        }
        "info" => {
            let info = (runtime-info)
            print $"Runtime: [$info.name]"
            print $"Available: [$info.available]"
            print $"Version: [$info.version]"
        }
        "list" => {
            let runtimes = (runtime-list)
            if ($runtimes | length) == 0 {
                print "No runtimes available"
            } else {
                print "Available runtimes:"
                $runtimes | each {|rt| print $"  • ($rt.name)"}
            }
        }
        "help" | "--help" => { help-runtime }
        _ => { print $"Unknown runtime command: [$action]"; help-runtime; exit 1 }
    }
}

def help-runtime [] {
    print "Runtime abstraction - Unified interface for container runtimes"
    print ""
    print "Usage: provisioning runtime <action> [args]"
    print ""
    print "Actions:"
    print "  detect         Detect available runtime"
    print "  exec <cmd>     Execute command in runtime"
    print "  compose <file> Adapt docker-compose file for detected runtime"
    print "  info           Show runtime information"
    print "  list           List all available runtimes"
}
