#!/usr/bin/env nu
# Cache management CLI - minimal wrapper for cache operations
# Works without requiring an active workspace

def main [...args: string] {
    use ../nulib/lib_provisioning/config/cache/simple-cache.nu *

    # Default to "status" if no args
    let args = if ($args | is-empty) { ["status"] } else { $args }

    # Parse command
    let command = if ($args | length) > 0 { $args | get 0 } else { "status" }
    let sub_args = if ($args | length) > 1 { $args | skip 1 } else { [] }

    match $command {
        "status" => {
            print ""
            cache-status
            print ""
        }

        "config" => {
            let sub_cmd = if ($sub_args | length) > 0 { $sub_args | get 0 } else { "show" }
            match $sub_cmd {
                "show" => {
                    print ""
                    let config = (get-cache-config)
                    print "Cache Configuration:"
                    print $"  enabled: ($config | get --optional enabled | default true)"
                    print $"  ttl_final_config: ($config | get --optional ttl_final_config | default 300)s"
                    print $"  ttl_kcl: ($config | get --optional ttl_kcl | default 1800)s"
                    print $"  ttl_sops: ($config | get --optional ttl_sops | default 900)s"
                    print ""
                }
                "get" => {
                    if ($sub_args | length) > 1 {
                        let setting = $sub_args | get 1
                        let value = (cache-config-get $setting)
                        if $value != null {
                            print $"($setting) = ($value)"
                        } else {
                            print $"Setting not found: ($setting)"
                        }
                    } else {
                        print "❌ cache config get requires a setting"
                        print "Usage: cache config get <setting>"
                        exit 1
                    }
                }
                "set" => {
                    if ($sub_args | length) > 2 {
                        let setting = $sub_args | get 1
                        let value = ($sub_args | skip 2 | str join " ")
                        # Convert value to appropriate type
                        let converted_value = (
                            if $value == "1" or $value == "yes" or $value == "on" { true }
                            else if $value == "0" or $value == "no" or $value == "off" { false }
                            else { $value }
                        )
                        cache-config-set $setting $converted_value
                        # Display the actual value stored
                        let display_value = if $converted_value == true { "true" } else if $converted_value == false { "false" } else { $value }
                        print $"✓ Set ($setting) = ($display_value)"
                    } else {
                        print "❌ cache config set requires setting and value"
                        print "Usage: cache config set <setting> <value>"
                        print "  For boolean: use 0/no/off for false, 1/yes/on for true"
                        exit 1
                    }
                }
                _ => {
                    print $"❌ Unknown cache config command: ($sub_cmd)"
                    print "Available: show, get, set"
                    exit 1
                }
            }
        }

        "clear" => {
            let cache_type = if ($sub_args | length) > 0 { $sub_args | get 0 } else { "all" }
            cache-clear $cache_type
            print $"✓ Cleared cache: ($cache_type)"
        }

        "list" => {
            let cache_type = if ($sub_args | length) > 0 { $sub_args | get 0 } else { "*" }
            let items = (cache-list $cache_type)
            if ($items | length) > 0 {
                print $"Cache items \(type: ($cache_type)\):"
                $items | each { |item| print $"  ($item)" }
            } else {
                print "No cache items found"
            }
        }

        "help" | "--help" | "-h" => {
            print "
Cache Management Commands:

  cache status                         # Show cache status and statistics
  cache config show                    # Show cache configuration
  cache config get <setting>           # Get specific cache setting
  cache config set <setting> <value>   # Set cache setting
  cache clear [type]                   # Clear cache (default: all)
  cache list [type]                    # List cached items (default: all)
  cache help                           # Show this help message

Available settings (for get/set):
  enabled          - Cache enabled (true/false)
  ttl_final_config - TTL for final config (seconds)
  ttl_kcl          - TTL for KCL compilation (seconds)
  ttl_sops         - TTL for SOPS decryption (seconds)

Examples:
  cache status
  cache config get ttl_final_config
  cache config set ttl_final_config 600
  cache config set enabled false
  cache clear kcl
  cache list
"
        }

        _ => {
            print $"❌ Unknown command: ($command)"
            print "Use 'cache help' for available commands"
            exit 1
        }
    }
}
