# Utilities Command Dispatcher
# Routes utility commands to appropriate domain-specific handlers
# NUSHELL 0.109 COMPLIANT - All handlers properly exported

use ./ssh.nu *
use ./sops.nu *
use ./cache.nu *
use ./providers.nu *
use ./plugins.nu *
use ./shell.nu *
use ./guides.nu *
use ./qr.nu *
use ./alias.nu *

# Main utility command dispatcher - Routes to appropriate domain handler
export def handle_utility_command [
    command: string
    ops: string
    flags: record
] {
    match $command {
        # Alias table (default: list)
        "alias" => {
            let action = ($ops | split row " " | first | default "list")
            match $action {
                "list" | "l" | "ls" | "" => { alias-list }
                _ => { alias-list }
            }
        }

        # SSH operations
        "ssh" => { handle_ssh $flags }

        # SOPS file editing (sed is alias)
        "sed" | "sops" => { handle_sops_edit $command $ops $flags }

        # Cache management
        "cache" => { handle_cache $ops $flags }

        # Provider management
        "providers" => { handle_providers $ops $flags }

        # Plugin management
        "plugin" | "plugins" => { handle_plugins $ops $flags }

        # Shell operations (nu, nuinfo, list)
        "nu" => { handle_nu $ops $flags }
        "nuinfo" => { handle_nuinfo }
        "list" | "l" | "ls" => { handle_list $ops $flags }

        # Guide system
        "guide" | "guides" | "howto" => { handle_guide $ops $flags }

        # QR code generation
        "qr" => { handle_qr }

        # Unknown command
        _ => {
            print $"❌ Unknown utility command: ($command)"
            print ""
            print "Available utility commands:"
            print "  ssh       - SSH into server"
            print "  sed       - Edit SOPS encrypted files (alias)"
            print "  sops      - Edit SOPS encrypted files"
            print "  cache     - Cache management (status, config, clear, list)"
            print "  providers - List available providers"
            print "  nu        - Start Nushell with provisioning library loaded"
            print "  list      - List resources (servers, taskservs, clusters)"
            print "  qr        - Generate QR code"
            print "  nuinfo    - Show Nushell version info"
            print "  plugin    - Plugin management (list, register, test, status)"
            print "  guide     - Show interactive guides (from-scratch, update, customize)"
            print ""
            print "Use 'provisioning help utilities' for more details"
            exit 1
        }
    }
}
