#!/usr/bin/env nu
# Dynamic Version Cache Agent
# Token-optimized agent for progressive version caching with infra-aware hierarchy
# Usage: nu agent.nu <command> [args]

# Selective imports (ADR-025 Phase 3 Layer 2).
# version_loader and grace_checker star-imports were dead — dropped.
use lib_provisioning/cache/cache_manager.nu [
    clear-cache-system get-cached-version init-cache-system show-cache-status
]
use lib_provisioning/cache/batch_updater.nu [batch-update-cache sync-cache-from-sources]

# Main agent entry point
def main [
    command: string         # Command: init, get, update-all, clear, status
    ...args                # Additional arguments
] {
    match $command {
        "init" => {
            print "🚀 Initializing dynamic version cache system..."
            init-cache-system
            print "✅ Cache system initialized"
        }

        "get" => {
            if ($args | length) == 0 {
                print "❌ Usage: agent.nu get <component-name>"
                exit 1
            }
            let component = ($args | get 0)
            print $"🔍 Getting version for ($component)..."
            let version = (get-cached-version $component)
            print $"📦 ($component): ($version)"
        }

        "update-all" => {
            print "🔄 Updating all cached versions..."
            batch-update-cache
            print "✅ Cache updated"
        }

        "clear" => {
            print "🗑️ Clearing version cache..."
            clear-cache-system
            print "✅ Cache cleared"
        }

        "status" => {
            print "📊 Version cache status:"
            show-cache-status
        }

        "sync" => {
            print "🔄 Syncing cache from sources..."
            sync-cache-from-sources
            print "✅ Cache synced"
        }

        _ => {
            print $"❌ Unknown command: ($command)"
            print "Available commands: init, get, update-all, clear, status, sync"
            exit 1
        }
    }
}
