# Plugin Command Handlers
# Domain: Plugin discovery, installation, testing, and status

# Plugins command handler - Manage provisioning plugins
export def handle_plugins [ops: string, flags: record] {
    let subcommand = if ($ops | is-not-empty) {
        ($ops | split row " " | get 0)
    } else {
        "list"
    }

    let remaining_ops = if ($ops | is-not-empty) {
        ($ops | split row " " | skip 1 | str join " ")
    } else {
        ""
    }

    match $subcommand {
        "list" | "ls" => { handle_plugin_list $flags }
        "register" | "add" => { handle_plugin_register $remaining_ops $flags }
        "test" => { handle_plugin_test $remaining_ops $flags }
        "build" => { handle_plugin_build $remaining_ops $flags }
        "status" => { handle_plugin_status $flags }
        "help" => { show_plugin_help }
        _ => {
            print $"❌ Unknown plugin subcommand: ($subcommand)"
            print "Use 'provisioning plugin help' for available commands"
            exit 1
        }
    }
}

# List installed plugins with status
def handle_plugin_list [flags: record] {
    use ../../../lib_provisioning/plugins/mod.nu [list-plugins]

    print $"\n (_ansi cyan_bold)Installed Plugins(_ansi reset)\n"

    let plugins = (list-plugins)

    if ($plugins | length) > 0 {
        print ($plugins | table -e)
    } else {
        print "(_ansi yellow)No plugins found(_ansi reset)"
    }

    print $"\n(_ansi default_dimmed)💡 Use 'provisioning plugin register <name>' to register a plugin(_ansi reset)"
}

# Register plugin with Nushell
def handle_plugin_register [ops: string, flags: record] {
    use ../../../lib_provisioning/plugins/mod.nu [register-plugin]

    let plugin_name = if ($ops | is-not-empty) {
        ($ops | split row " " | get 0)
    } else {
        print $"(_ansi red)❌ Plugin name required(_ansi reset)"
        print $"Usage: provisioning plugin register <plugin_name>"
        exit 1
    }

    register-plugin $plugin_name
}

# Test plugin functionality
def handle_plugin_test [ops: string, flags: record] {
    use ../../../lib_provisioning/plugins/mod.nu [test-plugin]

    let plugin_name = if ($ops | is-not-empty) {
        ($ops | split row " " | get 0)
    } else {
        print $"(_ansi red)❌ Plugin name required(_ansi reset)"
        print $"Usage: provisioning plugin test <plugin_name>"
        print $"Valid plugins: auth, kms, tera, nickel"
        exit 1
    }

    test-plugin $plugin_name
}

# Build plugins from source
def handle_plugin_build [ops: string, flags: record] {
    use ../../../lib_provisioning/plugins/mod.nu [build-plugins]

    let plugin_name = if ($ops | is-not-empty) {
        ($ops | split row " " | get 0)
    } else {
        ""
    }

    if ($plugin_name | is-empty) {
        print $"\n(_ansi cyan)Building all plugins...(_ansi reset)"
        build-plugins
    } else {
        print $"\n(_ansi cyan)Building plugin: ($plugin_name)(_ansi reset)"
        build-plugins --plugin $plugin_name
    }
}

# Show plugin status
def handle_plugin_status [flags: record] {
    use ../../../lib_provisioning/plugins/mod.nu [plugin-build-info]
    use ../../../lib_provisioning/plugins/auth.nu [plugin-auth-status]
    use ../../../lib_provisioning/plugins/kms.nu [plugin-kms-info]

    print $"\n(_ansi cyan_bold)Plugin Status(_ansi reset)\n"

    print $"(_ansi yellow_bold)Authentication Plugin:(_ansi reset)"
    let auth_status = (plugin-auth-status)
    print $"  Available: ($auth_status.plugin_available)"
    print $"  Enabled: ($auth_status.plugin_enabled)"
    print $"  Mode: ($auth_status.mode)"

    print $"\n(_ansi yellow_bold)KMS Plugin:(_ansi reset)"
    let kms_info = (plugin-kms-info)
    print $"  Available: ($kms_info.plugin_available)"
    print $"  Enabled: ($kms_info.plugin_enabled)"
    print $"  Backend: ($kms_info.default_backend)"
    print $"  Mode: ($kms_info.mode)"

    print $"\n(_ansi yellow_bold)Build Information:(_ansi reset)"
    let build_info = (plugin-build-info)
    if $build_info.exists {
        print $"  Source directory: ($build_info.plugins_dir)"
        print $"  Available sources: ($build_info.available_sources | length)"
    } else {
        print $"  Source directory: Not found"
    }
}

# Show plugin help
def show_plugin_help [] {
    print $"
(_ansi cyan_bold)╔══════════════════════════════════════════════════╗(_ansi reset)
(_ansi cyan_bold)║(_ansi reset)  🔌 PLUGIN MANAGEMENT                            (_ansi cyan_bold)║(_ansi reset)
(_ansi cyan_bold)╚══════════════════════════════════════════════════╝(_ansi reset)

(_ansi green_bold)[Plugin Operations](_ansi reset)
  (_ansi blue)plugin list(_ansi reset)              List all plugins with status
  (_ansi blue)plugin register <name>(_ansi reset)   Register plugin with Nushell
  (_ansi blue)plugin test <name>(_ansi reset)       Test plugin functionality
  (_ansi blue)plugin build [name](_ansi reset)      Build plugins from source
  (_ansi blue)plugin status(_ansi reset)            Show plugin status and info

(_ansi green_bold)[Available Plugins](_ansi reset)
  • (_ansi cyan)auth(_ansi reset) - JWT authentication with MFA support
  • (_ansi cyan)kms(_ansi reset) - Key Management Service integration
  • (_ansi cyan)tera(_ansi reset) - Template rendering engine
  • (_ansi cyan)nickel(_ansi reset) - Nickel configuration language

(_ansi green_bold)EXAMPLES(_ansi reset)

  # List all plugins
  provisioning plugin list

  # Register auth plugin
  provisioning plugin register nu_plugin_auth

  # Test KMS plugin
  provisioning plugin test kms

  # Build all plugins
  provisioning plugin build

  # Build specific plugin
  provisioning plugin build nu_plugin_auth

  # Show plugin status
  provisioning plugin status

(_ansi default_dimmed)💡 Plugins provide HTTP fallback when not registered
   Authentication and KMS work in both plugin and HTTP modes(_ansi reset)
"
}
