# Provider Registry System
# Dynamic provider discovery, registration, and management

use ../config/accessor.nu *
use ../utils/logging.nu *
use interface.nu *

# Provider registry cache file path
def get-provider-cache-file [] {
    let cache_dir = ($env.HOME | path join ".cache" "provisioning")
    if not ($cache_dir | path exists) {
        mkdir $cache_dir
    }
    $cache_dir | path join "provider-registry.json"
}

# Check if registry is initialized
def is-registry-initialized [] {
    ($env.PROVIDER_REGISTRY_INITIALIZED? | default false)
}

# Mark registry as initialized
def mark-registry-initialized [] {
    $env.PROVIDER_REGISTRY_INITIALIZED = true
}

# Initialize the provider registry
export def init-provider-registry [] {
    if (is-registry-initialized) {
        return
    }

    # Check if cache exists and is recent (less than 1 hour old)
    let cache_file = (get-provider-cache-file)
    if ($cache_file | path exists) {
        let cache_age = ((date now) - (ls $cache_file | get 0.modified))
        if ($cache_age | into int) < 3600_000_000_000 {  # 1 hour in nanoseconds
            # Use cached registry
            mark-registry-initialized
            return
        }
    }

    log-debug "🔄 Initializing provider registry..." "provider-registry"

    # Discover and cache providers
    discover-and-register-providers
    mark-registry-initialized
}

# Get provider registry from cache or discover
def get-provider-registry [] {
    let cache_file = (get-provider-cache-file)
    if ($cache_file | path exists) {
        open $cache_file
    } else {
        discover-providers-only
    }
}

# Discover providers without full registration
def discover-providers-only [] {
    mut registry = {}

    # Get provisioning system path from config or environment
    let base_path = (config-get "provisioning.path" ($env.PROVISIONING? | default ($env.HOME | path join "project-provisioning/provisioning")))

    # PRIORITY 1: Workspace .providers (if in workspace context)
    # Look for .providers in workspace root or parent directories
    let current_dir = ($env.PWD)
    let workspace_providers = (
        if (($current_dir | path join ".providers") | path exists) {
            $current_dir | path join ".providers"
        } else if (($current_dir | path dirname | path join ".providers") | path exists) {
            $current_dir | path dirname | path join ".providers"
        } else if (($current_dir | path dirname | path dirname | path join ".providers") | path exists) {
            $current_dir | path dirname | path dirname | path join ".providers"
        } else {
            ""
        }
    )

    if ($workspace_providers | is-not-empty) and ($workspace_providers | path exists) {
        let workspace_prov = (discover-providers-in-directory $workspace_providers "workspace")
        $registry = ($registry | merge $workspace_prov)
    }

    # PRIORITY 2: Core providers
    let core_providers_path = ($base_path | path join "core" "nulib" "providers")
    if ($core_providers_path | path exists) {
        let core_providers = (discover-providers-in-directory $core_providers_path "core")
        $registry = ($registry | merge $core_providers)
    }

    # PRIORITY 3: Extension providers
    let extension_providers_path = ($base_path | path join "extensions" "providers")
    if ($extension_providers_path | path exists) {
        let extension_providers = (discover-providers-in-directory $extension_providers_path "extension")
        $registry = ($registry | merge $extension_providers)
    }

    $registry
}

# Discover and register all providers
def discover-and-register-providers [] {
    let registry = (discover-providers-only)

    # Save to cache
    let cache_file = (get-provider-cache-file)
    $registry | to json | save --force $cache_file

    log-debug $"✅ Discovered ($registry | columns | length) providers" "provider-registry"
}

# Discover providers in a specific directory
def discover-providers-in-directory [base_path: string, provider_type: string] {
    mut providers = {}

    if not ($base_path | path exists) {
        return $providers
    }

    # Look for provider.nu files in subdirectories
    let provider_dirs = (ls $base_path | where type == dir | get name)

    for dir in $provider_dirs {
        let provider_file = ($dir | path join "provider.nu")
        if ($provider_file | path exists) {
            let provider_name = ($dir | path basename)

            # COMMENTED OUT: Metadata verification was causing silent failures
            # The nu -c subprocess doesn't have proper NICKEL_IMPORT_PATH configured
            # This caused all providers to be skipped even though they are valid
            # Solution: Just mark all providers with provider.nu as available
            # Actual metadata loading happens when the provider is used

            # Check if provider has metadata function (just test it's valid)
            # We don't parse the metadata here, just verify the provider loads
            # Suppress error output by redirecting to /dev/null
            # let has_metadata = (do {
            #     ^nu -c $"use ($provider_file) *; get-provider-metadata | ignore" o> /dev/null e> /dev/null
            # } | complete | get exit_code) == 0

            # if $has_metadata { ... } else { ... }
            # INSTEAD: Simply register any provider.nu file as available
            let provider_info = {
                name: $provider_name
                type: $provider_type
                path: $dir
                entry_point: $provider_file
                available: true
                loaded: false
                last_discovered: (date now)
            }

            $providers = ($providers | insert $provider_name $provider_info)
            log-debug $"  📦 Found ($provider_type) provider: ($provider_name)" "provider-discovery"
        }
    }

    $providers
}

# List all available providers
export def list-providers [
    --available-only          # Only show available providers
    --verbose                 # Show detailed information
] {
    if not (is-registry-initialized) {
        init-provider-registry | ignore
    }

    let registry = (get-provider-registry)
    let providers = ($registry | transpose name details)

    let filtered = if $available_only {
        $providers | where {|p| $p.details.available == true}
    } else {
        $providers
    }

    if $verbose {
        $filtered | select name details.type details.available details.loaded details.path details.last_discovered
    } else {
        $filtered | select name details.type details.available details.loaded
    }
}

# Check if a provider is available
export def is-provider-available [provider_name: string] {
    if not (is-registry-initialized) {
        init-provider-registry | ignore
    }

    let registry = (get-provider-registry)

    if ($provider_name in ($registry | columns)) {
        let provider = ($registry | get $provider_name)
        $provider.available
    } else {
        false
    }
}

# Get provider entry information
export def get-provider-entry [provider_name: string] {
    if not (is-registry-initialized) {
        init-provider-registry | ignore
    }

    let registry = (get-provider-registry)

    if ($provider_name in ($registry | columns)) {
        $registry | get $provider_name
    } else {
        error make { msg: $"Provider ($provider_name) not found in registry" }
    }
}

# Get provider registry statistics
export def get-provider-stats [] {
    if not (is-registry-initialized) {
        init-provider-registry | ignore
    }

    let registry = (get-provider-registry)
    let providers = ($registry | transpose name details)

    {
        total_providers: ($providers | length)
        available_providers: ($providers | where {|p| $p.details.available == true} | length)
        core_providers: ($providers | where {|p| $p.details.type == "core"} | length)
        extension_providers: ($providers | where {|p| $p.details.type == "extension"} | length)
        loaded_providers: ($providers | where {|p| $p.details.loaded == true} | length)
    }
}

# Get capabilities for a specific provider
export def get-provider-capabilities-for [provider_name: string] {
    if not (is-provider-available $provider_name) {
        return {}
    }

    let provider_entry = (get-provider-entry $provider_name)

    let metadata_cmd = $"nu -c \"use ($provider_entry.entry_point) *; get-provider-metadata\""
    let result = (nu -c $metadata_cmd | complete)

    if $result.exit_code == 0 {
        let metadata = ($result.stdout | from json)
        $metadata.capabilities? | default {}
    } else {
        {}
    }
}

# Refresh the provider registry
export def refresh-provider-registry [] {
    # Clear cache
    let cache_file = (get-provider-cache-file)
    if ($cache_file | path exists) {
        rm $cache_file
    }

    # Reset initialization flag
    $env.PROVIDER_REGISTRY_INITIALIZED = false

    # Reinitialize
    init-provider-registry | ignore
}

# Export environment setup
export-env {
    $env.PROVIDER_REGISTRY_INITIALIZED = false
}
