#compdef valley
# zsh completion for valley. The verb list mirrors the case statement in
# bin/valley — update both together.
#
# NEVER fetch from here: completion runs on every <TAB> and must be instant.
# Candidates come from the remote-tracking refs of the last fetch; valley's
# own verbs fetch before acting, so stale candidates cost nothing.

_valley() {
  if (( CURRENT == 2 )); then
    local -a verbs=(
      'pending:list the changes not yet landed, the unrecorded branches, and the drafts'
      'changes:print every change, across the host, as valley-changes/v1 JSON'
      'review:page through a branch, then approve, reject, or skip'
      'approve:approve a change without paging it, filing its integration request'
      'rebase:rebase a change onto origin/main and push it with a lease'
      'status:read the integration result for a change from origin'
      'ready:mark a head of a branch ready as a new version of its change'
      'unready:make a ready change a draft'
      'withdraw:mark a ready change or a draft withdrawn'
      'checks:print the checks a change owes under the composed policy'
      'tail:watch bus events scroll by'
      'replay:publish ref-updated events for the current refs of a repo'
      'help:show help'
    )
    _describe -t verbs 'valley verb' verbs
  elif (( CURRENT == 3 )) && [[ ${words[2]} == replay ]]; then
    _files -/
  elif [[ ${words[2]} == checks && ${words[CURRENT-1]} == --cost ]]; then
    compadd quick slow
  elif [[ ${words[2]} == checks && ${words[CURRENT]} == -* ]]; then
    local -a opts=(
      '--schema:the policy schema to compose against'
      '--instance:the instance policy layer directory'
      '--project:the project policy layer directory'
      '--names:print only the owed check names, one per line'
      '--cost:with --names, only the quick or the slow checks'
    )
    _describe -t options 'valley checks option' opts
  elif [[ ${words[2]} == ready && ${words[CURRENT]} == -* ]]; then
    local -a opts=(
      '--head:the head to mark ready, already on origin'
      '--change:the change ID to record the version under'
      '--if-active:add the version only while the change is ready at this active head'
      '--stamp:carry the change ID in the head as a Change-Id trailer'
    )
    _describe -t options 'valley ready option' opts
  elif [[ ${words[2]} == ready && ${words[CURRENT-1]} == --(head|change|if-active) ]]; then
    return
  elif [[ ${words[2]} == approve && ${words[CURRENT]} == -* ]]; then
    local -a opts=('--head:the exact head to approve, one of the change'"'"'s version heads')
    _describe -t options 'valley approve option' opts
  elif [[ ${words[2]} == approve && ${words[CURRENT-1]} == --head ]]; then
    return
  elif [[ ${words[2]} == pending && ${words[CURRENT]} == -* ]]; then
    local -a opts=(
      '--all:every project the serving host publishes'
      '--repo:that one project'
      '--owes:the checks each change owes, by cost'
      '--json:print valley-pending/v1 JSON'
    )
    _describe -t options 'valley pending option' opts
  elif [[ ${words[2]} == changes && ${words[CURRENT]} == -* ]]; then
    local -a opts=(
      '--json:print valley-changes/v1 JSON'
      '--all:every project the serving host publishes'
      '--repo:that one project'
    )
    _describe -t options 'valley changes option' opts
  elif [[ ${words[2]} == status && ${words[CURRENT]} == -* ]]; then
    local -a opts=('--json:print the status as one valley-status/v1 JSON object')
    _describe -t options 'valley status option' opts
  elif [[ ${words[2]} == (review|approve|rebase|status|ready) ]] \
    && { (( CURRENT == 3 )) || [[ ${words[2]} == (status|ready|approve) ]]; }; then
    # Cached topic branches on origin. Review, approve, rebase and ready filter
    # out merged branches; status needs those too. Silent outside a git
    # repo — no errors mid-typing.
    local -a branches selection
    selection=()
    [[ ${words[2]} != status ]] && selection=(--no-merged=origin/main)
    branches=(${(f)"$(git for-each-ref refs/remotes/origin \
      --format='%(refname)' "${selection[@]}" 2>/dev/null)"})
    branches=(${branches:#refs/remotes/origin/HEAD})
    branches=(${branches:#refs/remotes/origin/main})
    branches=(${branches#refs/remotes/origin/})
    (( ${#branches} )) && compadd -a branches
  fi
}

_valley "$@"
