# Diagnostics Command Handlers
# Handles: status, health, next

use ../flags.nu *
# Import all from diagnostics modules
use ../../lib_provisioning/diagnostics/system_status.nu *
use ../../lib_provisioning/diagnostics/health_check.nu *
use ../../lib_provisioning/diagnostics/next_steps.nu *

# Main diagnostics command dispatcher
export def handle_diagnostics_command [
  command: string
  ops: string
  flags: record
] {
  match $command {
    "status" => { handle_status $ops $flags }
    "health" => { handle_health $ops $flags }
    "next" => { handle_next $flags }
    "phase" => { handle_phase $flags }
    "" | "diagnostics" => { handle_status $ops $flags }  # Default to status when no subcommand
    _ => {
      print $"❌ Unknown diagnostics command: ($command)"
      print ""
      print "Available diagnostics commands:"
      print "  status      - Show comprehensive system status"
      print "  health      - Run deep health validation"
      print "  next        - Get next steps recommendations"
      print "  phase       - Show current deployment phase"
      exit 1
    }
  }
}

# Status command handler
def handle_status [ops: string, flags: record] {
  let subcommand = ($ops | str trim)

  if $subcommand == "json" or ($flags.out? | default "" | str trim) == "json" {
    provisioning status-json | to json
  } else {
    provisioning status
  }
}

# Health command handler
def handle_health [ops: string, flags: record] {
  let subcommand = ($ops | str trim)

  if $subcommand == "json" or ($flags.out? | default "" | str trim) == "json" {
    provisioning health-json | to json
  } else {
    provisioning health
  }
}

# Next steps command handler
def handle_next [flags: record] {
  if ($flags.out? | default "" | str trim) == "json" {
    provisioning phase | to json
  } else {
    provisioning next
  }
}

# Phase command handler
def handle_phase [flags: record] {
  let phase_info = (provisioning phase)

  if ($flags.out? | default "" | str trim) == "json" {
    $phase_info | to json
  } else {
    print $"(ansi cyan_bold)Current Deployment Phase(ansi reset)\n"
    print $"Phase: (ansi yellow)($phase_info.info.phase)(ansi reset)"
    print $"Description: ($phase_info.info.description)"
    print $"Progress: ($phase_info.info.step)/($phase_info.info.total_steps) steps \(($phase_info.progress_percentage)%\)"
    print $"Ready for deployment: (if $phase_info.info.ready_for_deployment { '✅ Yes' } else { '❌ No' })"
  }
}
