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

# > Clusters services
export def "main generate" [
    name?: string       # Server hostname in settings
    ...args                 # Args for generate 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 generated 
    --wait (-w)      # Wait clusters to be generated 
    --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)
]: nothing -> nothing {
    if ($out | is-not-empty) {
        $env.PROVISIONING_OUT = $out 
        $env.PROVISIONING_NO_TERMINAL = true
    }
    provisioning_init $helpinfo "cluster generate" $args
    #parse_help_command "cluster generate" $name --ismod --end
    # print "on cluster main generate"
    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 "generate " " " ) 
        let str_task = if  $name != null { 
            ($str_task | str replace $name "") 
        } else {
            $str_task
        } 
        ($str_task | str trim | split row " " | get -o 0 | default "" |
            split row "-" | get -o 0 | 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_generate = { 
        let curr_settings = (find_get_settings --infra $infra --settings $settings)
        $env.WK_CNPROV = $curr_settings.wk_path
        let match_name = if $name == null or $name == "" { "" } else { $name} 
        # on_clusters $curr_settings $check $wait $outfile $match_name $cluster_pos 
    }
    match $task {
        "" if $name == "h" => { 
            ^$"($env.PROVISIONING_NAME)" -mod cluster generate help --notitles
        },
        "" if $name == "help" => { 
            ^$"($env.PROVISIONING_NAME)" -mod cluster generate --help
            print (provisioning_options "generate")
        },
        "" => { 
            let result = desktop_run_notify $"($env.PROVISIONING_NAME) clusters generate" "-> " $run_generate --timeout 11sec
            #do $run_generate
        },
        _ => { 
            if $task != "" { print $"🛑 invalid_option ($task)" } 
            print $"\nUse (_ansi blue_bold)($env.PROVISIONING_NAME) -h(_ansi reset) for help on commands and options"
        }
    } 
    # "" | "generate"
    if not $env.PROVISIONING_DEBUG { end_run "" } 
} 
