def provider_lib_has_method [
    providers_path: string
    prov: string
    method: string
]: nothing -> bool {
    let prov_root = ($providers_path | path join $prov | path join "nulib" | path join $prov)
    let res = (^grep $method ...(glob ($prov_root | path join "*")) err> /dev/null | complete) 
    ($res.stdout | is-not-empty)
}

def make_provider_undefined [
    providers_path: string
    providers_list: list<string>
]: nothing -> string {
'def provider_undefined [
    server: record
] { 
    #use defs/lists.nu providers_list
    let str_providers_list = (providers_list | each { |it| $it.name} | str join " ")
    print ($"(_ansi blue_bold)($server.hostname)(_ansi reset) with provider " +
       $"(_ansi green_bold)($server.provider)(_ansi reset) ($server.zone) ")
    let text = ( $"expected to be one of available providers [(_ansi green_italic)($str_providers_list)(_ansi reset)], " +
        $"got (_ansi green_bold)($server.provider)(_ansi reset)")
    print $"Error 🛑 provider ($text)"
}'
} 
def make_mw_query_servers [
    providers_path: string
    providers_list: list<string>
]: nothing -> string {
    mut output = '
export def mw_query_servers [
    settings: record
    find?: string
    cols?: string
    --prov: string
    --serverpos: int
] {
    let str_find = if $find != null { $find } else { "" }
    let str_cols = if $cols != null { $cols } else { "" }
    $settings.data.servers | enumerate | each { |it| 
    #let res = for idx in ..($settings.data.servers | length) { 
        #let srv = ($settings.data.servers | get -o $idx) 
        if $prov == null or $it.item.provider == $prov {
            if $serverpos == null or $serverpos == $it.index  {
                let res = match $it.item.provider {'    
for prov in $providers_list { 
    let method = $"($prov)_query_servers"
    if not (provider_lib_has_method $providers_path $prov $method) { continue }
    $output = ($output | append $'
                    "($prov)" => {
                        ($"\(($method) $str_find $str_cols)")
                    },' | str join "") 
}
$output | append '
                    _ => {
                        provider_undefined $it.item
                        []
                    }
                }
                if ($res | length) > 0 { 
                    let result = if $str_find != "" { 
                        $res | find $str_find
                    } else { 
                        $res
                    }
                    if $str_cols != "" { 
                        let field_list = ($str_cols | split row ",") 
                        ($result | select -o $field_list) 
                    } else {
                        $result
                    }
                }
            }
        }
     # $list | append  $srv
    } | flatten
}' | str join ""
}
def make_mw_servers_ips [
   providers_path: string
   providers_list: list<string>
]: nothing -> string {
    mut output = '
export def mw_servers_ips [
    settings: record
    data: list
    prov?: string
    serverpos?: int
]: nothing -> list { 
    mut index = -1
    mut result = []
    for srv in $data {
        $index += 1
        let settings_server = ($settings.data.servers | where {|it| $it.hostname == $srv.hostname})
        if ($settings_server | length) == 0 { continue }
        let provider = ($settings_server | get -o provider | default "") 
        if $prov != null and $provider != $prov { continue }
        if $serverpos != null and $serverpos != $index  { continue }
        match $provider { '
for prov in $providers_list {
    $output = ($output | append $'
            "($prov)" => {' | append '
                if $srv.ip_addresses? != null {
                    $result = ($result | append ($srv.ip_addresses? |
                        each {|it| { hostname: $srv.hostname, ip: $it.address, access: $it.access, family: $it.family }} |
                        flatten
                    ))
                }
            },' | str join "")
}
$output | append ' 
            _ => {
                provider_undefined $srv.provider
                []
            }
        }
    }
    $result
} ' | str join ""
}
def make_mw_server_info [
   providers_path: string
   providers_list: list<string>
]: nothing -> string {
    mut output = '
export def mw_server_info [
    server: record
    check: bool
    find?: string
    cols?: string
]: nothing -> record { 
    let str_find = if $find != null { $find } else { "" }
    let str_cols = if $cols != null { $cols } else { "" }
    let res = match $server.provider { '
for prov in $providers_list {
    let method = $"($prov)_server_info"
    if not (provider_lib_has_method $providers_path $prov $method) { continue }
    $output = ($output | append $'
        "($prov)" => {
            ($"\(($method) $server $check)")
        },' | str join "")
}
$output = ($output | append '
        _ => {
           provider_undefined $server.hostname
           []
        }
    }
    if $res.hostname? != null { 
        let result = if $str_find != "" { 
            $res | find $str_find
        } else { 
            $res
        }
        let info = if $str_cols != "" { 
            let field_list = ($str_cols | split row ",") 
            ($result | select -o $field_list)
        } else {
            ($result)
        }
        let priv = match $server.provider {' | str join "")
for prov in $providers_list {
    if $prov == "aws" { 
        $output = ($output | append '
            "aws" => {
                ($info | get -o private_ips | default [] | each {|it| ($it | select Description PrivateIpAddress VpcId SubnetId Groups) }) 
            },' | str join "") 
    } 
}
$output | append '
            _ => ($info | get -o priv | default "")
        }
        let full_info = if ($priv | length) > 0 { 
            ($info | merge { private_ips: $priv })
        } else { 
            $info
        }
        if not $check { 
            print ($full_info | table -e)
        }
        $full_info
    } else {
        $res
    }
} ' | str join ""
}
def make_mw_servers_info [
   providers_path: string
   providers_list: list<string>
]: nothing -> string {
'
export def mw_servers_info [
    settings: record
    find?: string
    cols?: string
    --prov: string
    --serverpos: int
    --check
]: nothing -> nothing {
    let str_find = if $find != null { $find } else { "" }
    let str_cols = if $cols != null { $cols } else { "" }
    $settings.data.servers | enumerate | each { |it| 
        if $prov == null or $it.item.provider == $prov {
            if $serverpos == null or $serverpos == $it.index  {
                mw_server_info $it.item $check $str_find $str_cols
            }
        }
    }
}'
}
def make_mw_create_server [
   providers_path: string
   providers_list: list<string>
]: nothing -> string {
    mut output = '
export def mw_create_server [
    settings: record
    server: record
    check: bool
    error_exit: bool
]: nothing -> bool { 
    let zone = $server.zone? | default ""
    let res = match $server.provider { '
for prov in $providers_list {
    let method = $"($prov)_check_server_requirements"
    if not (provider_lib_has_method $providers_path $prov $method) { continue }
    $output = ($output | append $'
        "($prov)" => {
            print ($"\(($prov)_on_prov_server $server)")
            ($"\(($method) $settings $server $check)")
        },' | str join "")
}
$output | append '
        _ => {
           provider_undefined $server
           if $error_exit { exit } else { false }
        }
    }
    if not $res { 
        (throw-error $"🛑 ($server.provider) check requirements error" 
        $"for server ($server.hostname)"
        "create_server" --span (metadata $server.provider).span)
        return false   
    }
    print ($"Create (_ansi blue_bold)($server.hostname)(_ansi reset) with provider " +
       $"(_ansi green_bold)($server.provider)(_ansi reset) ($zone) ")
    return true
} ' | str join ""
}
def make_mw_server_state [
   providers_path: string
   providers_list: list<string>
]: nothing -> string {
    mut output = '
export def mw_server_state [
    server: record
    new_state: string
    error_exit: bool
    wait: bool
    settings: record
]: nothing -> bool {
    match $server.provider { '
for prov in $providers_list {
    let method = $"($prov)_server_state"
    if not (provider_lib_has_method $providers_path $prov $method) { continue }
    $output = ($output | append $'
        "($prov)" => {
           ($"\(($method) $server $new_state $error_exit $wait $settings)")
        },' | str join "")
}
$output | append '
        _ => {
            provider_undefined $server
            if $error_exit { exit } else { return false }
        }
    }
    true
} ' | str join ""
}
def make_mw_server_exists [
   providers_path: string
   providers_list: list<string>
]: nothing -> string {
    mut output = '
export def mw_server_exists [
    server: record
    error_exit: bool
]: nothing -> bool { 
    match $server.provider {'
for prov in $providers_list {
    let method = $"($prov)_server_exists"
    if not (provider_lib_has_method $providers_path $prov $method) { continue }
    $output = ($output | append $'
       "($prov)" => {
            ($"\(($method) $server $error_exit)")
        },' | str join "")
}
$output | append '
        _ => {
            provider_undefined $server
            if $error_exit { exit } else { false }
        }
    }
} ' | str join ""
}
def make_mw_server_is_running [
   providers_path: string
   providers_list: list<string>
]: nothing -> string {
    mut output = '
export def mw_server_is_running [
    server: record
    error_exit: bool
]: nothing -> bool { 
    match $server.provider { '
for prov in $providers_list {
    let method = $"($prov)_server_is_running"
    if not (provider_lib_has_method $providers_path $prov $method) { continue }
    $output = ($output | append $'
        "($prov)" => {
            ($"\(($method) $server $error_exit)")
        },' | str join "")
}
$output | append '
        _ => {
            provider_undefined $server
            if $error_exit { exit } else { false }
        }
    }
} ' | str join ""
}
def make_mw_get_ip [
   providers_path: string
   providers_list: list<string>
]: nothing -> string {
    mut output = '
export def mw_get_ip [
    settings: record
    server: record
    ip_type: string
    error_exit: bool
]: nothing -> string { 
    let use_type = match $ip_type {
        "$network_public_ip" => "public",
        "$network_private_ip" => "private",
        _ => $ip_type
    }
    let res = match $server.provider { '
for prov in $providers_list {
    let method = $"($prov)_server"
    if not (provider_lib_has_method $providers_path $prov $method) { continue }
    $output = ($output | append $'
        "($prov)" => {
           ($"\(($method) [ "get_ip", $use_type ] --server $server --settings $settings)")
        },' | str join "")
}
$output | append '
        _ => {
            provider_undefined $server
            if $error_exit { exit } else { "" }
        }
    }
    $"($res)" | str trim
} ' | str join ""
}
def make_mw_wait_storage [
   providers_path: string
   providers_list: list<string>
]: nothing -> string {
    mut output = '
export def mw_wait_storage [
   settings: record
   server: record
   new_state: str
   id: str
]: nothing -> record { 
    match $server.provider { '
for prov in $providers_list {
    let method = $"($prov)_wait_storage"
    if not (provider_lib_has_method $providers_path $prov $method) { continue }
    let str_it = "$server"
    $output = ($output | append $'
        "($prov)" => {
           ($"\(($method) $settings $server $new_state $id)")
        },' | str join "")
}
$output | append '
        _ => {
            provider_undefined $server.provider
            true
        }
    } 
} ' | str join ""
}
def make_mw_create_storage [
   providers_path: string
   providers_list: list<string>
]: nothing -> string {
    mut output = '
export def mw_create_storage [
   settings: record
   server: record
   server_info: record
   storage: record
   volumes: list
   total_size: int
]: nothing -> record { 
    match $server.provider { '
for prov in $providers_list {
    let method = $"($prov)_create_storage"
    if not (provider_lib_has_method $providers_path $prov $method) { continue }
    let str_it = "$server"
    $output = ($output | append $'
        "($prov)" => {
           ($"\(($method) $settings $server $server_info $storage $volumes $total_size)")
        },' | str join "")
}
$output | append '
        _ => {
            provider_undefined $server.provider
            true
        }
    } 
} ' | str join ""
}
def make_mw_post_create_server [
   providers_path: string
   providers_list: list<string>
]: nothing -> string {
    mut output = '
export def mw_post_create_server [
    settings: record
    server: record
    check: bool 
]: nothing -> bool { 
    match $server.provider { '
for prov in $providers_list {
    let method = $"($prov)_post_create_server"
    if not (provider_lib_has_method $providers_path $prov $method) { continue }
    let str_it = "$server"
    $output = ($output | append $'
        "($prov)" => {
           ($"\(($method) $settings ($str_it) $check)")
        },' | str join "")
}
$output | append '
        _ => {
            provider_undefined $server.provider
            true
        }
    } 
} ' | str join ""
}
def make_mw_modify_server [
   providers_path: string
   providers_list: list<string>
]: nothing -> string {
    mut output = '
export def mw_modify_server [
    settings: record
    server: record
    new_values: list
    error_exit: bool
]: nothing -> bool { 
    match $server.provider { '
for prov in $providers_list {
    let method = $"($prov)_modify_server"
    if not (provider_lib_has_method $providers_path $prov $method) { continue }
    let str_it = "$server"
    $output = ($output | append $'
        "($prov)" => {
           ($"\(($method) $settings ($str_it) $new_values $error_exit)")
        },' | str join "")
}
$output | append '
        _ => {
            provider_undefined $server.provider
            true
        }
    } 
} ' | str join ""
}
def make_mw_delete_server_storage [
   providers_path: string
   providers_list: list<string>
]: nothing -> string {
    mut output = '
export def mw_delete_server_storage [
    settings: record
    server: record
    error_exit: bool
]: nothing -> bool { 
    let zone = $server.zone? | default ""
    match $server.provider { '
for prov in $providers_list {
    let method = $"($prov)_delete_server_storage"
    if not (provider_lib_has_method $providers_path $prov $method) { continue }
    $output = ($output | append $'
        "($prov)" => {
            print ($"\(($prov)_on_prov_server $server)")
            ($"\(($method) $settings $server $error_exit)")
        },' | str join "")
}
$output | append '
        _ => {
            provider_undefined $server
            if $error_exit { exit } else { false }
        }
    }
} ' | str join ""
    #print ($"Delete storage (_ansi blue_bold)($server.hostname)(_ansi reset) with provider " +
    #    $"(_ansi green_bold)($server.provider)(_ansi reset) ($zone) ")
    #true
}
def make_mw_delete_server [
   providers_path: string
   providers_list: list<string>
]: nothing -> string {
    mut output = '
export def mw_delete_server [
    settings: record
    server: record
    keep_storage: bool
    error_exit: bool
]: nothing -> bool { 
    let zone = $server.zone? | default ""
    match $server.provider { '
for prov in $providers_list {
    let method = $"($prov)_delete_server"
    if not (provider_lib_has_method $providers_path $prov $method) { continue }
    $output = ($output | append $'
        "($prov)" => {
            print ($"\(($prov)_on_prov_server $server)")
            ($"\(($method) $settings $server $keep_storage $error_exit)")
        },' | str join "")
}
$output | append '
        _ => {
            provider_undefined $server
            if $error_exit { exit } else { false }
        }
   }
} ' | str join ""
   #print ($"Delete (_ansi blue_bold)($server.hostname)(_ansi reset) with provider " +
   #$"(_ansi green_bold)($server.provider)(_ansi reset) ($zone) ")
   #true
}
def make_mw_load_infra_servers_info [
   providers_path: string
   providers_list: list<string>
]: nothing -> string {
    mut output = '
export def mw_load_infra_servers_info [
    settings: record
    server: record
    error_exit: bool
]: nothing -> record { 
    match $server.provider { '
for prov in $providers_list {
    let method = $"($prov)_load_infra_servers_info"
    if not (provider_lib_has_method $providers_path $prov $method) { continue }
    $output = ($output | append $'
        "($prov)" => {
            ($"\(($method) $settings $server $error_exit)")
        },' | str join "")
}
$output | append '
        _ => {
            provider_undefined $server
            if $error_exit { exit } else { {} }
        }
   }
} ' | str join "\n"
}
def make_mw_load_infra_storages_info [
   providers_path: string
   providers_list: list<string>
]: nothing -> string {
    mut output = '
export def mw_load_infra_storages_info [
    settings: record
    server: record
    error_exit: bool
]: nothing -> record { 
    match $server.provider { '
for prov in $providers_list {
    let method = $"($prov)_load_infra_storages_info"
    if not (provider_lib_has_method $providers_path $prov $method) { continue }
    $output = ($output | append $'
        "($prov)" => {
            ($"\(($method) $settings $server $error_exit)")
        },' | str join "")
}
$output | append '
        _ => {
           provider_undefined $server
            if $error_exit { exit } else { {} }
        }
    }
} ' | str join "\n"
}
def make_mw_get_infra_storage [
   providers_path: string
   providers_list: list<string>
]: nothing -> string {
    mut output = '
export def mw_get_infra_storage [
    server: record
    settings: record
    cloud_data: record
    error_exit: bool
]: nothing -> list  { 
    match $server.provider { '
for prov in $providers_list {
    let method = $"($prov)_get_item_for_storage"
    if not (provider_lib_has_method $providers_path $prov $method) { continue }
    $output = ($output | append $'
        "($prov)" => {
           ($"\(($method) $server $settings $cloud_data)")
        },' | str join "")
}
$output | append '
        _ => {
            provider_undefined $server
            if $error_exit { exit } else { [] }
        }
    }
} ' | str join ""
}
def make_mw_get_infra_item [
   providers_path: string
   providers_list: list<string>
]: nothing -> string {
    mut output = '
export def mw_get_infra_item [
    server: record
    settings: record
    cloud_data: record
    error_exit: bool
]: nothing -> record { 
    match $server.provider { '
for prov in $providers_list {
    let method = $"($prov)_get_item_for_server"
    if not (provider_lib_has_method $providers_path $prov $method) { continue }
    $output = ($output | append $'
        "($prov)" => {
           ($"\(($method) $server $settings $cloud_data)")
        },' | str join "")
}
$output | append '
        _ => {
            provider_undefined $server
            if $error_exit { exit } else { return {} }
        }
    }
} ' | str join ""
}
def make_mw_get_infra_price [
   providers_path: string
   providers_list: list<string>
]: nothing -> string {
    mut output = '
export def mw_get_infra_price [
    server: record 
    data: record
    key:  string
    error_exit: bool
    price_col?: string
]: nothing -> float { 
    if ($data | get -o item | is-empty) { return {} }
    match $server.provider { '
for prov in $providers_list {
    let method = $"($prov)_get_price"
    if not (provider_lib_has_method $providers_path $prov $method) { continue }
    $output = ($output | append $'
        "($prov)" => {
           ($"\(($method) $data $key $price_col)")
       },' | str join "")
}
$output | append '
        _ => {
            provider_undefined $server
            if $error_exit { exit } else { return 0 }
        }
    }
} ' | str join ""
}
def make_mw_start_cache_info [
   providers_path: string
   providers_list: list<string>
]: nothing -> string {
    mut output = '
export def mw_start_cache_info [
    settings: record
    server: record
]: nothing -> nothing {
    match $server.provider { '
for prov in $providers_list {
    let method = $"($prov)_start_cache_info"
    if not (provider_lib_has_method $providers_path $prov $method) { continue }
    $output = ($output | append $'
        "($prov)" => {
            ($"\(($method) $settings $server)")
        },' | str join "")
}
$output | append '
        _ => {
            provider_undefined $server
        }
    }
} ' | str join ""
}
def make_mw_create_cache [
   providers_path: string
   providers_list: list<string>
]: nothing -> string {
    mut output = '
export def mw_create_cache [
    settings: record
    server: record
    error_exit: bool
]: nothing -> nothing {
    match $server.provider { '
for prov in $providers_list {
    let method = $"($prov)_create_cache"
    if not (provider_lib_has_method $providers_path $prov $method) { continue }
    $output = ($output | append $'
        "($prov)" => {
            ($"\(($method) $settings $server $error_exit)")
        },' | str join "")
}
$output | append '
        _ => {
            provider_undefined $server
            if $error_exit { exit } else { return 0 }
        }
    }
} ' | str join ""
}
def make_mw_read_cache [
   providers_path: string
   providers_list: list<string>
]: nothing -> string {
    mut output = '
export def mw_read_cache [
    settings: record
    server: record
    error_exit: bool
]: nothing -> nothing {
    match $server.provider { '
for prov in $providers_list {
    let method = $"($prov)_read_cache"
    if not (provider_lib_has_method $providers_path $prov $method) { continue }
    $output = ($output | append $'
        "($prov)" => {
            ($"\(($method) $settings $server $error_exit)")
        },' | str join "")
}
$output | append '
        _ => {
            provider_undefined $server
            if $error_exit { exit } else { return }
       }
    }
} ' | str join ""
}
def make_mw_clean_cache [
   providers_path: string
   providers_list: list<string>
]: nothing -> string {
    mut output = '
export def mw_clean_cache [
    settings: record
    server: record
    error_exit: bool
]: nothing -> nothing {
    match $server.provider { '
for prov in $providers_list {
    let method = $"($prov)_clean_cache"
    if not (provider_lib_has_method $providers_path $prov $method) { continue }
    $output = ($output | append $'
        "($prov)" => {
            ($"\(($method) $settings $server $error_exit)")
        },' | str join "")
}
$output | append '
        _ => {
            provider_undefined $server
            if $error_exit { exit } else { return }
        }
   }
} ' | str join ""
}
def make_mw_ip_from_cache [
   providers_path: string
   providers_list: list<string>
]: nothing -> string {
    mut output = '
export def mw_ip_from_cache [
    settings: record
    server: record
    error_exit: bool
]: nothing -> nothing {
    match $server.provider { '
for prov in $providers_list {
    let method = $"($prov)_ip_from_cache"
    if not (provider_lib_has_method $providers_path $prov $method) { continue }
    $output = ($output | append $'
        "($prov)" => {
            ($"\(($method) $settings $server $error_exit)")
        },' | str join "")
}
$output | append '
        "local" => {
            ($server | get -o network_public_ip | default "")
            #(local_ip_from_cache $settings $server $error_exit)
        }
        _ => {
            provider_undefined $server
            if $error_exit { exit } else { return }
        }
   }
} ' | str join ""
}
# - > Make middleware (middleware.nu env_middleware.nu) for existing providers 
export def make_middleware [
] {
    let provisioning_path = ($env.PROVISIONING? | default ("/" | path join "usr" |path join "local" | path join "provisioning"))
    let providers_path = ($provisioning_path | path join "providers")
    if not ($providers_path | path exists) { 
        print $"🛑 providers path (ansi red_bold)($providers_path)(ansi reset) not found"
        exit 1
    }
    let middleware_path = ($providers_path | path join "prov_lib" | path join "middleware.nu" )
    let env_middleware_path = ($providers_path | path join "prov_lib" | path join "env_middleware.nu" )
    let providers_list = (ls -s  $providers_path | where {|it| (
        ($it.name | str starts-with "_") == false 
            and ($providers_path | path join $it.name | path type) == "dir"
            and ($providers_path | path join $it.name | path join "templates" | path exists)
        )
    } | select name | values | flatten )
    let use_list = [ servers.nu, cache.nu, prices.nu, utils.nu ]
    mut output = $"# CNPROV middleware generated by 'make_middleware' on (date now | format date '%Y-%m-%d %H:%M:%S')"
    mut env_output = ($output | append "\nexport-env {" | str join "\n")
    for prov in $providers_list {
        let prov_root = ($providers_path | path join $prov | path join "nulib" | path join $prov)
        if not ($prov_root | path exists ) { continue }
        if ($prov_root | path join "env.nu" | path exists ) { $env_output = ($env_output | append $"   use ($prov)/env.nu" | str join "\n") }
        for $item in $use_list {
            if ($prov_root | path join $item | path exists ) { $output = ($output | append $"use ($prov)/($item) *" | str join "\n") }
        }
    }
    $env_output | append "}" | str join "" | save --force $env_middleware_path
    $output | append (make_provider_undefined $providers_path $providers_list) | str join "\n"
        | append (make_mw_query_servers $providers_path $providers_list) 
        | append (make_mw_servers_ips $providers_path $providers_list)
        | append (make_mw_server_info $providers_path $providers_list)
        | append (make_mw_servers_info $providers_path $providers_list)
        | append (make_mw_create_server $providers_path $providers_list)
        | append (make_mw_server_state $providers_path $providers_list)
        | append (make_mw_server_exists $providers_path $providers_list)
        | append (make_mw_server_is_running $providers_path $providers_list)
        | append (make_mw_get_ip $providers_path $providers_list)
        | append (make_mw_wait_storage $providers_path $providers_list)
        | append (make_mw_create_storage $providers_path $providers_list)
        | append (make_mw_post_create_server $providers_path $providers_list)
        | append (make_mw_modify_server $providers_path $providers_list)
        | append (make_mw_delete_server_storage $providers_path $providers_list)
        | append (make_mw_delete_server $providers_path $providers_list)
        | append (make_mw_load_infra_servers_info $providers_path $providers_list)
        | append (make_mw_load_infra_storages_info $providers_path $providers_list)
        | append (make_mw_get_infra_storage $providers_path $providers_list)
        | append (make_mw_get_infra_item $providers_path $providers_list)
        | append (make_mw_get_infra_price $providers_path $providers_list)
        | append (make_mw_start_cache_info $providers_path $providers_list)
        | append (make_mw_create_cache $providers_path $providers_list)
        | append (make_mw_read_cache $providers_path $providers_list)
        | append (make_mw_clean_cache $providers_path $providers_list)
        | str join ""
    | save --force $middleware_path 
}
