# Development Command Handlers
# Handles: module, layer, version, pack commands

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 --notitles
  } else {
    ^$"($env.PROVISIONING_NAME)" $use_debug -mod $module ($option | default "") $args --notitles
  }
}

# Main development command dispatcher
export def handle_development_command [
  command: string
  ops: string
  flags: record
] {
  set_debug_env $flags

  match $command {
    "module" => { handle_module $ops $flags }
    "layer" => { handle_layer $ops $flags }
    "version" => { handle_version $ops $flags }
    "pack" => { handle_pack $ops $flags }
    _ => {
      print $"❌ Unknown development command: ($command)"
      print ""
      print "Available development commands:"
      print "  module   - Module discovery and management"
      print "  layer    - Layer system (explain, show, test, stats)"
      print "  version  - Version management (check, show, updates)"
      print "  pack     - Packaging (core, provider, list, clean)"
      print ""
      print "Use 'provisioning help development' for more details"
      exit 1
    }
  }
}

# Module command handler
def handle_module [ops: string, flags: record] {
  let args = build_module_args $flags $ops
  run_module $args "module" --exec
}

# Layer command handler
def handle_layer [ops: string, flags: record] {
  let args = build_module_args $flags $ops
  run_module $args "layer" --exec
}

# Version command handler
def handle_version [ops: string, flags: record] {
  let args = build_module_args $flags $ops
  run_module $args "version" --exec
}

# Pack command handler
def handle_pack [ops: string, flags: record] {
  let args = build_module_args $flags $ops
  run_module $args "pack" --exec
}
