#!/usr/bin/env -S nu
##!/usr/bin/env -S nu --stdin

# Info: Script to run Provisioning
# Author: JesusPerezLorenzo 
# Release: 1.0.4
# Date: 6-2-2024

use env.nu *

export def _ansi [
  arg: string
]: nothing -> nothing {
  ansi $arg
}

export def github_latest_tag [
  url: string = ""
  use_dev_release: bool = false
  id_target: string = "releases/tag" 
] nothing:string {
  #let res = (http get $url -r )
  if ($url | is-empty) { return "" }
  let res = (^curl -s $url | complete)
  let html_content = if ($res.exit_code != 0) {
      print $"🛑 Error (_ansi red)($url)(_ansi reset):\n ($res.exit_code) ($res.stderr)"
      return ""
  } else { $res.stdout }
  # curl -s https://github.com/project-zot/zot/tags | grep "<h2 " | grep "releases/tag" 

  let versions = ($html_content | parse --regex '<h2 (?<a>.*?)</a>' | get -o a | each {|it|
    ($it | parse --regex ($"($id_target)" + '/(?<version>.*?)"') | get version | get -o 0 | default "") 
  })
  let list = if $use_dev_release { 
     $versions
  } else {  
     ($versions | filter {|it| 
        not ($it | str contains "-rc") and not ($it | str contains "-alpha") 
     })
  }
  $list | sort -r  | get -o 0 | default ""
}

export def value_input_list [
  input_type: string
  options_list: list
  msg: string
  default_value: string
] nothing:string {
  let selection_pos = ( $options_list
  | input list --index ( 
    $"(_ansi default_dimmed)Select(_ansi reset) (_ansi yellow_bold)($msg)(_ansi reset) " +
    $"\n(_ansi default_dimmed)\(use arrow keys and press [enter] or [escape] for default '(_ansi reset)" +
    $"($default_value)(_ansi default_dimmed)'\)(_ansi reset)"
  ))
  if $selection_pos != null { 
    ($options_list | get -o $selection_pos | default $default_value)
  } else { $default_value }
}

export def value_input [
  input_type: string
  numchar: int 
  msg: string
  default_value: string
  not_empty: bool
] nothing:string {
  while true {
    let value_input = if $numchar > 0 {
      print ($"(_ansi yellow_bold)($msg)(_ansi reset) " + 
        $"(_ansi default_dimmed) type value (_ansi green_bold)($numchar) chars(_ansi reset) " + 
        $"(_ansi default_dimmed) default '(_ansi reset)" +
        $"($default_value)(_ansi default_dimmed)'(_ansi reset)"
      )
      (input --numchar $numchar)
    } else { 
      print ($"(_ansi yellow_bold)($msg)(_ansi reset) " +
        $"(_ansi default_dimmed)\(type value and press [enter] default '(_ansi reset)" +
        $"($default_value)(_ansi default_dimmed)'\)(_ansi reset)"
      )
      (input)
    }
    if $not_empty and ($value_input | is-empty) { 
      if ($default_value | is-not-empty) { return $default_value }
      continue
    }
    let result = match $input_type {
      "number" => $value_input,
      _ => $value_input,
    }
    if $value_input != $result { continue }
    return $value_input
  }
  # if $value_input != "yes" and $user_input != "YES"  {
  #}
}

export def get_input_data [
#print (get_latest_tag "https://github.com/project-zot/zot/tags" false)
#print (get_latest_tag "https://github.com/kubernetes/kubernetes/tags" false)
...args: string 
--use-rc
] string:string {
  # let in_url = ($in | default "")
  # let url = if ($in_url | is-empty) { 
  #   ($args | get -o 0 | default "")
  # } else {
  #    $in_url
  # }
  mut data = {}
  for it in ($env | get -o defs_values | default []) {
    let value = match $it.input_type {
      "list" => (value_input_list $it.input_type $it.options_list $it.msg $it.default_value),
      _ => (value_input $it.input_type $it.numchar $it.msg $it.default_value $it.not_empty),
    }
    $data = ($data | merge { $it.var: $value })
  }
  $data = ($data | merge { version: 
    (github_latest_tag ($env | get -o tags_url | default "")  ($env | get -o use_dev_release | default false))
  })
  print $data
}
