#!/usr/bin/env bash

function cmd::inspect::on_load() {
  flag::register --name
  flag::register --type
}

function cmd::inspect::help() {
  cat <<EOF
Usage: wgctl inspect --name <name> [--type <type>]
       wgctl inspect <full-name>

Show detailed information for a single client.

Options:
  --name <name>     Client name
  --type <type>     Device type (optional, combines with --name)

Examples:
  wgctl inspect --name phone-nuno
  wgctl inspect --name nuno --type phone
EOF
}

function cmd::inspect::run() {
  local name=""
  local type=""

  # Support positional argument: wgctl inspect phone-nuno
  if [[ $# -gt 0 && "$1" != "--"* ]]; then
    name="$1"
    shift
  fi

  while [[ $# -gt 0 ]]; do
    case "$1" in
      --name)  name="$2";             shift 2 ;;
      --type)  type="$2";             shift 2 ;;
      --help)  cmd::inspect::help;    return  ;;
      *)
        log::error "Unknown flag: $1"
        cmd::inspect::help
        return 1
        ;;
    esac
  done

  if [[ -z "$name" ]]; then
    log::error "Missing required flag: --name"
    cmd::inspect::help
    return 1
  fi

  name=$(peers::resolve_and_require "$name" "$type") || return 1

  load_command list
  cmd::list::run --name "$name"
}