#!/usr/bin/env nu
# Info: UpCloud

use std

export def upcloud_start_cache_info [
  settings: record
  server: record
] {
   $""
}
export def upcloud_create_cache [
  settings: record
  server: record
  error_exit: bool
] {
   if $settings == null {
      print $"❗ No settings found "
      return
   }
   let provider_path = (get_provider_data_path $settings $server)
   #use lib_provisioning/utils/settings.nu load_provider_env
   let data = (load_provider_env $settings $server $provider_path)
   if ($data | is-not-empty) or ($data | get -o main) != "?"  {
      if $env.PROVISIONING_DEBUG {
         print $"UpCloud main data already exists in ($provider_path | path basename)"
      }
   }
   let result = (^upctl "server" "show" $server.hostname -o "json" err> (if $nu.os-info.name == "windows" { "NUL" } else { "/dev/null" })| complete)
   let info_server = if $result.exit_code == 0 {
      ($result.stdout | from json)
   } else { {} }
   let all_servers = if ($data.servers? == null) {
      {}
   }  else if ($info_server | is-empty) {
      $data.servers
   } else {
      $data.servers | default {} | append  $info_server
   }
   let new_data = ( $data | merge { servers: $all_servers})
   save_provider_env $new_data $settings $provider_path
   if $env.PROVISIONING_DEBUG {  print $"Cache for ($server.provider) on ($server.hostname) saved in: ($provider_path | path basename)" }
}
export def upcloud_read_cache [
  settings: record
  server: record
  error_exit: bool
] {
   if $settings == null {
      print $"❗ No settings found "
      return
   }
}
export def upcloud_clean_cache [
  settings: record
  server: record
  error_exit: bool
] {
   if $settings == null {
      print $"❗ No settings found "
      return
   }
   let provider_path = (get_provider_data_path $settings $server)
   let data = (load_provider_env $settings $server $provider_path)
   if ($data.servers? == null) {  return {} }
   if ($data.servers | where {|it| ($it.hostname? | default "") == $server.hostname} | length) == 0 {
      if $env.PROVISIONING_DEBUG {
         print $"❗server ($server.hostname) already deleted from ($provider_path | path basename)"
      }
      return
   }
   let all_servers = ( $data.servers? | default []  | where {|it| ($it.hostname? | is-not-empty) and ($it.hostname? | default "") != $server.hostname})
   if $env.PROVISIONING_DEBUG {  print $"Cache for ($server.provider) delete ($server.hostname) in: ($provider_path | path basename)" }
   let new_data = if ($all_servers | length) == 0 {
      ( $data | merge { servers: []})
   } else {
      ( $data | merge { servers: $all_servers})
   }
   save_provider_env $new_data $settings $provider_path
}
export def upcloud_ip_from_cache [
  settings: record
  server: record
  error_exit: bool
] {
   let data = ($settings.providers | find $server.provider |  get -o settings | get -o servers | flatten
     | find $server.hostname | select -o ip_addresses)
   mut pub_ip = ""
   for it in $data {
     $pub_ip = ($it | get -o ip_addresses | find "public" | get -o address | get -o 0)
   }
   $pub_ip
}
