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

# > TaskServs update 
export def "main update" [
  name?: string       # task in settings
  server?: string     # Server hostname in settings
  ...args                 # Args for update command 
  --infra (-i): string     # Infra directory 
  --settings (-s): string # Settings path 
  --iptype: string = "public" # Ip type to connect
  --outfile (-o): string  # Output file
  --taskserv_pos (-p): int   # Server position in settings 
  --check (-c)     # Only check mode no taskservs will be created 
  --wait (-w)      # Wait taskservs to be updated
  --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 taskservs 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 "taskserv update" $args
    if $debug { $env.PROVISIONING_DEBUG = true } 
    if $metadata { $env.PROVISIONING_METADATA = true } 
    let curr_settings = (find_get_settings --infra $infra --settings $settings)
    let task = if ($args | length) > 0 { 
        ($args| get 0) 
    } else { 
        let str_task = (($env.PROVISIONING_ARGS? | default "") | str replace "update " " " ) 
        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_update = { 
        let curr_settings = (settings_with_env (find_get_settings --infra $infra --settings $settings))
        $env.WK_CNPROV = $curr_settings.wk_path
        let arr_task = if $name == null or $name == "" or $name == $task { [] } else { $name | split row "/" } 
        let match_task = if ($arr_task | length ) == 0 { "" } else { ($arr_task | get -o 0) } 
        let match_task_profile = if ($arr_task | length ) < 2 { "" } else { ($arr_task | get -o 1) } 
        let match_server = if $server == null or $server == "" { "" } else { $server} 
        on_taskservs  $curr_settings $match_task $match_task_profile $match_server $iptype $check
    }
    match $task {
        "" if $name == "h" => { 
            ^$"($env.PROVISIONING_NAME)" -mod taskserv update help --notitles
        },
        "" if $name == "help" => { 
            ^$"($env.PROVISIONING_NAME)" -mod taskserv update --help
            print (provisioning_options "update")
        },
        "" | "u" | "update" => { 
            let result = desktop_run_notify $"($env.PROVISIONING_NAME) taskservs update" "-> " $run_update --timeout 11sec
            #do $run_update
        },
        _ => { 
            if $task != "" { print $"🛑 invalid_option ($task)" } 
            _print $"\nUse (_ansi blue_bold)($env.PROVISIONING_NAME) -h(_ansi reset) for help on commands and options"
        }
    } 
    if not $env.PROVISIONING_DEBUG { end_run "" } 
} 
