#!/usr/bin/env nu
# Prepare script for fip taskserv.
# Generates env-fip directly from the vars file so the install script
# has FLOATING_IP and IFACE set regardless of template rendering order.

let vars_path = ($env.PROVISIONING_VARS? | default "")
if ($vars_path | is-empty) or not ($vars_path | path exists) {
    print $"🛑 PROVISIONING_VARS not set or not found: ($vars_path)"
    exit 1
}

let wk_data = (open $vars_path)
mut floating_ip = ($wk_data | get -o server.floating_ip_address | default "")
let iface       = ($wk_data | get -o taskserv.interface | default "eth0")

# If floating_ip_address is not set in NCL (runtime data), resolve it from state files.
if ($floating_ip | is-empty) {
    let fip_name   = ($wk_data | get -o server.floating_ip | default "")
    let hostname   = ($wk_data | get -o server.hostname    | default "")
    let ws_root    = do {
        let explicit = ($env.PROVISIONING_WORKSPACE_PATH? | default "")
        if ($explicit | is-not-empty) { $explicit } else {
            let wss = ($env.PROVISIONING_WORKSPACES? | default "")
            if ($wss | is-not-empty) { $wss | path dirname } else { "" }
        }
    }

    # Try 1: .servers-state.json (written by server sync/create)
    if ($floating_ip | is-empty) and ($ws_root | is-not-empty) and ($hostname | is-not-empty) {
        let infra_dir = ($ws_root | path join "infra")
        let state_candidates = if ($infra_dir | path exists) {
            do -i { ls $infra_dir }
            | where type == "dir"
            | get name
            | each {|d| $d | path join ".servers-state.json" }
            | where {|p| $p | path exists }
        } else { [] }
        for sc in $state_candidates {
            let srv_state = (open $sc | get -o $hostname | default null)
            if $srv_state != null {
                let fip_addr = ($srv_state | get -o floating_ip_address | default "")
                if ($fip_addr | is-not-empty) {
                    $floating_ip = $fip_addr
                    print $"✓ floating_ip_address resolved from servers-state: ($fip_addr)"
                    break
                }
            }
        }
    }

    # Try 2: .provisioning-state.json bootstrap FIPs (keyed by fip name without prefix)
    if ($floating_ip | is-empty) and ($fip_name | is-not-empty) and ($ws_root | is-not-empty) {
        let bs_path = ($ws_root | path join ".provisioning-state.json")
        if ($bs_path | path exists) {
            let fip_key = ($fip_name | str replace --all "librecloud-fip-" "" | str replace --all "-" "_")
            let bs_data = (open $bs_path)
            let fip_rec = do -i { $bs_data.bootstrap.floating_ips | get $fip_key } | default null
            if $fip_rec != null {
                $floating_ip = ($fip_rec | get -o ip | default "")
                if ($floating_ip | is-not-empty) {
                    print $"✓ floating_ip_address resolved from bootstrap state: ($floating_ip)"
                }
            }
        }
    }

    if ($floating_ip | is-empty) {
        print $"🛑 Cannot resolve floating_ip_address for ($hostname) — run: provisioning server sync"
        exit 1
    }
}

# PROVISIONING_WK_ENV_PATH is the local taskserv working directory (set by make_cmd_env_temp)
let out_dir = ($env.PROVISIONING_WK_ENV_PATH? | default ".")
$"IFACE=($iface)\nFLOATING_IP=($floating_ip)\n" | save -f ($out_dir | path join "env-fip")
print $"✓ env-fip written: IFACE=($iface) FLOATING_IP=($floating_ip)"
