#!/usr/bin/env nu

# Simple, working Nushell 0.109+ installer
# Installs Nushell binary and plugins from local build directories

def main [
    --install-dir (-i): string = ""      # Installation directory
    --interactive (-I)                   # Interactive mode
    --verify                             # Verify after install
    --test                               # Test mode (dry run)
] {
    print "🚀 Nushell Distribution Installer"
    print "=================================="
    print ""

    # Set installation directory
    let install_dir = if ($install_dir == "") {
        $"($env.HOME)/.local/bin"
    } else {
        $install_dir
    }

    print $"📍 Installation directory: ($install_dir)"

    # Find binaries
    let nu_binary = if ("./nushell/target/release/nu" | path exists) {
        "./nushell/target/release/nu"
    } else {
        print "❌ Error: Nushell binary not found at ./nushell/target/release/nu"
        print "Run 'just build-nushell' first"
        exit 1
    }

    let plugins = (glob ./nu_plugin_*/target/release/nu_plugin_* | each {|p| {name: ($p | path basename), path: $p}})

    print $"📦 Found components:"
    print $"   - Nushell binary: ($nu_binary | path basename)"
    print $"   - Plugins: (($plugins | length))"
    ($plugins | each {|p| print $"      • ($p.name)"})

    if $test {
        print ""
        print "🧪 Test mode (dry run) - no changes made"
        print "   Run without --test to actually install"
        return
    }

    print ""

    # Create directory
    mkdir -p $install_dir
    print $"✅ Created: ($install_dir)"

    # Install Nushell
    print ""
    print "📥 Installing Nushell..."
    cp $nu_binary $"($install_dir)/nu"
    chmod +x $"($install_dir)/nu"
    print $"✅ Installed: ($install_dir)/nu"

    # Install plugins
    if ($plugins | length) > 0 {
        print ""
        print "📥 Installing plugins..."
        for plugin in $plugins {
            cp $plugin.path $"($install_dir)/($plugin.name)"
            chmod +x $"($install_dir)/($plugin.name)"
            print $"✅ Installed: ($plugin.name)"
        }
    }

    # Register plugins
    print ""
    print "📝 Registering plugins..."

    let nu_exe = $"($install_dir)/nu"
    let plugin_names = ($plugins | map {|p| $p.name})

    for plugin_name in $plugin_names {
        let plugin_path = $"($install_dir)/($plugin_name)"
        try {
            ^$nu_exe -c $"plugin add ($plugin_path)"
            print $"✅ Registered: ($plugin_name)"
        } catch {|err|
            print $"⚠️  Failed to register ($plugin_name): ($err.msg)"
        }
    }

    # Summary
    print ""
    print "✨ Installation Complete!"
    print ""
    print "📋 Next steps:"
    print $"   1. Add ($install_dir) to your PATH:"
    print $"      export PATH=\"($install_dir):${'$'}PATH\""
    print $"   2. Verify installation:"
    print $"      ($install_dir)/nu -c \"version\""
    print $"   3. Check registered plugins:"
    print $"      ($install_dir)/nu -c \"plugin list\""

    if $verify {
        print ""
        print "🔍 Verifying installation..."
        verify_installation $install_dir $plugin_names
    }
}

def verify_installation [install_dir: string, plugins: list<string>] {
    let nu_exe = $"($install_dir)/nu"

    print ""
    print "Verification Results:"
    print "===================="

    # Check Nushell
    if ($nu_exe | path exists) {
        try {
            let version = (^$nu_exe -c "version")
            print $"✅ Nushell: ($version)"
        } catch {
            print "❌ Nushell binary not executable"
        }
    } else {
        print "❌ Nushell binary not found"
    }

    # Check plugins
    for plugin in $plugins {
        let plugin_path = $"($install_dir)/($plugin)"
        if ($plugin_path | path exists) {
            print $"✅ ($plugin): installed"
        } else {
            print $"❌ ($plugin): not found"
        }
    }
}

main
