#!/usr/bin/env nu
# Info: Script to run Provisioning
# Author: JesusPerezLorenzo
# Release: 1.0.4
# Date: 6-2-2024

#use std  # assert
use std log

use lib_provisioning *

use env.nu *

use taskservs *

# - > TaskServs Help
export def "main help" [
  --src: string = ""
  --notitles       # not tittles
] {
  if $notitles == null or not $notitles { show_titles }
  ^($env.PROVISIONING_NAME) "-mod" "taskserv" "--help"
  _print (provisioning_options $src)
  if not $env.PROVISIONING_DEBUG { end_run "" }
}

# >  Task and Services for servers
def main [
  ...args: string  # Other options, use help to get info
  --iptype: string = "public" # Ip type to connect
  -v               # Show version
  --version (-V)   # Show version with title
  --info (-I)      # Show Info  with title
  --about (-a)     # Show About
  --infra (-i): string  # Infra directory
  --settings (-s): string # Settings path
  --serverpos (-p): int   # Server position in settings
  --check (-c)     # Only check mode no servers will be created
  --yes (-y)       # Confirm task
  --wait (-w)      # Wait servers to be created
  --select: string # Select with taskservice  as option
  --onsel: string  # On selection: e (edit) | v (view) | l (list)
  --debug (-x)     # Use Debug mode
  --xm             # Debug with PROVISIONING_METADATA
  --xc             # Debuc for taskservice and services locally PROVISIONING_DEBUG_CHECK
  --xr             # Debug for remote servers PROVISIONING_DEBUG_REMOTE
  --xld            # Log level with DEBUG PROVISIONING_LOG_LEVEL=debug
  --nc             # Not clean working settings
  --metadata       # Error with metadata (-xm)
  --notitles       # Do not show banner titles
  --helpinfo (-h)  # For more details use options "help" (no dashes)
  --out: string    #  Print Output format: json, yaml, text (default)
]: nothing -> nothing {
  if ($out | is-not-empty) {
    $env.PROVISIONING_OUT = $out
    $env.PROVISIONING_NO_TERMINAL = true
  }
  provisioning_init $helpinfo "taskserv" $args
  if $version or $v { ^$env.PROVISIONING_NAME -v ; exit }
  if $info { ^$env.PROVISIONING_NAME -i ; exit }
  if $about {
    #use defs/about.nu [ about_info ]
    _print (get_about_info)
    exit
  }
  if $debug { $env.PROVISIONING_DEBUG = true }
  let use_debug = if $debug or $env.PROVISIONING_DEBUG { "-x" } else { "" }
  if $metadata { $env.PROVISIONING_METADATA = true }
  # for $arg in $args { print $arg  }
  let task = if ($args | length) > 0 { ($args| get 0) } else { "" }
  let ops = if ($args | length) > 1 { ($args | skip 1 | str join " ") } else { "" }
  print $"---TASK ($task)"
  exit 1
  exec ($env.PROVISIONING_NAME) $use_debug "-mod" "taskserv" "create" ...($ops | split row " ") --notitles
   if not $env.PROVISIONING_DEBUG { end_run "" }
}

# export def use_match [task: string ops: string infra: string settings: record ] {
#     match $task {
#       "h" | "help" => {
#         # Redirect to main categorized help system
#         exec ($env.PROVISIONING_NAME) "help" "infrastructure" "--notitles"
#       },
#       "sed" => {
#         if $ops == "" {
#           (throw-error $"🛑 No file found" $"for (_ansi yellow_bold)sops(_ansi reset) edit")
#           exit 1
#         } else if ($ops | path exists) == false {
#           (throw-error $"🛑 No file (_ansi green_italic)($ops)(_ansi reset) found" $"for (_ansi yellow_bold)sops(_ansi reset) edit")
#           exit 1
#         }
#         if $env.PROVISIONING_SOPS? == null  {
#           let curr_settings = (find_get_settings --infra $infra --settings $settings)
#           $env.CURRENT_INFRA_PATH = $"($curr_settings.infra_path)/($curr_settings.infra)"
#           use sops_env.nu
#         }
#         #use sops on_sops
#         on_sops "sed" $ops
#       },
#       "c" | "create" => {
#         exec ($env.PROVISIONING_NAME) $use_debug "-mod" "taskserv" "create" ...($ops | split row " ") --notitles
#       }
#       "d" | "delete" => {
#         exec ($env.PROVISIONING_NAME) $use_debug "-mod" "taskserv" "delete" ...($ops | split row " ") --notitles
#       }
#       "g" | "generate" => {
#         exec ($env.PROVISIONING_NAME) $use_debug "-mod" "taskserv" "generate" ...($ops | split row " ") --notitles
#       }
#       "l"| "list" => {
#         #use defs/lists.nu on_list
#         on_list "taskservs" ($onsel | default "") ""
#       },
#       "qr" => {
#         #use utils/qr.nu *
#         make_qr
#       },
#       _ => {
#         invalid_task "taskserv" $task --end
#       },
#     }

# }
