#!/usr/bin/env nu

# List All Available Plugins - Custom & Core
#
# Usage:
#   list_plugins.nu                  # Show all plugins
#   list_plugins.nu --custom-only    # Show custom plugins only
#   list_plugins.nu --core-only      # Show core plugins only
#   list_plugins.nu --json           # Output as JSON
#   list_plugins.nu --csv            # Output as CSV

def main [
    --custom-only                   # Show only custom plugins
    --core-only                     # Show only core plugins
    --json (-j)                     # Output JSON format
    --csv (-c)                      # Output CSV format
] {
    log_info "📦 Nushell Plugins Manifest"
    log_info "=================================================================="

    # Get all plugins
    let custom = get_custom_plugins
    let core = get_core_plugins
    let all = ($custom | append $core)

    # Filter by type
    let plugins = if $custom_only {
        $custom
    } else if $core_only {
        $core
    } else {
        $all
    }

    # Output format
    if $json {
        output_json $plugins $custom_only $core_only
    } else if $csv {
        output_csv $plugins
    } else {
        output_table $plugins
    }

    # Summary
    print ""
    log_info "📊 Summary:"
    if not $core_only {
        log_success $"  Custom plugins: ($custom | length)"
    }
    if not $custom_only {
        let core_available = ($core | where status == "Built" | length)
        let core_required = ($core | where status == "Build Required" | length)
        log_success $"  Core plugins available: ($core_available)"
        if $core_required > 0 {
            log_warn $"  Core plugins requiring build: ($core_required)"
        }
    }
    log_success $"  Total plugins: ($plugins | length)"
}

# Get custom plugins
def get_custom_plugins []: nothing -> list<record> {
    [
        {name: "nu_plugin_auth", purpose: "Authentication (JWT, MFA)", status: "Built", location: "~/.local/bin/"}
        {name: "nu_plugin_kms", purpose: "Encryption & KMS", status: "Built", location: "~/.local/bin/"}
        {name: "nu_plugin_orchestrator", purpose: "Orchestration operations", status: "Built", location: "~/.local/bin/"}
        {name: "nu_plugin_kcl", purpose: "KCL configuration language", status: "Built", location: "~/.local/bin/"}
        {name: "nu_plugin_tera", purpose: "Template rendering", status: "Built", location: "~/.local/bin/"}
        {name: "nu_plugin_highlight", purpose: "Syntax highlighting", status: "Built", location: "~/.local/bin/"}
        {name: "nu_plugin_clipboard", purpose: "Clipboard operations", status: "Built", location: "~/.local/bin/"}
        {name: "nu_plugin_image", purpose: "Image processing", status: "Built", location: "~/.local/bin/"}
        {name: "nu_plugin_hashes", purpose: "Hash functions", status: "Built", location: "~/.local/bin/"}
        {name: "nu_plugin_qr_maker", purpose: "QR code generation", status: "Built", location: "~/.local/bin/"}
        {name: "nu_plugin_fluent", purpose: "Localization & i18n", status: "Built", location: "~/.local/bin/"}
        {name: "nu_plugin_desktop_notifications", purpose: "Desktop notifications", status: "Built", location: "~/.local/bin/"}
        {name: "nu_plugin_port_extension", purpose: "Port/networking extensions", status: "Built", location: "~/.local/bin/"}
    ]
}

# Get core plugins
def get_core_plugins []: nothing -> list<record> {
    let nushell_release = "./nushell/target/release"
    let core_exist = ($nushell_release | path exists)

    let core_list = [
        {name: "nu_plugin_polars", purpose: "Data analysis with Polars", status: (if $core_exist {"Built"} else {"Build Required"}), location: "nushell/target/release/"}
        {name: "nu_plugin_formats", purpose: "Data format conversions", status: (if $core_exist {"Built"} else {"Build Required"}), location: "nushell/target/release/"}
        {name: "nu_plugin_inc", purpose: "Increment operations", status: (if $core_exist {"Built"} else {"Build Required"}), location: "nushell/target/release/"}
        {name: "nu_plugin_gstat", purpose: "Git status information", status: (if $core_exist {"Built"} else {"Build Required"}), location: "nushell/target/release/"}
        {name: "nu_plugin_query", purpose: "Advanced querying", status: (if $core_exist {"Built"} else {"Build Required"}), location: "nushell/target/release/"}
        {name: "nu_plugin_custom_values", purpose: "Custom value types", status: (if $core_exist {"Built"} else {"Build Required"}), location: "nushell/target/release/"}
        {name: "nu_plugin_example", purpose: "Example plugin template", status: (if $core_exist {"Built"} else {"Build Required"}), location: "nushell/target/release/"}
        {name: "nu_plugin_stress_internals", purpose: "Stress testing", status: (if $core_exist {"Built"} else {"Build Required"}), location: "nushell/target/release/"}
    ]

    $core_list
}

# Output as table
def output_table [
    plugins: list<record>
] {
    print ""
    print $plugins | table
}

# Output as JSON
def output_json [
    plugins: list<record>
    custom_only: bool
    core_only: bool
] {
    let output = {
        manifest_version: "1.0.0"
        nushell_version: "0.108.0+"
        plugins: $plugins
        summary: {
            total: ($plugins | length)
            custom: (get_custom_plugins | length)
            core: (get_core_plugins | length)
        }
    }

    $output | to json
}

# Output as CSV
def output_csv [
    plugins: list<record>
] {
    print "Name,Purpose,Status,Location"
    for p in $plugins {
        print $"($p.name),\"($p.purpose)\",($p.status),($p.location)"
    }
}

# Logging
def log_info [msg: string] {
    print $"ℹ️  ($msg)"
}

def log_success [msg: string] {
    print $"✅ ($msg)"
}

def log_warn [msg: string] {
    print $"⚠️  ($msg)"
}

main
