#compdef keepassxc-cli

_keepassxc-cli_commands() {
  local -a commands
  commands=(
    'add:Add a new entry to a database.'
    'analyze:Analyze passwords for weaknesses and problems.'
    'attachment-export:Export an attachment of an entry.'
    'attachment-import:Imports an attachment to an entry.'
    'attachment-rm:Remove an attachment of an entry.'
    'clip:Copy an entry'\''s attribute to the clipboard.'
    'close:Close the currently opened database.'
    'db-create:Create a new database.'
    'db-edit:Edit a database.'
    'db-info:Show a databases information.'
    'diceware:Generate a new random diceware passphrase.'
    'edit:Edit an entry.'
    'estimate:Estimate the entropy of a password.'
    'export:Exports the content of a database to standard output in the specified format.'
    'generate:Generate a new random password.'
    'help:Display command help.'
    'import:Import the contents of an XML database.'
    'ls:List database entries.'
    'merge:Merge two databases.'
    'mkdir:Adds a new group to a database.'
    'mv:Moves an entry to a new group.'
    'open:Open a database.'
    'rm:Remove an entry from the database.'
    'rmdir:Removes a group from a database.'
    'search:Find entries quickly.'
    'show:Show an entry'\''s information.'
  )
  _describe 'command' commands
}

_keepassxc-cli() {
  local context curcontext=$curcontext state line ret=1
  declare -A opt_args

  _arguments -C \
    '--debug-info[Displays debugging information]' \
    '(-h --help)'{-h,--help}'[Displays help on commandline options]' \
    '--help-all[Displays help including Qt specific options]' \
    '(-v --version)'{-v,--version}'[Displays version information]' \
    '1: :_keepassxc-cli_commands' \
    '*:: :->args' \
    && ret=0

  local -a database
  database=("${KEEPASSXC_DATABASE_CLI:=${XDG_DATA_HOME:=${HOME%/}/.local/share}/pa/cli.kbdx}")

  case $state in
    args)
      case $words[1] in
        add)
          _arguments \
            {-q,--quiet}'[Silence password prompt and other secondary outputs]' \
            {-k,--key-file}'[Key file of the database]:path:' \
            '--no-password[Deactivate password key for the database]' \
            {-y,--yubikey}'[Yubikey slot and optional serial used to access the database (e.g., 1:7370001)]:slot[\:serial]:(1\: 2\:)' \
            {-u,--username}'[Username for the entry]:username:' \
            '--url[URL for the entry]:URL:' \
            '--notes[Notes for the entry]:Notes:' \
            {-p,--password-prompt}'[Prompt for the entry'\''s password]' \
            {-g,--generate}'[Generate a password for the entry]' \
            {-L,--length}'[Length of the generated password]:length:' \
            {-l,--lower}'[Use lowercase characters]' \
            {-U,--upper}'[Use uppercase characters]' \
            {-n,--numeric}'[Use numbers]' \
            {-s,--special}'[Use special characters]' \
            {-e,--extended}'[Use extended ASCII]' \
            {-x,--exclude}'[Exclude character set]:chars:' \
            '--exclude-similar[Exclude similar looking characters]' \
            '--every-group[Include characters from every selected group]' \
            {-c,--custom}'[Use custom character set]:chars:' \
            {-h,--help}'[Display this help]' \
            && ret=0
          local -a add_args=(
            'database:Path of the database'
            'entry:Path of the entry to add'
          )
          _describe 'arguments' add_args
          ;;
        analyze)
          _arguments \
            {-q,--quiet}'[Silence password prompt and other secondary outputs]' \
            {-k,--key-file}'[Key file of the database]:path:' \
            '--no-password[Deactivate password key for the database]' \
            {-y,--yubikey}'[Yubikey slot and optional serial used to access the database (e.g., 1:7370001)]:slot[\:serial]:(1\: 2\:)' \
            {-H,--hibp}'[Check if any passwords have been publicly leaked]:FILENAME:' \
            '--okon[Path to okon-cli to search a formatted HIBP file]:okon-cli:' \
            {-h,--help}'[Display this help]' \
            && ret=0
          ;;
        clip)
          _arguments \
            {-q,--quiet}'[Silence password prompt and other secondary outputs]' \
            {-k,--key-file}'[Key file of the database]:path:' \
            '--no-password[Deactivate password key for the database]' \
            {-y,--yubikey}'[Yubikey slot and optional serial used to access the database (e.g., 1:7370001)]:slot[\:serial]:(1\: 2\:)' \
            {-a,--attribute}'[Copy the given attribute to the clipboard]:attr:' \
            {-t,--totp}'[Copy the current TOTP to the clipboard]' \
            {-b,--best-match}'[Must match only one entry, otherwise a list of possible matches]' \
            {-h,--help}'[Display this help]' \
            && ret=0
          # local -a clip_args=(
          #   'database:Path of the database'
          #   'entry:Path of the entry to clip'
          #   'timeout:Timeout before clearing the clipboard'
          # )
          # _describe 'arguments' clip_args
          ;;
        db-info)
          _arguments \
            {-q,--quiet}'[Silence password prompt and other secondary outputs]' \
            {-k,--key-file}'[Key file of the database]:path:' \
            '--no-password[Deactivate password key for the database]' \
            {-y,--yubikey}'[Yubikey slot and optional serial used to access the database (e.g., 1:7370001)]:slot[\:serial]:(1\: 2\:)' \
            {-h,--help}'[Display this help]' \
            && ret=0
          ;;
        diceware)
          _arguments \
            {-q,--quiet}'[Silence password prompt and other secondary outputs]' \
            {-W,--words}'[Word count for the diceware passphrase]:count:' \
            {-w,--word-list}'[Wordlist for the diceware generator (Default: EFF English)]:path:' \
            {-h,--help}'[Display this help]' \
            && ret=0
          ;;
        edit)
          _arguments \
            {-q,--quiet}'[Silence password prompt and other secondary outputs]' \
            {-k,--key-file}'[Key file of the database]:path:' \
            '--no-password[Deactivate password key for the database]' \
            {-y,--yubikey}'[Yubikey slot and optional serial used to access the database (e.g., 1:7370001)]:slot[\:serial]:(1\: 2\:)' \
            {-u,--username}'[Username for the entry]:username:' \
            '--url[URL for the entry]:URL:' \
            '--notes[Notes for the entry]:Notes:' \
            {-p,--password-prompt}'[Prompt for the entry'\''s password]' \
            {-t,--title}'[Title for the entry]:title:' \
            {-g,--generate}'[Generate a password for the entry]' \
            {-L,--length}'[Length of the generated password]:length:' \
            {-l,--lower}'[Use lowercase characters]' \
            {-U,--upper}'[Use uppercase characters]' \
            {-n,--numeric}'[Use numbers]' \
            {-s,--special}'[Use special characters]' \
            {-e,--extended}'[Use extended ASCII]' \
            {-x,--exclude}'[Exclude character set]:chars:' \
            '--exclude-similar[Exclude similar looking characters]' \
            '--every-group[Include characters from every selected group]' \
            {-c,--custom}'[Use custom character set]:chars:' \
            {-h,--help}'[Display this help]' \
            && ret=0
          ;;
        estimate)
          _arguments \
            {-q,--quiet}'[Silence password prompt and other secondary outputs]' \
            {-a,--advanced}'[Perform advanced analysis on the password]' \
            {-h,--help}'[Display this help]' \
            && ret=0
          ;;
        export)
          _arguments \
            {-q,--quiet}'[Silence password prompt and other secondary outputs]' \
            {-k,--key-file}'[Key file of the database]:path:' \
            '--no-password[Deactivate password key for the database]' \
            {-y,--yubikey}'[Yubikey slot and optional serial used to access the database (e.g., 1:7370001)]:slot[\:serial]:(1\: 2\:)' \
            {-f,--format}'[Format to use when exporting (Default: xml)]:format:(xml csv html)' \
            {-h,--help}'[Display this help]' \
            && ret=0
          ;;
        generate)
          _arguments \
            {-q,--quiet}'[Silence password prompt and other secondary outputs]' \
            {-L,--length}'[Length of the generated password]:length:' \
            {-l,--lower}'[Use lowercase characters]' \
            {-U,--upper}'[Use uppercase characters]' \
            {-n,--numeric}'[Use numbers]' \
            {-s,--special}'[Use special characters]' \
            {-e,--extended}'[Use extended ASCII]' \
            {-x,--exclude}'[Exclude character set]:chars:' \
            '--exclude-similar[Exclude similar looking characters]' \
            '--every-group[Include characters from every selected group]' \
            {-c,--custom}'[Use custom character set]:chars:' \
            {-h,--help}'[Display this help]' \
            && ret=0
          ;;
        help)
          local -a values expl
          values=$_keepassxc-cli_commands
          _wanted values expl $state _keepassxc-cli_commands \
            && ret=0
          ;;
        ls)
          _arguments \
            {-q,--quiet}'[Silence password prompt and other secondary outputs]' \
            {-k,--key-file}'[Key file of the database]:path:' \
            '--no-password[Deactivate password key for the database]' \
            {-y,--yubikey}'[Yubikey slot and optional serial used to access the database (e.g., 1:7370001)]:slot[\:serial]:(1\: 2\:)' \
            {-R,--recursive}'[Recursively list the elements of the group]' \
            {-f,--flatten}'[Flattens the output to single lines]' \
            {-h,--help}'[Display this help]' \
            && ret=0
          compadd $database
          # local -a ls_args=(
          #   'database:Path of the database'
          #   'group:Path of the group to list'
          # )
          # _describe 'arguments' ls_args
          ;;
        open)
          _arguments \
            {-q,--quiet}'[Silence password prompt and other secondary outputs]' \
            {-k,--key-file}'[Key file of the database]:path:' \
            '--no-password[Deactivate password key for the database]' \
            {-y,--yubikey}'[Yubikey slot and optional serial used to access the database (e.g., 1:7370001)]:slot[\:serial]:(1\: 2\:)' \
            {-h,--help}'[Display this help]' \
            && ret=0
          compadd $database
          ;;
        search)
          _arguments \
            {-q,--quiet}'[Silence password prompt and other secondary outputs]' \
            {-k,--key-file}'[Key file of the database]:path:' \
            '--no-password[Deactivate password key for the database]' \
            {-y,--yubikey}'[Yubikey slot and optional serial used to access the database (e.g., 1:7370001)]:slot[\:serial]:(1\: 2\:)' \
            {-h,--help}'[Display this help]' \
            && ret=0
          ;;
        show)
          _arguments \
            {-q,--quiet}'[Silence password prompt and other secondary outputs]' \
            {-k,--key-file}'[Key file of the database]:path:' \
            '--no-password[Deactivate password key for the database]' \
            {-y,--yubikey}'[Yubikey slot and optional serial used to access the database (e.g., 1:7370001)]:slot[\:serial]:(1\: 2\:)' \
            {-t,--totp}'[Show the entry'\''s current TOTP]' \
            {-a,--attributes}'[Names of the attributes to show]:attribute:' \
            {-s,--show-protected}'[Show the protected attributes in clear text]' \
            '--all[Show all the attributes of the entry]' \
            '--show-attachments[Show the attachments of the entry]' \
            {-h,--help}'[Display this help]' \
            && ret=0
          ;;
      esac
      ;;
  esac
  return ret
}

_keepassxc-cli "$@"
