#compdef bloop
_bloop() {
  local -a commands
  commands=(
    'inbox:the newest emails in the Inbox'
    'list:a mailbox: sent, drafts, archive, flagged, snoozed, spam or all'
    'search:emails by words, people and times'
    'read:one email in full'
    'thread:its whole conversation'
    'save:an email'"'"'s files, to Downloads'
    'archive:archive emails'
    'trash:move emails to Trash'
    'junk:move emails to Spam'
    'not-spam:move emails out of Spam'
    'mark:read, unread, flagged or unflagged'
    'move:move emails to a folder'
    'label:label emails'
    'snooze:snooze emails until a time'
    'unsnooze:bring snoozed emails back'
    'done:stop waiting for a reply'
    'undo:undo the last change'
    'sync:check for new mail'
    'open:open an email in Bloop.email'
    'draft:start an email in Bloop.email'
    'send:send an email'
    'reply:reply to an email'
    'forward:forward an email'
    'outbox:what'"'"'s waiting to go'
    'send-now:send a waiting email now'
    'cancel-send:throw away a waiting email'
    'reschedule:send a waiting email at another time'
    'follow-ups:sent emails waiting for a reply'
    'subscriptions:newsletters and who sends them'
    'unsubscribe:leave a mailing list'
    'sort:sort mail into its groups again'
    'accounts:your accounts'
    'account:one account in full'
    'folders:every folder'
    'labels:every label'
    'groups:the email classifier'"'"'s groups'
    'rules:the rules for new mail'
    'templates:your templates'
    'template:one template in full'
    'signatures:your signatures'
    'events:calendar invitations'
    'selection:what Bloop.email is showing'
    'watch:print new emails as they come'
    'wait:wait for an email like that to come in'
    'pick:choose an email with fzf'
    'status:the unread count, for a status bar'
    'config:settings in ~/.config/bloop/config.toml'
    'version:the version'
    'tools:what Bloop can do'
    'call:any tool'
    'completion:this completion'
    'help:every command'
  )
  if (( CURRENT == 2 )); then
    _describe 'command' commands
    return
  fi
  case $words[2] in
    help) _values 'command' ${commands%%:*} ;;
    list|ls|watch|pick) _values 'mailbox' inbox sent drafts archive flagged snoozed spam all ;;
    mark) (( CURRENT == 3 )) && _values 'state' read unread flagged unflagged ;;
    move) (( CURRENT == 3 )) && _values 'folder' ${(f)"$(bloop folders --names 2>/dev/null)"} ;;
    label) (( CURRENT == 3 )) && _values 'label' ${(f)"$(bloop labels --names 2>/dev/null)"} ;;
    tools|call) (( CURRENT == 3 )) && _values 'tool' ${(f)"$(bloop tools --names 2>/dev/null)"} ;;
    sort) _arguments '1:folders:(inbox inbox-and-archive every-folder)' '--since[from a day, like 2026-01-31]:' '--account[an account]:' ;;
    completion) _values 'shell' zsh bash fish ;;
    send|draft|reply|forward)
      _arguments '*--to[addresses]:' '*--cc[addresses]:' '--subject[subject]:' '--body[text, or - for standard input]:' \
        '*--attach[a file]:file:_files' '--template[a template]:' '--signature[a signature]:' '--send-at[when]:' \
        '--all[everyone on it]' '--yes[without asking]' ;;
    save) _arguments '--to[folder]:folder:_files -/' ;;
    wait) _arguments '--from[from]:' '--to[to]:' '--subject[subject]:' '--text[text]:' '--timeout[like 2m]:' '--since[like 10m]:' '--link[print its link]' '--json[as JSON]' ;;
    *) _arguments '--json[as JSON]' '--jq[a jq filter]:' '--format[a line per email]:' '--count[how many]' '--ids[only ids]' \
         '--unread[only unread]' '--limit[how many]:' '--offset[how many to skip]:' '--account[an account]:' '--dry-run[change nothing]' ;;
  esac
}
_bloop "$@"
