# Integrations Command Dispatcher
# Routes integration commands to appropriate domain-specific handlers
# Provides access to prov-ecosystem, provctl, and native plugin functionality
# NUSHELL 0.109 COMPLIANT - All handlers properly exported

use ./auth.nu *
use ./kms.nu *
use ./orch.nu *
use ./runtime.nu *
use ./ssh.nu *
use ./backup.nu *
use ./gitops.nu *
use ./service.nu *
use ./shared.nu *

# Main integration command dispatcher
export def cmd-integrations [
    subcommand: string
    args: list = []
    --check = false
] {
    match $subcommand {
        # Plugin-powered commands (10-30x faster)
        "auth" => { cmd-auth ($args | get 0?) ($args | skip 1) --check=$check }
        "kms" => { cmd-kms ($args | get 0?) ($args | skip 1) --check=$check }
        "orch" | "orchestrator" => { cmd-orch ($args | get 0?) ($args | skip 1) --check=$check }
        "plugin" | "plugins" => { cmd-plugin-status ($args | get 0?) ($args | skip 1) }

        # Legacy integration commands
        "runtime" => { cmd-runtime ($args | get 0?) ($args | skip 1) --check=$check }
        "ssh" => { cmd-ssh ($args | get 0?) ($args | skip 1) --check=$check }
        "backup" => { cmd-backup ($args | get 0?) ($args | skip 1) --check=$check }
        "gitops" => { cmd-gitops ($args | get 0?) ($args | skip 1) --check=$check }
        "service" => { cmd-service ($args | get 0?) ($args | skip 1) --check=$check }
        "help" | "--help" | "-h" => { help-integrations }
        _ => {
            print $"Unknown integration command: [$subcommand]"
            help-integrations
            exit 1
        }
    }
}

# Plugin status command handler
def cmd-plugin-status [
    action: string
    args: list = []
] {
    if ($action == null or $action == "status") {
        let status = (plugins-status)
        print ""
        print "Provisioning Plugins Status"
        print "============================"
        print ""
        let auth_status = if $status.auth { "[OK] " } else { "[--]" }
        let kms_status = if $status.kms { "[OK] " } else { "[--]" }
        let orch_status = if $status.orchestrator { "[OK] " } else { "[--]" }

        print $"($auth_status) nu_plugin_auth        - JWT authentication with keyring"
        print $"($kms_status) nu_plugin_kms         - Multi-backend encryption"
        print $"($orch_status) nu_plugin_orchestrator - Local orchestrator \(30x faster\)"
        print ""

        let all_loaded = $status.auth and $status.kms and $status.orchestrator
        if $all_loaded {
            print "All plugins loaded - using native high-performance mode"
        } else {
            print "Some plugins not loaded - using HTTP fallback"
            print ""
            print "Install plugins with:"
            print "  nu provisioning/core/plugins/install-plugins.nu"
        }
        print ""
        return
    }

    match $action {
        "list" => {
            let plugins = (plugin list | default [])
            let provisioning_plugins = ($plugins | where name =~ "nu_plugin_(auth|kms|orchestrator)" | default [])
            if ($provisioning_plugins | length) == 0 {
                print "No provisioning plugins registered"
            } else {
                print "Registered provisioning plugins:"
                $provisioning_plugins | table
            }
        }
        "test" => {
            print "Running plugin tests..."
            let status = (plugins-status)

            let results = (
                [
                    { name: "auth", available: $status.auth }
                    { name: "kms", available: $status.kms }
                    { name: "orchestrator", available: $status.orchestrator }
                ]
                | each { |item|
                    if $item.available {
                        print $"  [OK] ($item.name) plugin responding"
                        { status: "ok", name: $item.name }
                    } else {
                        print $"  [FAIL] ($item.name) plugin not available"
                        { status: "fail", name: $item.name }
                    }
                }
            )

            let passed = ($results | where status == "ok" | length)
            let failed = ($results | where status == "fail" | length)

            print ""
            print $"Results: ($passed) passed, ($failed) failed"
        }
        "help" | "--help" => {
            print "Plugin management commands"
            print ""
            print "Usage: provisioning plugin <action>"
            print ""
            print "Actions:"
            print "  status     Show plugin status (default)"
            print "  list       List registered plugins"
            print "  test       Test plugin functionality"
        }
        _ => { print $"Unknown plugin command: [$action]" }
    }
}

# Help for integration commands
def help-integrations [] {
    print "Integration commands - Access prov-ecosystem, provctl, and plugin functionality"
    print ""
    print "Usage: provisioning integrations <command> [options]"
    print ""
    print "PLUGIN-POWERED COMMANDS (10-30x faster):"
    print "  auth       JWT authentication with system keyring"
    print "  kms        Multi-backend encryption (RustyVault, Age, AWS, Vault)"
    print "  orch       Local orchestrator operations (30x faster than HTTP)"
    print "  plugin     Plugin status and management"
    print ""
    print "LEGACY INTEGRATION COMMANDS:"
    print "  runtime    Container runtime abstraction (docker, podman, orbstack, colima, nerdctl)"
    print "  ssh        Advanced SSH operations with pooling and circuit breaker"
    print "  backup     Multi-backend backup management (restic, borg, tar, rsync)"
    print "  gitops     Event-driven deployments from Git"
    print "  service    Cross-platform service management (systemd, launchd, runit, openrc)"
    print ""
    print "Shortcuts: int, integ, integrations"
    print "Use: provisioning <command> help"
}
