use utils.nu *
use lib_provisioning *
use run.nu *
use check_mode.nu *
use ../lib_provisioning/config/accessor.nu *
use ../lib_provisioning/utils/hints.nu *

#use ../extensions/taskservs/run.nu run_taskserv
def install_from_server  [
    defs: record
    server_taskserv_path: string
    wk_server: string
] {
    _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)"
    )
    let run_taskservs_path = (get-run-taskservs-path)
    (run_taskserv $defs
        ($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
] {
    _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)"
    )
    let base = (get-taskservs-path)
    let name = $defs.taskserv.name
    # Resolve the script directory with profile → mode fallback chain:
    #   1. Exact profile name (e.g. "k0sctl")
    #   2. "taskserv" (canonical mode dir — was "default/" pre-migration)
    #   3. Error with actionable message
    let profile  = $defs.taskserv_profile
    let by_profile = ($base | path join $name | path join $profile)
    let by_taskserv = ($base | path join $name | path join "taskserv")
    let lib_path = if ($by_profile | path exists) {
        $by_profile
    } else if ($by_taskserv | path exists) {
        if $profile != "default" {
            _print $"(_ansi yellow)⚠ profile '($profile)' not found for ($name), falling back to taskserv/(_ansi reset)"
        }
        $by_taskserv
    } else {
        error make { msg: $"No script directory for component '($name)': tried ($by_profile) and ($by_taskserv)" }
    }
    ( run_taskserv $defs $lib_path ($wk_server | path join $name) )
}

export def on_taskservs [
    settings: record
    match_taskserv: string
    match_taskserv_profile: string
    match_server: string
    iptype: string
    check: bool
] {
    _print $"Running (_ansi yellow_bold)taskservs(_ansi reset) ..."
    let provisioning_sops = ($env.PROVISIONING_SOPS? | default "")
    if $provisioning_sops == ""  {
        # 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 (is-debug-enabled) { "bash -x" } else { "" }
    $settings.data.servers
    | enumerate
    | where {|it|
        $match_server == "" or $it.item.hostname == $match_server
    }
    | each {|it|
        let server_pos = $it.index
        let srvr = $it.item
        _print $"on (_ansi green_bold)($srvr.hostname)(_ansi reset) pos ($server_pos) ..."
        let clean_created_taskservs = ($settings.data.servers | get $server_pos? | default $dflt_clean_created_taskservs)

        # Determine IP address
        let ip = if (is-debug-check-enabled) or $check {
            "127.0.0.1"
        } else {
            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) "
                null
            } else {
                let network_public_ip = ($srvr | get 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)"
                }

                # Check if server is in running state
                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)"
                    null
                } else {
                    $curr_ip
                }
            }
        }

        # Process server only if we have valid IP
        if ($ip != null) {
        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
        | where {|it|
            let taskserv = $it.item
            let matches_taskserv = ($match_taskserv == "" or $match_taskserv == $taskserv.name)
            let matches_profile = ($match_taskserv_profile == "" or $match_taskserv_profile == $taskserv.profile)
            $matches_taskserv and $matches_profile
        }
        | each {|it|
            let taskserv = $it.item
            let taskserv_pos = $it.index
            let taskservs_path = (get-taskservs-path)

            # Check if taskserv path exists - skip if not found
            if not ($taskservs_path | path join $taskserv.name | path exists) {
                _print $"taskserv path: ($taskservs_path | path join $taskserv.name) (_ansi red_bold)not found(_ansi reset)"
            } else {
                # Taskserv path exists, proceed with processing
                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 }

                # Enhanced check mode
                if $check {
                    let check_result = (run-check-mode $taskserv.name $taskserv_profile $settings $server --verbose=(is-debug-enabled))
                    if $check_result.overall_valid {
                        # Check passed, proceed (no action needed, validation was successful)
                    } else {
                        _print $"(_ansi red)⊘ Skipping deployment due to validation errors(_ansi reset)"
                    }
                } else {
                    # Normal installation mode
                    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
    }

    # Show next-step hints after successful taskserv installation
    if not $check and ($match_taskserv | is-not-empty) {
        show-next-step "taskserv_create" {name: $match_taskserv}
    }

    true
}
