# Version Management Commands
# Manages versions with progressive cache hierarchy

use ../lib_provisioning/cache/cache_manager.nu *
use ../lib_provisioning/cache/grace_checker.nu *
use ../lib_provisioning/cache/version_loader.nu *
use ../lib_provisioning/cache/batch_updater.nu *

# Get version for a specific component
export def "version get" [
    component: string       # Component name (e.g., kubernetes, containerd)
] {
    get-cached-version $component
}

# Show cache status and statistics
export def "version status" [] {
    show-cache-status
}

# Initialize the cache system
export def "version init" [] {
    print "🚀 Initializing version cache system..."
    init-cache-system
    print "✅ Cache system initialized"
}

# Clear all cached versions
export def "version clear" [] {
    print "🧹 Clearing version cache..."
    clear-cache-system
    print "✅ Cache cleared"
}

# Update all cached versions in batches
export def "version update-all" [] {
    print "🔄 Updating all cached versions..."
    batch-update-all
    print "✅ Cache updated"
}

# Invalidate a specific component's cache entry
export def "version invalidate" [
    component: string       # Component to invalidate
] {
    invalidate-cache-entry $component "infra"
    invalidate-cache-entry $component "provisioning"
    print $"✅ Invalidated cache for ($component)"
}

# List all available components
export def "version list" [] {
    get-all-components
}

# Sync cache from source (force refresh)
export def "version sync" [
    component?: string      # Optional specific component
] {
    if ($component | is-not-empty) {
        invalidate-cache-entry $component "infra"
        invalidate-cache-entry $component "provisioning"
        let version = (get-cached-version $component)
        print $"🔄 Synced ($component): ($version)"
    } else {
        version clear
        version update-all
        print "🔄 Synced all versions"
    }
}
