#compdef ctrld
#autoload

typeset -A opt_args

local -a _1st_arguments
_1st_arguments=(
    'run:Run the DNS proxy server:_ctrld_run'
    'service:Manage ctrld service:_ctrld_service'
    'start:Quick start service and configure DNS on interface:_ctrld_start'
    'stop:Quick stop service and remove DNS from interface:_ctrld_stop'
    'restart:Restart the ctrld service:_ctrld_restart'
    'reload:Reload the ctrld service:_ctrld_reload'
    'status:Show status of the ctrld service:_ctrld_status'
    'uninstall:Stop and uninstall the ctrld service:_ctrld_uninstall'
    'clients:Manage clients:_ctrld_clients'
)

local context state line curcontext="$curcontext"

_arguments -C \
  '(-h)-h[help]' \
  '(--help)--help[help]' \
  '(-v)-v[verbose | basic logging]' \
  '(-vv)-vv[verbose | debug level logging]' \
  '1:: :->cmds' \
  '2:: :->subcmds' \
  '3:: :->subsubcmds' \
&& return 0

local rules

case "$state" in
  (cmds)
    _describe -t commands "ctrld commands" _1st_arguments
    return 0
    ;;
  (subcmds)
    case "$line[1]" in
      (run)
          _values 'run' \
                    '--base64_config[string | Base64 encoded config]' \
                    '--cache_size[int | Enable cache with size items]' \
                    '--cd[string | Control D resolver uid]' \
                    '--cd-org[string | Control D provision token]' \
                    '-c[string | Path to config file]' \
                    '--config[string | Path to config file]' \
                    '-d[Run as daemon]' \
                    '--daemon[Run as daemon]' \
                    '--domains[strings | List of domain to apply in a split DNS policy]' \
                    '-h[help for run]' \
                    '--listen[string | Listener address and port, in format: address:port]' \
                    '--log[string | Path to log file]' \
                    '--primary_upstream[string | Primary upstream endpoint]' \
                    '--proto[string | Control D upstream type, either "doh" or "doh3" (default "doh")]' \
                    '--secondary_upstream[string | Secondary upstream endpoint]' \
                && ret=0
                ;;
      (service)
        _values 'service' \
            'start[Install and start the ctrld service]' \
                    'stop[Stop the ctrld service]' \
                    'restart[Restart the ctrld service]' \
                    'reload[Reload the ctrld service]' \
                    'status[Show status of the ctrld service]' \
                    'uninstall[Stop and uninstall the ctrld service]' \
                    'interfaces[Manage network interfaces]' \
        && ret=0
        ;;
        (start)
            _values 'start' \
                '--base64_config[string | Base64 encoded config]' \
                '--cache_size[int | Enable cache with size items]' \
                '--cd[string | Control D resolver uid]' \
                '--cd-org[string | Control D provision token]' \
                '-c[string | Path to config file]' \
                '--config[string | Path to config file]' \
                '--domains[strings | List of domain to apply in a split DNS policy]' \
                '-h[help for start]' \
                '--iface[string | Update DNS setting for iface, "auto" means the default interface gateway >]' \
                '--listen[string | Listener address and port, in format: address:port]' \
                '--log[string | Path to log file]' \
                '--nextdns[string | NextDNS resolver id]' \
                '--primary_upstream[string | Primary upstream endpoint]' \
                '--proto[string | Control D upstream type, either "doh" or "doh3" (default "doh")]' \
                '--secondary_upstream[string | Secondary upstream endpoint]' \
                '--skip_self_checks[Skip self checks after installing ctrld service]' \
            && ret=0
            ;;
      (clients)
        _values 'clients' \
          'list[List clients that ctrld discovered]' \
        && ret=0
        ;;
    esac
    ;;
  (subsubcmds)
    case "$line[1]" in
      (service)
        case "$line[2]" in
          (interfaces)
            _values 'interfaces' \
            'list[List network interfaces of the host]' \
                && ret=0
            ;;
        esac
        ;;
    esac
esac

return
compdef _ctrld ctrld
