# reflection/lib/fmt.nu — ANSI formatting primitives for the dispatcher UI.
# Shared by help, dashboard, and interactive systems.

export def fmt-header [text: string] {
  print $"(ansi cyan_bold)($text)(ansi reset)"
}

export def fmt-sep [] {
  print $"(ansi dark_gray)──────────────────────────────────────────────────────────────────(ansi reset)"
}

export def fmt-section [text: string] {
  print $"  (ansi white_bold)($text)(ansi reset)"
}

export def fmt-info [text: string] {
  print $"  (ansi default_dimmed)($text)(ansi reset)"
}

# Format a command line with the group verb highlighted.
# verb_pos: which word (0-indexed from after caller) to highlight.
export def fmt-cmd [cmd: string, desc: string = "", --verb-pos (-v): int = 0] {
  let parts = ($cmd | split row " ")
  let caller = ($env.ONTOREF_CALLER? | default "./onref")
  let caller_parts = ($caller | split row " " | length)
  let after = ($parts | skip $caller_parts)
  let colored = if ($after | is-empty) {
    $"(ansi green)($cmd)(ansi reset)"
  } else {
    mut segments = []
    for i in 0..(($after | length) - 1) {
      let word = ($after | get $i)
      if $i == $verb_pos {
        $segments = ($segments | append $"(ansi cyan_bold)($word)(ansi reset)")
      } else {
        $segments = ($segments | append $"(ansi green)($word)(ansi reset)")
      }
    }
    let prefix = ($parts | first $caller_parts | str join " ")
    $"(ansi green)($prefix)(ansi reset) ($segments | str join ' ')"
  }
  if ($desc | is-empty) {
    print $"  ($colored)"
  } else {
    print $"  ($colored)  (ansi dark_gray)($desc)(ansi reset)"
  }
}

export def fmt-badge [text: string]: nothing -> string {
  $"(ansi magenta_bold)[($text)](ansi reset)"
}

# Resolve --fmt: if "select" or "?", show interactive picker; otherwise pass through.
def expand-fmt-alias [fmt: string]: nothing -> string {
  match $fmt {
    "txt" | "tx" => "text",
    "tab" | "ta" => "table",
    "j" | "jsn"  => "json",
    "y" | "yml"  => "yaml",
    "t" | "tml"  => "toml",
    "m"          => "md",
    _            => $fmt,
  }
}

export def resolve-fmt [fmt: string, choices: list<string>]: nothing -> string {
  if $fmt == "select" or $fmt == "?" {
    let picked = ($choices | input list $"(ansi cyan_bold)Format:(ansi reset) ")
    if ($picked | is-empty) { $choices | first } else { $picked }
  } else if ($fmt | is-empty) {
    $choices | first
  } else {
    expand-fmt-alias $fmt
  }
}

export def fmt-aliases [aliases: list<record>] {
  fmt-section "ALIASES"
  print ""
  let max_short = ($aliases | each { |a| $a.short | str length } | math max)
  for a in $aliases {
    let pad = ($max_short - ($a.short | str length))
    let spaces = ("" | fill -c " " -w $pad)
    print $"    (ansi cyan)($a.short)(ansi reset)($spaces)  →  ($a.long)"
  }
  print ""
}

export def render-health-bar [health: float] {
  let color = if $health >= 90.0 { (ansi green_bold) } else if $health >= 70.0 { (ansi yellow_bold) } else { (ansi red_bold) }
  let bar_len = (($health / 5.0) | math round | into int)
  let bar_fill = ("█" | fill -c "█" -w $bar_len)
  let bar_empty = ("░" | fill -c "░" -w (20 - $bar_len))
  print ""
  print $"  ($color)($bar_fill)(ansi dark_gray)($bar_empty)(ansi reset)  ($color)($health)%(ansi reset)"
  print ""
}
