#!/usr/bin/env nu

# Plugin Installation Script
# Builds and registers nushell plugins

# Build and register a single plugin
def install_single_plugin [
    plugin_name: string
    --verify (-v)                # Verify registration after installation
] {
    print $"📥 Installing ($plugin_name) locally..."

    # Check if plugin directory exists
    if not ($plugin_name | path exists) {
        print $"❌ Plugin directory not found: ($plugin_name)"
        return {success: false, error: "directory_not_found"}
    }

    # Build the plugin
    print $"🔨 Building ($plugin_name)..."
    try {
        nu -c $"cd ($plugin_name); cargo build --release"
    } catch {|err|
        print $"❌ Build failed for ($plugin_name): ($err.msg)"
        return {success: false, error: "build_failed"}
    }

    # Check if binary was created
    let binary_path = $"($plugin_name)/target/release/($plugin_name)"
    if not ($binary_path | path exists) {
        print $"❌ Plugin binary not found after build: ($binary_path)"
        return {success: false, error: "binary_not_found"}
    }

    # Register the plugin using our registration script
    print "🔌 Registering with nushell..."
    if $verify {
        let register_result = (nu $"./scripts/register_plugins.nu" register $binary_path --verify)
    } else {
        let register_result = (nu $"./scripts/register_plugins.nu" register $binary_path)
    }

    print $"✅ ($plugin_name) installation completed"
    return {success: true, binary_path: $binary_path}
}

# Main command interface
def main [
    plugin_name?: string        # Plugin name to install
    --verify (-v)               # Verify registration after installation
    --all (-a)                  # Install all plugins
] {
    if $all {
        print "📥 Installing all plugins..."
        let plugin_dirs = (glob "nu_plugin_*" | where ($it | path type) == "dir")

        if ($plugin_dirs | is-empty) {
            print "❓ No plugin directories found"
            return
        }

        mut installed = 0
        mut failed = 0

        for plugin in $plugin_dirs {
            let result = (install_single_plugin $plugin --verify=$verify)
            if $result.success {
                $installed = $installed + 1
            } else {
                $failed = $failed + 1
            }
        }

        print ""
        print "📊 Installation summary:"
        print $"  ✅ Installed: ($installed) plugins"
        if $failed > 0 {
            print $"  ❌ Failed: ($failed) plugins"
        }
    } else if not ($plugin_name | is-empty) {
        install_single_plugin $plugin_name --verify=$verify
    } else {
        print "🔧 Plugin Installation Script"
        print "============================="
        print ""
        print "Usage:"
        print "  nu install_plugin.nu <plugin_name> [--verify]"
        print "  nu install_plugin.nu --all [--verify]"
        print ""
        print "Options:"
        print "  --verify (-v)    - Verify registration after installation"
        print "  --all (-a)       - Install all plugins"
        print ""
        print "Examples:"
        print "  nu install_plugin.nu nu_plugin_clipboard --verify"
        print "  nu install_plugin.nu --all --verify"
    }
}