use lib_provisioning *
#use ../lib_provisioning/utils/generate.nu *
use utils.nu *
# Provider middleware now available through lib_provisioning

# > Clusters services
export def "main create" [
    name?: string       # Server hostname in settings
    ...args             # Args for create command
    --infra (-i): string # infra directory
    --settings (-s): string # Settings path
    --outfile (-o): string  # Output file
    --cluster_pos (-p): int   # Server position in settings
    --check (-c)     # Only check mode no clusters will be created
    --wait (-w)      # Wait clusters to be created
    --select: string # Select with task as option
    --debug (-x)     # Use Debug mode
    --xm             # Debug with PROVISIONING_METADATA
    --xc             # Debuc for task and services locally PROVISIONING_DEBUG_CHECK
    --xr             # Debug for remote clusters PROVISIONING_DEBUG_REMOTE
    --xld            # Log level with DEBUG PROVISIONING_LOG_LEVEL=debug
    --metadata       # Error with metadata (-xm)
    --notitles       # not tittles
    --helpinfo (-h)  # For more details use options "help" (no dashes)
    --out: string    #  Print Output format: json, yaml, text (default)
] {
    if ($out | is-not-empty) {
        $env.PROVISIONING_OUT = $out
        $env.PROVISIONING_NO_TERMINAL = true
    }
    provisioning_init $helpinfo "cluster create" $args
    #parse_help_command "cluster create" $name --ismod --end
    # print "on cluster main create"
    if $debug { $env.PROVISIONING_DEBUG = true }
    if $metadata { $env.PROVISIONING_METADATA = true }
    if $name != null and $name != "h" and $name != "help" {
        let curr_settings = (find_get_settings --infra $infra --settings $settings)
        if ($curr_settings.data.clusters | find $name| length) == 0 {
            _print $"🛑 invalid name ($name)"
            exit 1
        }
    }
    let task = if ($args | length) > 0 {
        ($args| get 0)
    } else {
        let str_task = (($env.PROVISIONING_ARGS? | default "") | str replace "create " " " )
        let str_task = if  $name != null {
            ($str_task | str replace $name "")
        } else {
            $str_task
        }
        ($str_task | str trim | split row " " | first | default "" | split row "-" | first | default "" | str trim)
    }
    let other = if ($args | length) > 0 { ($args| skip 1) } else { "" }
    let ops = $"($env.PROVISIONING_ARGS? | default "") " | str replace $"($task) " "" | str trim
    let run_create = {
        # on_clusters is not defined anywhere in the codebase; cluster-create via
        # this entrypoint was dead at runtime. The workflow now lives in
        # main_provisioning/cluster-deploy.nu (prvng cluster deploy).
        _print $"(_ansi yellow)cluster create via this command is not wired(_ansi reset) — use 'prvng cluster deploy <layer> <cluster>' instead."
    }
    match $task {
        "" if $name == "h" => {
            ^$"($env.PROVISIONING_NAME)" -mod cluster create help --notitles
        },
        "" if $name == "help" => {
            ^$"($env.PROVISIONING_NAME)" -mod cluster create --help
            print (provisioning_options "create")
        },
        "" => {
            let result = desktop_run_notify $"($env.PROVISIONING_NAME) clusters create" "-> " $run_create --timeout 11sec
            #do $run_create
        },
        _ => {
            if $task != "" { print $"🛑 invalid_option ($task)" }
            print $"\nUse (_ansi blue_bold)($env.PROVISIONING_NAME) -h(_ansi reset) for help on commands and options"
        }
    }
    # "" | "create"
    if not $env.PROVISIONING_DEBUG { end_run "" }
}
