use utils.nu *
use lib_provisioning *
use taskservs/run.nu *

#use taskservs/run.nu run_taskserv
def install_from_server  [
    defs: record
    server_taskserv_path: string
    wk_server: string
]: nothing -> bool {
    _print (
        $"(_ansi yellow_bold)($defs.taskserv.name)(_ansi reset) (_ansi default_dimmed)on(_ansi reset) " + 
        $"($defs.server.hostname) (_ansi default_dimmed)install(_ansi reset) " +
        $"(_ansi purple_bold)from ($defs.taskserv_install_mode)(_ansi reset)"
    )
    (run_taskserv $defs 
        ($env.PROVISIONING_RUN_TASKSERVS_PATH | path join $defs.taskserv.name | path join $server_taskserv_path) 
        ($wk_server | path join $defs.taskserv.name)
    )
}
def install_from_library  [
    defs: record
    server_taskserv_path: string
    wk_server: string
]: nothing -> bool {
    _print (
        $"(_ansi yellow_bold)($defs.taskserv.name)(_ansi reset) (_ansi default_dimmed)on(_ansi reset) " + 
        $"($defs.server.hostname) (_ansi default_dimmed)install(_ansi reset) " + 
        $"(_ansi purple_bold)from library(_ansi reset)"
    )
    ( run_taskserv $defs
        ($env.PROVISIONING_TASKSERVS_PATH |path join $defs.taskserv.name | path join $defs.taskserv_profile) 
        ($wk_server | path join $defs.taskserv.name)
    )
}

export def on_taskservs [
    settings: record
    match_taskserv: string  
    match_taskserv_profile: string  
    match_server: string  
    iptype: string
    check: bool
]: nothing -> bool {
    _print $"Running (_ansi yellow_bold)taskservs(_ansi reset) ..."
    if $env.PROVISIONING_SOPS? == null  {
        # A SOPS load env 
        $env.CURRENT_INFRA_PATH = ($settings.infra_path | path join $settings.infra)
        use sops_env.nu  
    }
    let ip_type = if $iptype == "" { "public" } else { $iptype }
    let str_created_taskservs_dirpath = ( $settings.data.created_taskservs_dirpath | default (["/tmp"] | path join) | 
        str replace "./" $"($settings.src_path)/" | str replace "~" $env.HOME | str replace "NOW" $env.NOW 
    )
    let created_taskservs_dirpath = if ($str_created_taskservs_dirpath | str starts-with "/" ) { $str_created_taskservs_dirpath } else { $settings.src_path | path join $str_created_taskservs_dirpath }
    let root_wk_server = ($created_taskservs_dirpath | path join "on-server")
    if not ($root_wk_server | path exists ) { ^mkdir "-p" $root_wk_server }
    let dflt_clean_created_taskservs = ($settings.data.clean_created_taskservs? | default $created_taskservs_dirpath | 
        str replace "./" $"($settings.src_path)/" | str replace "~" $env.HOME 
    )
    let run_ops = if  $env.PROVISIONING_DEBUG { "bash -x" } else { "" }
    $settings.data.servers | enumerate | each {|it| 
        let server_pos = $it.index
        let srvr = $it.item
        if $match_server != "" and $srvr.hostname != $match_server { continue } 
        _print $"on (_ansi green_bold)($srvr.hostname)(_ansi reset) pos ($server_pos) ..."
        let clean_created_taskservs = ($settings.data.servers | get -o $server_pos | get -o clean_created_taskservs | default $dflt_clean_created_taskservs )
        let ip = if $env.PROVISIONING_DEBUG_CHECK or $check { 
            "127.0.0.1"
        } else { 
            #  use ../../../providers/prov_lib/middleware.nu  mw_get_ip 
            let curr_ip = (mw_get_ip $settings $srvr $ip_type false | default "")
            if $curr_ip == "" { 
                _print $"🛑 No IP ($ip_type) found for (_ansi green_bold)($srvr.hostname)(_ansi reset) ($server_pos) " 
                continue
            }
            let network_public_ip = ($srvr | get -o network_public_ip | default "") 
            if ($network_public_ip | is-not-empty) and  $network_public_ip != $curr_ip { 
                _print $"🛑 IP ($network_public_ip) not equal to ($curr_ip) in (_ansi green_bold)($srvr.hostname)(_ansi reset)" 
            }
            #use utils.nu wait_for_server 
            if not (wait_for_server $server_pos $srvr $settings $curr_ip) { 
                _print $"🛑 server ($srvr.hostname) ($curr_ip)  (_ansi red_bold)not in running state(_ansi reset)" 
                continue  
            }
            $curr_ip
        }
        let server = ($srvr | merge { ip_addresses: { pub: $ip, priv: $srvr.network_private_ip }})
        let wk_server  = ($root_wk_server | path join $server.hostname)
        if ($wk_server | path exists )  { rm -rf  $wk_server }
        ^mkdir "-p" $wk_server
        $server.taskservs | enumerate | each {|it| 
            let taskserv = $it.item
            let taskserv_pos = $it.index
            if $match_taskserv  !=  "" and $match_taskserv != $taskserv.name { continue } 
            if $match_taskserv_profile  !=  "" and $match_taskserv_profile != $taskserv.profile { continue } 
            if not ($env.PROVISIONING_TASKSERVS_PATH | path join $taskserv.name | path exists) { 
                _print $"taskserv path: ($env.PROVISIONING_TASKSERVS_PATH | path join $taskserv.name) (_ansi red_bold)not found(_ansi reset)" 
                continue
            }
            if not ($wk_server | path join $taskserv.name| path exists)  { ^mkdir "-p" ($wk_server | path join $taskserv.name) }
            let $taskserv_profile = if $taskserv.profile == "" { "default" } else { $taskserv.profile }
            let $taskserv_install_mode = if $taskserv.install_mode == "" { "library" } else { $taskserv.install_mode }
            let server_taskserv_path = ($server.hostname | path join $taskserv_profile)
            let defs = { 
                settings: $settings, server: $server, taskserv: $taskserv, 
                taskserv_install_mode: $taskserv_install_mode, taskserv_profile: $taskserv_profile,
                pos: { server: $"($server_pos)", taskserv: $taskserv_pos}, ip: $ip, check: $check }
            match $taskserv.install_mode {
                "server" | "getfile" => { 
                    (install_from_server $defs $server_taskserv_path $wk_server )
                },
                "library-server" => { 
                    (install_from_library $defs $server_taskserv_path $wk_server)
                    (install_from_server $defs $server_taskserv_path $wk_server )
                },
                "server-library" => { 
                    (install_from_server $defs $server_taskserv_path $wk_server )
                    (install_from_library $defs $server_taskserv_path $wk_server)
                },
                "library" => { 
                    (install_from_library $defs $server_taskserv_path $wk_server)
                },
            }
            if $clean_created_taskservs == "yes" { rm -rf ($wk_server | pth join $taskserv.name) }
        }
        if $clean_created_taskservs == "yes" { rm -rf $wk_server }
        _print $"Tasks completed on ($server.hostname)"
    }
    if ("/tmp/k8s_join.sh"  | path exists) { cp "/tmp/k8s_join.sh" $root_wk_server ;  rm -r /tmp/k8s_join.sh }
    if $dflt_clean_created_taskservs == "yes" { rm -rf $root_wk_server }
    _print $"✅ Tasks (_ansi green_bold)completed(_ansi reset) ($match_server) ($match_taskserv) ($match_taskserv_profile) ....."
    if not $check and ($match_server | is-empty) {
        #use utils.nu servers_selector
        servers_selector $settings $ip_type false
    }
    true
}
