# Template Utilities for Enhanced Module Loader
# Supporting functions for template management

# Apply template to infrastructure
export def apply_template_to_infra [
    template_info: record,
    target_infra: string,
    provider: string
] {
    print $"🔧 Applying template to ($target_infra) with provider ($provider)"

    let target_dir = $"workspace/infra/($target_infra)"
    mkdir ($"($target_dir)/taskservs")
    mkdir ($"($target_dir)/defs")

    # Create basic template-based configuration
    let template_content = create_template_usage $template_info.name $target_infra $provider

    # Save template-based configuration
    $template_content | save $"($target_dir)/taskservs/($template_info.name).k"

    print $"✅ Template applied: ($template_info.name) -> ($target_infra)"
}

# Create template usage content
def create_template_usage [
    template_name: string,
    infra_name: string,
    provider: string
]: nothing -> string {
    $"# ($template_name | str title-case) Configuration for ($infra_name)
# Generated from workspace template

import provisioning.workspace.registry.imports as reg
import provisioning.workspace.templates.lib.override as ovr

# Apply template with infrastructure-specific overrides
_taskserv = ovr.infrastructure_overrides.taskserv_override(
    reg.templates.($template_name).base,
    \"($infra_name)\",
    \"($template_name)\",
    {
        cluster_name: \"($infra_name)\"
        # Add custom overrides here
    }
)

_taskserv"
}

# Copy template as override
export def copy_template_as_override [
    template_path: string,
    override_dir: string,
    module_name: string
] {
    if ($template_path | path exists) {
        cp $template_path $"($override_dir)/($module_name).k"
        print $"📋 Template copied as override: ($module_name)"
    } else {
        print $"⚠️ Template not found: ($template_path)"
    }
}

# Load taskservs with layer support
export def load_taskservs_with_layer [
    workspace: string,
    modules: list<string>,
    layer: string,
    force: bool,
    with_overrides: bool
] {
    print $"🔄 Loading taskservs from ($layer) layer"

    for module in $modules {
        let source_path = match $layer {
            "core" => $"provisioning/extensions/taskservs/($module)"
            "workspace" => $"provisioning/workspace/templates/taskservs/($module)"
            "templates" => $"provisioning/workspace/templates/taskservs/($module)"
            _ => $"workspace/infra/($workspace)/taskservs/($module).k"
        }

        if ($source_path | path exists) {
            print $"  ✅ Loading ($module) from ($layer)"
            # Implementation would copy or link the module
        } else {
            print $"  ❌ Module ($module) not found in ($layer) layer"
        }
    }
}

# Load providers with layer support
export def load_providers_with_layer [
    workspace: string,
    modules: list<string>,
    layer: string,
    force: bool,
    with_overrides: bool
] {
    print $"🔄 Loading providers from ($layer) layer"

    for module in $modules {
        let source_path = match $layer {
            "core" => $"provisioning/extensions/providers/($module)"
            "workspace" => $"provisioning/workspace/templates/providers/($module)"
            "templates" => $"provisioning/workspace/templates/providers/($module)"
            _ => $"workspace/infra/($workspace)/defs/($module)_defaults.k"
        }

        if ($source_path | path exists) {
            print $"  ✅ Loading ($module) from ($layer)"
            # Implementation would copy or link the module
        } else {
            print $"  ❌ Provider ($module) not found in ($layer) layer"
        }
    }
}

# Load clusters with layer support
export def load_clusters_with_layer [
    workspace: string,
    modules: list<string>,
    layer: string,
    force: bool,
    with_overrides: bool
] {
    print $"🔄 Loading clusters from ($layer) layer"

    for module in $modules {
        let source_path = match $layer {
            "core" => $"provisioning/extensions/clusters/($module)"
            "workspace" => $"provisioning/workspace/templates/clusters/($module)"
            "templates" => $"provisioning/workspace/templates/clusters/($module)"
            _ => $"workspace/infra/($workspace)/clusters/($module).k"
        }

        if ($source_path | path exists) {
            print $"  ✅ Loading ($module) from ($layer)"
            # Implementation would copy or link the module
        } else {
            print $"  ❌ Cluster ($module) not found in ($layer) layer"
        }
    }
}
