#! {{TOOL_NAME}} - NuShell Implementation
#!
#! A simple, single-file NuShell tool following the Tools ecosystem patterns.

use std/log

# Configuration for {{tool_name_kebab}}
export def config [] {
    # Load configuration from standard locations
    # 1. .project/{{tool_name_kebab}}.toml
    # 2. .vapora/{{tool_name_kebab}}.toml
    # 3. .coder/{{tool_name_kebab}}.toml
    # 4. ./{{tool_name_kebab}}.toml

    let search_paths = [
        ".project/{{tool_name_kebab}}.toml"
        ".vapora/{{tool_name_kebab}}.toml"
        ".coder/{{tool_name_kebab}}.toml"
        "./{{tool_name_kebab}}.toml"
    ]

    let found = $search_paths
        | find { |path| ($path | path exists) }
        | first

    if ($found | is-empty) {
        log error $"No config found in standard locations"
        return null
    }

    open $found
}

# List items [filter: optional filter pattern]
export def list [filter?: string] {
    let items = [
        { id: "1", name: "Item 1", status: "active" }
        { id: "2", name: "Item 2", status: "inactive" }
    ]

    if ($filter | is-empty) {
        return $items
    }

    $items | where name =~ $filter
}

# Create a new item [name: item name, description: optional description]
export def create [name: string, --description: string = ""] {
    {
        id: (random uuid)
        name: $name
        description: $description
        created_at: (now)
    }
}

# Show summary statistics
export def summary [] {
    let items = list

    {
        total_items: ($items | length)
        active_items: ($items | where status == "active" | length)
        last_updated: (now)
    }
}

# Validate configuration
export def "validate config" [] {
    let cfg = config

    if ($cfg | is-empty) {
        return { valid: false, errors: ["No configuration found"] }
    }

    {
        valid: true
        config: $cfg
    }
}

# Main entry point
export def main [] {
    print "{{TOOL_NAME}} v0.1.0"
    print ""
    print "Available commands:"
    print "  list               - List all items"
    print "  create <name>      - Create a new item"
    print "  summary            - Show statistics"
    print "  validate config    - Check configuration"
    print "  help               - Show this message"
}

# Help command
export def help [] {
    main
}
