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

# > Server generate
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
    --serverpos (-p): int   # Server position in settings
    --check (-c)     # Only check mode no servers will be generated
    --wait (-w)      # Wait servers 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 servers 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)
    --inputfile: string  # Input file
]: nothing -> nothing {
    if ($out | is-not-empty) {
        $env.PROVISIONING_OUT = $out
        $env.PROVISIONING_NO_TERMINAL = true
    }
    provisioning_init $helpinfo "servers generate" $args
    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.servers | 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 false true)
        $env.WK_CNPROV = $curr_settings.wk_path
        let match_name = if $name == null or $name == "" { "" } else { $name}
        on_generate_servers $curr_settings $check $wait $outfile $match_name $serverpos --inputfile $inputfile --select $select
    }
    match $task {
        "" if $name == "h" => {
            ^$"($env.PROVISIONING_NAME)" -mod server generate help --notitles
        },
        "" if $name == "help" => {
            ^$"($env.PROVISIONING_NAME)" -mod server generate --help
            _print (provisioning_options "generate")
        },
        "" | "g" | "generate" => {
            let result = desktop_run_notify $"($env.PROVISIONING_NAME) servers generate" "-> " $run_generate --timeout 11sec
            if not ($result | get -o status | default true) { exit 1 }
        },
        _ => {
            invalid_task "servers generate" $task --end
        }
    }
    if not $notitles and not $env.PROVISIONING_DEBUG { end_run "" }
}
export def on_generate_servers [
    settings: record   # Settings record
    check: bool        # Only check mode no servers will be generated
    wait: bool         # Wait for creation
    outfile?: string   # Out file for creation
    hostname?: string  # Server hostname in settings
    serverpos?: int    # Server position in settings
    --notitles         # not tittles
    --select: string   # Provider selection
    --inputfile: string # input file with data for no interctive input mode
]: nothing -> nothing {
    let match_hostname = if $hostname != null {
        $hostname
    } else if $serverpos != null  {
        let total = $settings.data.servers | length
        let pos = if $serverpos == -1 {
            _print $"Use number form 0 to ($total)"
            $serverpos
        } else if $serverpos <= $total {
            $serverpos - 0
        } else {
            (throw-error $"🛑 server pos" $"($serverpos) from ($total) servers"
                "on_generate" --span (metadata $serverpos).span)
            exit 0
        }
        ($settings.data.servers | get $pos).hostname
    }
    let providers_list = (providers_list "selection")
    if ($providers_list | length) == 0 {
        _print $"🛑 no providers found for (_ansi cyan)providers list(_ansi reset)"
        return
    }
    # let servers_path_0 = if ($settings.data.servers_paths | length) > 1 { #TODO }
    let servers_path_0 = ($settings.data.servers_paths | get -o 0)
    let servers_path = if ($servers_path_0 | str ends-with ".k") { $servers_path_0 } else { $"($servers_path_0).k"}
    #if not ($servers_path | path exists) {
        #(throw-error $"🛑 servers path" $"($servers_path) not found in ($settings.infra)"
        #       "on_generate" --span (metadata $servers_path).span)
        # exit 0
    #}
    #open -r $servers_path | str replace --multiline --regex '^]' ''  |
    #   save -f ($settings.wk_path | path join $"_($servers_path | path basename)")
    _print $"\n(_ansi green)PROVIDERS(_ansi reset) list: \n"
    let full_servers_path = if ($servers_path | str starts-with "/") {
        $servers_path
    } else {
        ($settings.src_path | path join $servers_path)
    }
    let target_path = ($full_servers_path | path dirname)
    mut $servers_length = ($settings.data.servers | length)
    while true {
        _print $"(_ansi yellow)($servers_length)(_ansi reset) servers "
        let servers_kcl = (open -r $full_servers_path | str replace --multiline --regex '^]' '')
        # TODO SAVE A COPY
        let item_select = if ($select | is-empty) {
            let selection_pos = ($providers_list | each {|it|
                match ($it.name | str length) {
                    2..5  =>   $"($it.name)\t\t ($it.info) \tversion: ($it.vers)",
                    _  =>   $"($it.name)\t ($it.info) \tversion: ($it.vers)",
                }
            } | input list --index (
                    $"(_ansi default_dimmed)Select one provider for (_ansi cyan_bold)new server(_ansi reset)" +
                    $" \(use arrow keys and press [enter] or [escape] to exit\)( _ansi reset)"
                )
            )
            if ($selection_pos | is-empty) { break }
            ($providers_list | get -o $selection_pos)
        } else {
            ($providers_list | where {|it| $it.name == $select} | get -o 0 | default {})
        }
        if ($item_select | is-not-empty) {
            let item_path = ($env.PROVISIONING_PROVIDERS_PATH | path join $item_select.name)
            if not ($item_path | path join $env.PROVISIONING_GENERATE_DIRPATH | path exists) {
                _print $"Path ($item_path | path join $env.PROVISIONING_GENERATE_DIRPATH) not found\n"
                continue
            }
            let template_path = ($item_path | path join $env.PROVISIONING_GENERATE_DIRPATH)
            let new_created = if not ($target_path | path join $"($item_select.name)_defaults.k" | path exists) {
                ^cp -pr ($template_path | path join $"($item_select.name)_defaults.k.j2") ($target_path)
                _print $"copy (_ansi green)($item_select.name)_defaults.k.j2(_ansi reset) to (_ansi green)($settings.infra)(_ansi reset)"
                true
            } else {
                false
            }
            if not ($full_servers_path | path exists) or ($servers_kcl | is-empty) or $servers_length == 0 {
                ($"import ($item_select.name)_prov\nservers = [\n" + (open -r ($template_path | path join "servers.k.j2")) + "\n]" )
                    | save -f $"($full_servers_path).j2"
                _print $"create (_ansi green)($item_select.name) servers.k.j2(_ansi reset) to (_ansi green)($settings.infra)(_ansi reset)"
            } else {
                let head_text = if not ($servers_kcl | str contains $"import ($item_select.name)") {
                    $"import ($item_select.name)_prov\n"
                } else {"" }
                print $"import ($item_select.name)"
                print $head_text
                ($head_text + $servers_kcl + (open -r ($template_path | path join "servers.k.j2")) + "\n]" )
                    | save -f $"($full_servers_path).j2"
                _print $"add (_ansi green)($item_select.name) servers.k.j2(_ansi reset) to (_ansi green)($settings.infra)(_ansi reset)"
            }
            generate_data_def $item_path $settings.infra ($settings.src_path | path join ($full_servers_path | path dirname)) $new_created $inputfile
            # TODO CHECK if compiles KCL OR RECOVERY
            # TODO  ADD tasks for server
            if ($inputfile | is-not-empty) { break }
            $servers_length += 1
        } else {
            #(open -r $servers_path) + "\n]" | save -f $servers_path
            break
        }
    }
}
export def generate_server [
    server: record
    index: int
    check: bool
    wait: bool
    settings: record
    outfile?: string
]: nothing -> bool {
    ## Provider middleware now available through lib_provisioning
    #use utils.nu *
    let server_info = (mw_server_info $server true)
    let already_generated = ($server_info | get -o hostname | is-not-empty)
    if ($already_generated) {
        _print $"Server (_ansi green_bold)($server.hostname)(_ansi reset) already generated "
        check_server $settings $server $index $server_info $check $wait $settings $outfile
        #mw_server_info $server false
        if not $check { return true }
    }
    let server_template = ($env.PROVISIONING | path join "providers" | path join $server.provider | path join templates |
        path join $"($server.provider)_servers.j2"
    )
    let generate_result = on_server_template $server_template $server $index $check false $wait $settings $outfile
    if $check { return true }
    if not $generate_result { return false }
    check_server $settings $server $index $server_info $check $wait $settings $outfile
    true
}

export def verify_server_info [
    settings: record
    server: record
    info: record
]: nothing -> nothing {
    _print $"Checking server (_ansi green_bold)($server.hostname)(_ansi reset) info "
    let server_plan = ($server | get -o plan | default "")
    let curr_plan = ($info | get -o plan | default "")
    if ($server_plan | is-not-empty) {
        if $server_plan != $curr_plan {
            mw_modify_server $settings $server [{plan: $server_plan}] false
        }
    }
}
export def check_server [
    settings: record
    server: record
    index: int
    info: record
    check: bool
    wait: bool
    settings: record
    outfile?: string
]: nothing -> bool {
    ## Provider middleware now available through lib_provisioning
    #use utils.nu *
    let server_info = if ($info | is-empty) {
        (mw_server_info $server true)
    } else {
        $info
    }
    let already_generated = ($server_info | is-not-empty)
    if not $already_generated {
        _print $"🛑 server (_ansi green_bold)($server.hostname)(_ansi reset) not exists"
        return false
    }
    if not $check {
        ^ssh-keygen -f $"($env.HOME)/.ssh/known_hosts" -R $server.hostname err> (if $nu.os-info.name == "windows" { "NUL" } else { "/dev/null" })
        let ip = (mw_get_ip $settings $server $server.liveness_ip false )
        if $ip == "" {
            _print "🛑 No liveness ip found for state checking "
            return false
        }
        verify_server_info $settings $server $server_info
        _print $"liveness (_ansi purple)($ip):($server.liveness_port)(_ansi reset)"
        if (wait_for_server $index $server $settings $ip) {
            on_server_ssh $settings $server "pub" "generate" false
            # collect fingerprint
            let res = (^ssh-keyscan "-H" $ip err> (if $nu.os-info.name == "windows" { "NUL" } else { "/dev/null" })| complete)
            if $res.exit_code == 0 {
                let known_hosts_path = (("~" | path join ".ssh" | path join "known_hosts") | path expand)
                let markup = $"# ($ip) keyscan"
                let lines_found = (open $known_hosts_path --raw | lines |  find $markup | length)
                if $lines_found == 0 {
                    ( $"($markup)\n" | save --append $known_hosts_path)
                    ($res.stdout | save --append $known_hosts_path)
                    _print $"(_ansi green_bold)($ip)(_ansi reset) (_ansi yellow)ssh-keyscan(_ansi reset) added to ($known_hosts_path)"
                }
            #} else {
            #   _print $"🛑 Error (_ansi yellow)ssh-keyscan(_ansi reset) from ($ip)"
            #   _print $"($res.stdout)"
            }
            if $already_generated {
                let res = (mw_post_generate_server $settings $server $check)
                match $res {
                    "error" | "-1" => { exit 1},
                    "storage" | "" => {
                        let storage_sh = ($settings.wk_path | path join $"($server.hostname)-storage.sh")
                        let result = (on_server_template ($env.PROVISIONING_TEMPLATES_PATH | path join "storage.j2") $server 0 true true true $settings $storage_sh)
                        if $result and ($storage_sh | path exists) and (wait_for_server $index $server $settings $ip) {
                            let target_cmd = "/tmp/storage.sh"
                            #use ssh.nu scp_to ssh_cmd
                            if not (scp_to $settings $server [$storage_sh] $target_cmd $ip) { return false }
                            _print $"Running (_ansi blue_italic)($target_cmd | path basename)(_ansi reset) in (_ansi green_bold)($server.hostname)(_ansi reset)"
                            if not (ssh_cmd $settings $server true $target_cmd $ip) { return false }
                            if $env.PROVISIONING_SSH_DEBUG? != null and $env.PROVISIONING_SSH_DEBUG { return true }
                            if not $env.PROVISIONING_DEBUG {
                                (ssh_cmd $settings $server false $"rm -f ($target_cmd)" $ip)
                            }
                        } else {
                            return false
                        }
                    }
                    _  => {
                        return true
                    },
                }
            }
        }
    }
    true
}
