# pkf bash completion.
# Source this file, or save into /usr/local/etc/bash_completion.d/.
# Suggested install:  pkf completion bash > ~/.bash_completion.d/pkf
#
# Dynamic task-name completion calls back to `pkf list` (without -v
# so the output is one name per line, prefix-trimmable).

_pkf() {
  local cur prev
  COMPREPLY=()
  cur="${COMP_WORDS[COMP_CWORD]}"
  prev="${COMP_WORDS[COMP_CWORD-1]}"

  # First positional = a subcommand, or a task name: `pkf build` is
  # shorthand for `pkf run build` whenever `build` is not a subcommand.
  if [ "$COMP_CWORD" -eq 1 ]; then
    local subs tasks
    subs="init list describe info run up trace doctor lint format hooks affected clean cache completion graph explain migrate pkl-cache version help"
    tasks=$(pkf list 2>/dev/null | awk '{print $1}')
    COMPREPLY=($(compgen -W "$subs $tasks" -- "$cur"))
    return
  fi

  local sub="${COMP_WORDS[1]}"
  case "$sub" in
    list)
      if [[ "$cur" == -* ]]; then
        COMPREPLY=($(compgen -W "-f --file -v --verbose --json --all" -- "$cur"))
      fi
      ;;
    lint)
      if [[ "$cur" == -* ]]; then
        COMPREPLY=($(compgen -W "-f --file --json" -- "$cur"))
      fi
      ;;
    doctor)
      if [[ "$cur" == -* ]]; then
        COMPREPLY=($(compgen -W "-f --file --json" -- "$cur"))
      fi
      ;;
    affected)
      if [[ "$cur" == -* ]]; then
        COMPREPLY=($(compgen -W "-f --file --check --changed=" -- "$cur"))
        return
      fi
      local tasks
      tasks=$(pkf list 2>/dev/null | awk '{print $1}')
      COMPREPLY=($(compgen -W "$tasks" -- "$cur"))
      ;;
    run)
      if [[ "$cur" == -* ]]; then
        COMPREPLY=($(compgen -W "-f --file --dry-run --print-hash --explain-cache --no-cache --refresh --timing --quiet --keep-going --profile --config --execution-log --remote-only -j --jobs" -- "$cur"))
        return
      fi
      local tasks
      tasks=$(pkf list 2>/dev/null | awk '{print $1}')
      COMPREPLY=($(compgen -W "$tasks" -- "$cur"))
      ;;
    clean|up)
      if [[ "$cur" == -* ]]; then return; fi
      local tasks
      tasks=$(pkf list 2>/dev/null | awk '{print $1}')
      COMPREPLY=($(compgen -W "$tasks" -- "$cur"))
      ;;
    trace)
      if [[ "$cur" == -* ]]; then
        COMPREPLY=($(compgen -W "-f --file --check --emit" -- "$cur"))
        return
      fi
      local tasks
      tasks=$(pkf list 2>/dev/null | awk '{print $1}')
      COMPREPLY=($(compgen -W "$tasks" -- "$cur"))
      ;;
    describe)
      if [[ "$cur" == -* ]]; then
        COMPREPLY=($(compgen -W "-f --file --json" -- "$cur"))
        return
      fi
      local tasks
      tasks=$(pkf list --all 2>/dev/null | awk '{print $1}')
      COMPREPLY=($(compgen -W "$tasks" -- "$cur"))
      ;;
    info)
      if [[ "$cur" == -* ]]; then
        COMPREPLY=($(compgen -W "-f --file --json --all" -- "$cur"))
      fi
      ;;
    hooks)
      if [ "$COMP_CWORD" -eq 2 ]; then
        COMPREPLY=($(compgen -W "install uninstall list" -- "$cur"))
      fi
      ;;
    cache)
      if [ "$COMP_CWORD" -eq 2 ]; then
        COMPREPLY=($(compgen -W "stats prune rm clear" -- "$cur"))
      fi
      ;;
    completion)
      if [ "$COMP_CWORD" -eq 2 ]; then
        COMPREPLY=($(compgen -W "bash zsh fish" -- "$cur"))
      fi
      ;;
    graph)
      if [[ "$prev" == "--format" ]]; then
        COMPREPLY=($(compgen -W "tree dot mermaid json" -- "$cur"))
      elif [[ "$prev" == "--target" ]]; then
        COMPREPLY=($(compgen -W "$(pkf list 2>/dev/null | awk '{print $1}')" -- "$cur"))
      elif [[ "$cur" == -* ]]; then
        COMPREPLY=($(compgen -W "-f --file --json --all --format --target --depth" -- "$cur"))
      fi
      ;;
    explain)
      if [[ "$cur" == -* ]]; then
        COMPREPLY=($(compgen -W "-f --file" -- "$cur"))
        return
      fi
      local tasks
      tasks=$(pkf list 2>/dev/null | awk '{print $1}')
      COMPREPLY=($(compgen -W "$tasks" -- "$cur"))
      ;;
    migrate)
      if [[ "$cur" == -* ]]; then
        COMPREPLY=($(compgen -W "-f --file --to --dry-run --skip-verify" -- "$cur"))
      fi
      ;;
    pkl-cache)
      if [ "$COMP_CWORD" -eq 2 ]; then
        COMPREPLY=($(compgen -W "warm" -- "$cur"))
      elif [[ "$cur" == -* ]]; then
        COMPREPLY=($(compgen -W "-f --file" -- "$cur"))
      fi
      ;;
  esac
}
complete -F _pkf pkf
