# Generation Command Handlers
# Handles: generate commands (server, taskserv, cluster, infra)

use ../flags.nu *
# REMOVED: use ../../lib_provisioning * - causes circular import

# Helper to run module commands
def run_module [
  args: string
  module: string
  option?: string
  --exec
] {
  let use_debug = if ($env.PROVISIONING_DEBUG? | default false) { "-x" } else { "" }

  if $exec {
    exec $"($env.PROVISIONING_NAME)" $use_debug -mod $module ($option | default "") $args
  } else {
    ^$"($env.PROVISIONING_NAME)" $use_debug -mod $module ($option | default "") $args
  }
}

# Main generation command dispatcher
export def handle_generation_command [
  command: string
  ops: string
  flags: record
] {
  let use_debug = if $flags.debug_mode { "-x" } else { "" }
  let use_check = if $flags.check_mode { "--check " } else { "" }
  let str_infra = if ($flags.infra | is-not-empty) { $"--infra ($flags.infra) " } else { "" }
  let str_out = if ($flags.outfile | is-not-empty) { $"--outfile ($flags.outfile) " } else { "" }
  let str_template = if ($flags.template | is-not-empty) { $"--template ($flags.template)" } else { "" }
  let str_select = if ($flags.select | is-not-empty) { $"--select ($flags.select)" } else { "" }

  # Handle Infrastructure-from-Code commands (detect, complete, workflow)
  match $command {
    "detect" => {
      # Technology detection command - directly invoke module
      let detect_module = ($env.PROVISIONING | path join "core" "nulib" "provisioning detect")
      if ($detect_module | path exists) {
        let path = if ($ops | is-not-empty) { ($ops | split row " " | first) } else { "." }
        let out_fmt = if ($flags.output_format | is-not-empty) { $"--out ($flags.output_format)" } else { "" }
        let pretty = if $flags.pretty_print { "--pretty" } else { "" }
        exec nu $detect_module $path $out_fmt $pretty
      } else {
        print -e $"❌ Detection module not found at: ($detect_module)"
        exit 1
      }
      return
    }
    "complete" => {
      # Infrastructure completion command - directly invoke module
      let complete_module = ($env.PROVISIONING | path join "core" "nulib" "provisioning complete")
      if ($complete_module | path exists) {
        let path = if ($ops | is-not-empty) { ($ops | split row " " | first) } else { "." }
        let out_fmt = if ($flags.output_format | is-not-empty) { $"--out ($flags.output_format)" } else { "" }
        let check = if $flags.check_mode { "--check" } else { "" }
        let pretty = if $flags.pretty_print { "--pretty" } else { "" }
        exec nu $complete_module $path $out_fmt $check $pretty
      } else {
        print -e $"❌ Completion module not found at: ($complete_module)"
        exit 1
      }
      return
    }
    "workflow" => {
      # Full Infrastructure-from-Code workflow - directly invoke module
      let workflow_module = ($env.PROVISIONING | path join "core" "nulib" "provisioning workflow")
      if ($workflow_module | path exists) {
        let path = if ($ops | is-not-empty) { ($ops | split row " " | first) } else { "." }
        let org = if ($flags.org | is-not-empty) { $"--org ($flags.org)" } else { "" }
        let out_fmt = if ($flags.output_format | is-not-empty) { $"--out ($flags.output_format)" } else { "" }
        let apply = if $flags.apply_changes { "--apply" } else { "" }
        let verbose = if $flags.verbose_output { "--verbose" } else { "" }
        let pretty = if $flags.pretty_print { "--pretty" } else { "" }
        exec nu $workflow_module $path $org $out_fmt $apply $verbose $pretty
      } else {
        print -e $"❌ Workflow module not found at: ($workflow_module)"
        exit 1
      }
      return
    }
    "orchestrate" => {
      # IaC → Orchestrator integration - directly invoke module
      let orch_module = ($env.PROVISIONING | path join "core" "nulib" "provisioning orchestrate")
      if ($orch_module | path exists) {
        let path = if ($ops | is-not-empty) { ($ops | split row " " | first) } else { "." }
        let org = if ($flags.org | is-not-empty) { $"--org ($flags.org)" } else { "" }
        let infra = if ($flags.infra | is-not-empty) { $"--infra ($flags.infra)" } else { "" }
        let verbose = if $flags.verbose_output { "--verbose" } else { "" }
        let dry_run = if $flags.dry_run { "--dry-run" } else { "" }
        exec nu $orch_module $path $org $infra $verbose $dry_run
      } else {
        print -e $"❌ Orchestration module not found at: ($orch_module)"
        exit 1
      }
      return
    }
    _ => { }  # Fall through to regular generate command handling
  }

  # If no subcommand, show general generate help
  if ($ops | is-empty) {
    exec $"($env.PROVISIONING_NAME)" $use_debug "generate" $ops $use_check $str_infra $str_out
    return
  }

  let target = ($ops | split row " " | first | default null)
  let gen_ops = ($ops | split row " " | skip 1 | str join " ") + $" ($str_infra) ($str_template) ($str_out) ($use_check) ($use_debug) ($str_select)"

  match $target {
    "s" | "server" => {
      run_module $"- ($gen_ops)" "server" "generate" --exec
    }
    "t" | "task" | "taskserv" => {
      run_module $"- ($gen_ops)" "taskserv" "generate" --exec
    }
    "i" | "infra" | "infras" => {
      run_module $"- ($gen_ops)" "infra" "generate" --exec
    }
    "cl" | "cluster" => {
      run_module $"- ($gen_ops)" "cluster" "generate" --exec
    }
    "h" | "help" => {
      _print $"\n(provisioning_generate_options)"
      exit
    }
    "new" => {
      exec $"($env.PROVISIONING_NAME)" $use_debug "generate" "new" $gen_ops $str_template $use_check $str_infra $str_out
    }
    _ => {
      print $"❌ Unknown generate target: ($target)"
      print ""
      print "Available generate targets:"
      print "  server      - Generate server configurations"
      print "  taskserv    - Generate taskserv definitions"
      print "  cluster     - Generate cluster configurations"
      print "  infra       - Generate infrastructure setup"
      print "  new         - Generate new infrastructure from scratch"
      print ""
      print "Use 'provisioning generate help' for more details"
      exit 1
    }
  }
}
