# Batch Updater - Efficient batch operations for version cache
# Token-optimized batch processing to minimize LLM context usage

# Batch update cache from all sources
export def batch-update-cache [] {
    print "🔄 Starting batch cache update..."

    # Get all available components
    let all_components = (get-all-components)
    print $"📦 Found ($all_components | length) components to process"

    # Process in batches to be memory efficient
    let batch_size = 10
    let batches = ($all_components | chunks $batch_size)

    print $"⚡ Processing ($batches | length) batches of ($batch_size) components each"

    for batch in $batches {
        print $"🔄 Processing batch: ($batch | str join ', ')"
        process-batch $batch
    }

    print "✅ Batch update completed"
}

# Process a batch of components
def process-batch [components: list<string>] {
    # Load versions for all components in this batch
    let versions = (batch-load-versions $components)

    # Cache each version
    for component in ($versions | columns) {
        let version = ($versions | get $component)

        # Cache in both provisioning and infra
        cache-version $component $version "provisioning"
        cache-version $component $version "infra"

        print $"  ✓ ($component): ($version)"
    }
}

# Sync cache from sources (rebuild cache)
export def sync-cache-from-sources [] {
    print "🔄 Syncing cache from Nickel sources..."

    # Clear existing cache
    clear-cache-system

    # Initialize fresh cache
    init-cache-system

    # Batch update all components
    batch-update-cache

    print "✅ Cache sync completed"
}

# Update specific components
export def update-components [
    components: list<string>    # Specific components to update
] {
    print $"🔄 Updating specific components: ($components | str join ', ')"

    let versions = (batch-load-versions $components)

    for component in ($versions | columns) {
        let version = ($versions | get $component)

        # Invalidate old cache entries
        invalidate-cache-entry $component "infra"
        invalidate-cache-entry $component "provisioning"

        # Cache new versions
        cache-version $component $version "provisioning"
        cache-version $component $version "infra"

        print $"  ✓ Updated ($component): ($version)"
    }

    print "✅ Component update completed"
}

# Update expired components only
export def update-expired-components [] {
    print "🔄 Updating expired cache entries..."

    let expired_infra = (get-expired-entries "infra")
    let expired_prov = (get-expired-entries "provisioning")
    let all_expired = ($expired_infra ++ $expired_prov) | uniq

    if ($all_expired | is-empty) {
        print "✅ No expired entries found"
        return
    }

    print $"📋 Found ($all_expired | length) expired entries: ($all_expired | str join ', ')"
    update-components $all_expired
}

# Auto-update components with check_latest = true
export def auto-update-components [] {
    print "🔄 Checking for auto-updates (check_latest = true)..."

    let components_needing_update = (get-components-needing-update)

    if ($components_needing_update | is-empty) {
        print "✅ No components need auto-update"
        return
    }

    print $"📋 Components needing update: ($components_needing_update | str join ', ')"

    # For now, just update from sources
    # TODO: Add GitHub API integration for latest version checking
    update-components $components_needing_update

    print "⚠️ Note: GitHub API integration not yet implemented"
}

# Optimize cache (remove duplicates, compress)
export def optimize-cache [] {
    print "🔧 Optimizing cache..."

    let cache_types = ["infra", "provisioning"]

    for cache_type in $cache_types {
        let cache_path = if $cache_type == "infra" {
            get-infra-cache-path
        } else {
            get-provisioning-cache-path
        }

        let cache_file = ($cache_path | path join "versions.json")

        if ($cache_file | path exists) {
            let result = (do { open $cache_file } | complete)
            if $result.exit_code == 0 {
                let cache_data = ($result.stdout | from json)

                # Remove empty entries
                let cleaned_cache = ($cache_data | items { |key, value|
                    if ($value.current | is-not-empty) {
                        { $key: $value }
                    } else {
                        {}
                    }
                } | reduce { |item, acc| $acc | merge $item })

                # Save optimized cache
                $cleaned_cache | save -f $cache_file

                let entry_count = ($cleaned_cache | columns | length)
                print $"  ✓ Optimized ($cache_type) cache: ($entry_count) entries"
            } else {
                print $"  ❌ Failed to optimize ($cache_type) cache"
            }
        }
    }

    print "✅ Cache optimization completed"
}

# Import required functions
use cache_manager.nu [cache-version, clear-cache-system, init-cache-system, get-infra-cache-path, get-provisioning-cache-path]
use version_loader.nu [batch-load-versions, get-all-components]
use grace_checker.nu [get-expired-entries, get-components-needing-update, invalidate-cache-entry]
