# UpCloud Provider Defaults Template
# Extracted from wuji infrastructure patterns (real data)
# Provides UpCloud configuration with proven production settings

import providers.upcloud.kcl.defaults_upcloud as upcloud_prov
import workspace_templates.lib.compose as comp
import workspace_templates.lib.override as ovr

# UpCloud defaults configuration schema based on wuji
schema UpCloudDefaults {
    # Basic configuration
    time_zone: str = "UTC"
    zone: str = "es-mad1"  # Proven zone from wuji

    # Timing configuration
    running_wait: int = 10
    running_timeout: int = 200

    # Default storage configuration (from wuji production)
    default_storages: [any] = [
        {name = "root", size = 25, total = 25, type = "ext4", mount = True, mount_path = "/", parts = []}
    ]

    # OS configuration (Debian 12 - production tested)
    storage_os: str = "01000000-0000-4000-8000-000020080100"

    # SSH configuration (will be overridden per infrastructure)
    ssh_key_path: str = "~/.ssh/id_cdci.pub"  # From wuji
    ssh_key_name: str = "cdci"

    # Network configuration
    network_utility_ipv4: bool = True
    network_utility_ipv6: bool = False
    network_public_ipv4: bool = True
    network_public_ipv6: bool = False

    # Private network (customizable per infrastructure)
    network_private_id: str = "CREATE"  # Default to auto-create
    network_private_name: str  # Will be set per infrastructure
    priv_cidr_block: str = "10.11.1.0/24"  # Proven CIDR from wuji

    # DNS configuration (wuji production settings)
    primary_dns: str = "94.237.127.9"
    secondary_dns: str = "94.237.40.9"
    main_domain: str  # Must be provided per infrastructure
    domains_search: str  # Usually same as main_domain

    # User configuration (wuji production settings)
    user: str = "devadm"
    user_home: str = "/home/devadm"
    user_ssh_port: int = 22
    fix_local_hosts: bool = True
    installer_user: str = "root"
}

# Template function to create UpCloud defaults with infrastructure overrides
def create_upcloud_defaults [
    infrastructure_name: str,
    domain: str,
    network_config: {str: any} = {},
    overrides: {str: any} = {}
] -> any {
    let base_config = UpCloudDefaults {
        network_private_name: $"($infrastructure_name)-network"
        main_domain: $domain
        domains_search: $domain
    }

    # Apply network-specific configuration
    let with_network = comp.deep_merge $base_config $network_config

    # Apply final overrides
    let final_config = comp.deep_merge $with_network $overrides

    # Create core UpCloud provider configuration
    upcloud_prov.ServerDefaults_upcloud {
        time_zone: $final_config.time_zone
        zone: $final_config.zone
        running_wait: $final_config.running_wait
        running_timeout: $final_config.running_timeout
        storages: $final_config.default_storages
        storage_os: $final_config.storage_os
        ssh_key_path: $final_config.ssh_key_path
        ssh_key_name: $final_config.ssh_key_name
        network_utility_ipv4: $final_config.network_utility_ipv4
        network_utility_ipv6: $final_config.network_utility_ipv6
        network_public_ipv4: $final_config.network_public_ipv4
        network_public_ipv6: $final_config.network_public_ipv6
        network_private_id: $final_config.network_private_id
        network_private_name: $final_config.network_private_name
        priv_cidr_block: $final_config.priv_cidr_block
        primary_dns: $final_config.primary_dns
        secondary_dns: $final_config.secondary_dns
        main_domain: $final_config.main_domain
        domains_search: $final_config.domains_search
        user: $final_config.user
        user_home: $final_config.user_home
        user_ssh_port: $final_config.user_ssh_port
        fix_local_hosts: $final_config.fix_local_hosts
        installer_user: $final_config.installer_user
    }
}

# Wuji-specific network configuration (for reference)
wuji_network_config = {
    network_private_id: "03f6ab4b-f747-40f1-b905-431e6eec76a8"
    network_private_name: "LibreCloudNetwork"
    main_domain: "librecloud.online"
    domains_search: "librecloud.online"
}

# Export the template for use in infrastructure
_provider = upcloud_prov.ServerDefaults_upcloud {
    time_zone = "UTC"
    zone = "es-mad1"
    running_wait = 10
    running_timeout = 200
    storages = [
        {name = "root", size = 25, total = 25, type = "ext4", mount = True, mount_path = "/", parts = []}
    ]
    storage_os = "01000000-0000-4000-8000-000020080100"
    ssh_key_path = "~/.ssh/id_cdci.pub"
    ssh_key_name = "cdci"
    network_utility_ipv4 = True
    network_utility_ipv6 = False
    network_public_ipv4 = True
    network_public_ipv6 = False
    network_private_id = "CREATE"
    network_private_name = "librecloud-network"
    priv_cidr_block = "10.11.1.0/24"
    primary_dns = "94.237.127.9"
    secondary_dns = "94.237.40.9"
    main_domain = "librecloud.online"
    domains_search = "librecloud.online"
    user = "devadm"
    user_home = "/home/devadm"
    user_ssh_port = 22
    fix_local_hosts = True
    installer_user = "root"
}

_provider
