# bash completion for atglance
_atglance() {
  local cur prev
  cur=${COMP_WORDS[COMP_CWORD]}
  prev=${COMP_WORDS[COMP_CWORD-1]}
  if [ "$prev" = "-o" ]; then
    COMPREPLY=($(compgen -W "table json" -- "$cur"))
    return
  fi
  # A leading -v/--verbose is not the subcommand; skip it.
  local first=1
  case "${COMP_WORDS[1]}" in -v|--verbose) first=2 ;; esac
  if [ "$COMP_CWORD" -le "$first" ]; then
    COMPREPLY=($(compgen -W "status service export watch version help login logout whoami check doctor schedule system config sync security" -- "$cur"))
    return
  fi
  case "${COMP_WORDS[$first]}" in
    status) COMPREPLY=($(compgen -W "--all -o" -- "$cur")) ;;
    service)
      if [ "$COMP_CWORD" -eq $((first + 1)) ]; then
        COMPREPLY=($(compgen -W "$(systemctl list-units --type=service --all --plain --no-legend --no-pager 2>/dev/null | awk '{sub(/\.service$/, "", $1); print $1}')" -- "$cur"))
      else
        COMPREPLY=($(compgen -W "-o" -- "$cur"))
      fi ;;
    export) COMPREPLY=($(compgen -W "--logs" -- "$cur")) ;;
    login) COMPREPLY=($(compgen -W "--server" -- "$cur")) ;;
    system)
      if [ "$COMP_CWORD" -eq $((first + 1)) ]; then
        COMPREPLY=($(compgen -W "register status deregister reactivate rotate-key service-account" -- "$cur"))
      else
        case "${COMP_WORDS[$((first + 1))]}" in
          register) COMPREPLY=($(compgen -W "--name --tags --workspace --ip --force-new --service-account" -- "$cur")) ;;
          deregister|reactivate) COMPREPLY=($(compgen -W "--pin --force --id" -- "$cur")) ;;
          rotate-key) COMPREPLY=($(compgen -W "--pin" -- "$cur")) ;;
          service-account) [ "$COMP_CWORD" -eq $((first + 2)) ] && COMPREPLY=($(compgen -W "set remove" -- "$cur")) ;;
        esac
      fi ;;
    config)
      if [ "$COMP_CWORD" -eq $((first + 1)) ]; then
        COMPREPLY=($(compgen -W "backup list pull diff restore" -- "$cur"))
      else
        case "${COMP_WORDS[$((first + 1))]}" in
          backup)
            COMPREPLY=($(compgen -W "--all --local --force $(systemctl list-units --type=service --all --plain --no-legend --no-pager 2>/dev/null | awk '{sub(/\.service$/, "", $1); print $1}')" -- "$cur")) ;;
          list)
            if [ "$COMP_CWORD" -eq $((first + 2)) ]; then
              COMPREPLY=($(compgen -W "$(systemctl list-units --type=service --all --plain --no-legend --no-pager 2>/dev/null | awk '{sub(/\.service$/, "", $1); print $1}')" -- "$cur"))
            fi ;;
          restore)
            if [ "$COMP_CWORD" -eq $((first + 2)) ]; then
              COMPREPLY=($(compgen -W "--yes --allow-ssh-restart $(systemctl list-units --type=service --all --plain --no-legend --no-pager 2>/dev/null | awk '{sub(/\.service$/, "", $1); print $1}')" -- "$cur"))
            else
              COMPREPLY=($(compgen -W "--yes --allow-ssh-restart" -- "$cur"))
            fi ;;
        esac
      fi ;;
    security)
      if [ "$COMP_CWORD" -eq $((first + 1)) ]; then
        COMPREPLY=($(compgen -W "events lockouts" -- "$cur"))
      else
        case "${COMP_WORDS[$((first + 1))]}" in
          events) COMPREPLY=($(compgen -W "--type --since --until --user --all -o" -- "$cur")) ;;
          lockouts) COMPREPLY=($(compgen -W "-o" -- "$cur")) ;;
        esac
      fi ;;
    sync) COMPREPLY=($(compgen -W "--scheduled" -- "$cur")) ;;
    check|doctor) COMPREPLY=($(compgen -W "--scheduled" -- "$cur")) ;;
    schedule) [ "$COMP_CWORD" -eq $((first + 1)) ] && COMPREPLY=($(compgen -W "enable disable status" -- "$cur")) ;;
    watch) COMPREPLY=($(compgen -W "--interval --all" -- "$cur")) ;;
  esac
}
complete -F _atglance atglance
