# pkf fish completion.
# Suggested install:  pkf completion fish > ~/.config/fish/completions/pkf.fish

function __pkf_tasks
    pkf list 2>/dev/null | awk '{print $1}'
end

# `pkf build` is shorthand for `pkf run build`, so a task name is a
# valid first word wherever it does not collide with a subcommand.
complete -c pkf -f -n '__fish_use_subcommand' -a '(__pkf_tasks)' -d 'task'

# Subcommands when no subcommand has been picked yet.
complete -c pkf -f -n '__fish_use_subcommand' -a init      -d 'write a starter Taskfile.pkl'
complete -c pkf -f -n '__fish_use_subcommand' -a list      -d 'list declared tasks'
complete -c pkf -f -n '__fish_use_subcommand' -a describe  -d 'show a single task surface'
complete -c pkf -f -n '__fish_use_subcommand' -a info      -d 'structured snapshot of the Taskfile'
complete -c pkf -f -n '__fish_use_subcommand' -a run       -d 'run one or more tasks'
complete -c pkf -f -n '__fish_use_subcommand' -a up        -d 'supervise long-running services'
complete -c pkf -f -n '__fish_use_subcommand' -a doctor    -d 'diagnose pkfire setup'
complete -c pkf -f -n '__fish_use_subcommand' -a lint      -d 'detect Taskfile issues'
complete -c pkf -f -n '__fish_use_subcommand' -a format    -d 'pkl format -w wrapper'
complete -c pkf -f -n '__fish_use_subcommand' -a hooks     -d 'manage .git/hooks shims'
complete -c pkf -f -n '__fish_use_subcommand' -a affected  -d 'run tasks whose inputs changed since <ref>'
complete -c pkf -f -n '__fish_use_subcommand' -a clean     -d 'remove declared outputs'
complete -c pkf -f -n '__fish_use_subcommand' -a cache     -d 'inspect / clean the local CAS'
complete -c pkf -f -n '__fish_use_subcommand' -a completion -d 'emit shell completion script'
complete -c pkf -f -n '__fish_use_subcommand' -a trace     -d 'run a task under the fspy shim and audit its declared inputs'
complete -c pkf -f -n '__fish_use_subcommand' -a graph     -d 'emit DAG (dot / mermaid / tree / json)'
complete -c pkf -f -n '__fish_use_subcommand' -a explain   -d 'dump or compare inputs to the action key'
complete -c pkf -f -n '__fish_use_subcommand' -a migrate   -d 'rewrite Taskfile schema version'
complete -c pkf -f -n '__fish_use_subcommand' -a pkl-cache -d 'warm the local Pkl package cache'
complete -c pkf -f -n '__fish_use_subcommand' -a version   -d 'print pkf version'
complete -c pkf -f -n '__fish_use_subcommand' -a help      -d 'show usage'

# Dynamic task names for the subcommands that take them.
complete -c pkf -f -n '__fish_seen_subcommand_from run affected clean up trace explain describe' -a '(__pkf_tasks)'

# Nested subcommand completions.
complete -c pkf -f -n '__fish_seen_subcommand_from hooks' -a 'install uninstall list'
complete -c pkf -f -n '__fish_seen_subcommand_from cache' -a 'stats prune rm clear'
complete -c pkf -f -n '__fish_seen_subcommand_from completion' -a 'bash zsh fish'
complete -c pkf -f -n '__fish_seen_subcommand_from list' -s v -l verbose -d 'show cmd preview and deps'
complete -c pkf -f -n '__fish_seen_subcommand_from list' -l json -d 'emit machine-readable output'
complete -c pkf -f -n '__fish_seen_subcommand_from list' -l all -d 'include internal tasks'
complete -c pkf -f -n '__fish_seen_subcommand_from lint' -l json -d 'emit machine-readable output'
complete -c pkf -f -n '__fish_seen_subcommand_from doctor' -l json -d 'emit machine-readable output'
complete -c pkf -f -n '__fish_seen_subcommand_from affected' -l check -d 'run workflowTests'
complete -c pkf -f -n '__fish_seen_subcommand_from affected' -l changed -d 'read newline-separated changed paths from a file'
complete -c pkf -f -n '__fish_seen_subcommand_from run' -l dry-run -d 'preview plan without running'
complete -c pkf -f -n '__fish_seen_subcommand_from run' -l print-hash -d 'print action keys and exit'
complete -c pkf -f -n '__fish_seen_subcommand_from run' -l explain-cache -d 'explain cache decisions and exit'
complete -c pkf -f -n '__fish_seen_subcommand_from run' -l no-cache -d 'disable cache for this run'
complete -c pkf -f -n '__fish_seen_subcommand_from run' -l refresh -d 'skip lookup but store outputs'
complete -c pkf -f -n '__fish_seen_subcommand_from run' -l timing -d 'print task durations'
complete -c pkf -f -n '__fish_seen_subcommand_from run' -l execution-log -d 'write a JSON Lines record of the run'
complete -c pkf -f -n '__fish_seen_subcommand_from run' -l config -d 'build setting KEY=VALUE for this run'
complete -c pkf -f -n '__fish_seen_subcommand_from run' -l quiet -d 'suppress per-task log lines'
complete -c pkf -f -n '__fish_seen_subcommand_from run' -l keep-going -d 'do not stop on first failure'
complete -c pkf -f -n '__fish_seen_subcommand_from run' -l profile -d 'profile name for cache keys'
complete -c pkf -f -n '__fish_seen_subcommand_from run' -l remote-only -d 'consult only remote cache'
complete -c pkf -f -n '__fish_seen_subcommand_from run' -s j -d 'actions to run at once, or auto'
complete -c pkf -f -n '__fish_seen_subcommand_from run' -l jobs -d 'actions to run at once, or auto'
complete -c pkf -f -n '__fish_seen_subcommand_from trace' -s f -l file -d 'path to Taskfile.pkl'
complete -c pkf -f -n '__fish_seen_subcommand_from trace' -l check -d 'fail when a file is read but not declared'
complete -c pkf -f -n '__fish_seen_subcommand_from trace' -l emit -d 'print a matching inputs block'
complete -c pkf -f -n '__fish_seen_subcommand_from clean' -l dry-run -d 'list what would be removed, remove nothing'

complete -c pkf -f -n '__fish_seen_subcommand_from graph' -l json -d 'emit machine-readable output'
complete -c pkf -f -n '__fish_seen_subcommand_from graph' -l format -a 'tree dot mermaid json' -d 'output format'
complete -c pkf -f -n '__fish_seen_subcommand_from graph' -l target -a '(__pkf_tasks)' -d 'only this task and what it depends on'
complete -c pkf -f -n '__fish_seen_subcommand_from graph' -l depth -d 'follow at most N dependency hops'
complete -c pkf -f -n '__fish_seen_subcommand_from graph' -l all -d 'include internal tasks'

complete -c pkf -f -n '__fish_seen_subcommand_from explain' -s f -l file -d 'path to Taskfile.pkl'
complete -c pkf -f -n '__fish_seen_subcommand_from describe' -s f -l file -d 'path to Taskfile.pkl'
complete -c pkf -f -n '__fish_seen_subcommand_from describe' -l json -d 'emit machine-readable output'
complete -c pkf -f -n '__fish_seen_subcommand_from info' -s f -l file -d 'path to Taskfile.pkl'
complete -c pkf -f -n '__fish_seen_subcommand_from info' -l json -d 'emit machine-readable output'
complete -c pkf -f -n '__fish_seen_subcommand_from info' -l all -d 'include internal tasks'
complete -c pkf -f -n '__fish_seen_subcommand_from migrate' -s f -l file -d 'path to Taskfile.pkl'
complete -c pkf -f -n '__fish_seen_subcommand_from migrate' -l to -d 'target schema version'
complete -c pkf -f -n '__fish_seen_subcommand_from migrate' -l dry-run -d 'preview rewrite without writing'
complete -c pkf -f -n '__fish_seen_subcommand_from migrate' -l skip-verify -d 'skip post-migration pkl eval'
complete -c pkf -f -n '__fish_seen_subcommand_from pkl-cache' -a warm -d 'pre-evaluate Pkl files'
complete -c pkf -f -n '__fish_seen_subcommand_from pkl-cache' -s f -l file -d 'path to Taskfile.pkl'
