#!/usr/bin/env nu
# alias.nu — Command alias reference
#
# prvng alias / prvng a / prvng al — show the full shortcut table.
# Reads the JSON cache (~/.cache/provisioning/commands-registry.json) — no nickel export.

# Load commands from the JSON cache written by _validate_command in the bash wrapper.
# Cache is at ~/.cache/provisioning/commands-registry.json, rebuilt on registry mtime change.
# Falls back to static table if cache is absent.
def _load-registry []: nothing -> list<record> {
    let cache = ($env.HOME | path join ".cache" | path join "provisioning" | path join "commands-registry.json")
    if not ($cache | path exists) { return [] }
    let result = (do { open --raw $cache | from json } | complete)
    if $result.exit_code != 0 { return [] }
    $result.stdout | get -o commands | default []
}

# Print section header + rows for one category.
def _print-section [title: string, rows: list<record>]: nothing -> nothing {
    if ($rows | is-empty) { return }
    print $title
    for r in $rows {
        let al  = ($r.aliases | str join "  " | fill -w 14 -a l)
        let cmd = $r.command
        print $"  ($al) →  ($cmd)"
    }
    print ""
}

# Main alias list — reads registry and renders grouped alias table.
export def alias-list []: nothing -> nothing {
    let cmds = (_load-registry)

    if ($cmds | is-empty) {
        _alias-list-static
        return
    }

    let rows = ($cmds
        | where {|c| ($c | get -o aliases | default []) | is-not-empty }
        | each {|c| {
            command:  $c.command
            aliases:  ($c | get -o aliases | default [])
            category: ($c | get -o help_category | default "other")
        }}
        | sort-by command
    )

    print ""
    print "ALIASES"
    print "════════════════════════════════════════════════════"

    _print-section "" ($rows | where category == "infrastructure")
    _print-section "ORCHESTRATION" ($rows | where category == "orchestration")

    let rest = ($rows | where {|r| $r.category not-in ["infrastructure", "orchestration"] })
    if ($rest | is-not-empty) {
        _print-section "OTHER" $rest
    }

    print "════════════════════════════════════════════════════"
    print "Tip: prvng <alias> help  →  subcommand details"
    print ""
}

# Static fallback when registry unavailable at runtime.
def _alias-list-static []: nothing -> nothing {
    print ""
    print "ALIASES"
    print "════════════════════════════════════════════════════"
    print ""
    print "INFRASTRUCTURE"
    print "  s               →  server"
    print "  t  task         →  taskserv"
    print "  c  e  comp  ext →  component"
    print ""
    print "ORCHESTRATION"
    print "  w  wflow        →  workflow"
    print "  j               →  job"
    print "  b  bat          →  batch"
    print "  o  orch         →  orchestrator"
    print ""
    print "OTHER"
    print "  a  al           →  alias"
    print "  ws              →  workspace"
    print "  h               →  help"
    print "  p  plat         →  platform"
    print "  bd              →  build"
    print "  val             →  validate"
    print ""
    print "════════════════════════════════════════════════════"
    print "Tip: prvng <alias> help  →  subcommand details"
    print ""
}
