#compdef gw worktrees gwa gwbc gwbp gwbs gwff gwl gwm gwp gwp! gwpa gwpa! gwr gwr! gws

# Flags as `gw <command> --help` lists them, and candidates from the CLI.
# A shortcut completes as the gw command it runs: gwa as `gw add`.

# TAG DESCRIPTION [WORD ...]: what `gw WORD ... --complete` prints, one
# name and description a line, separated by a tab.
__worktrees_candidates() {
    local tag=$1 descr=$2 line
    local -a names descs
    shift 2
    for line in ${(f)"$(command gw "$@" --complete 2>/dev/null)"}; do
        names+=( ${line%%$'\t'*} )
        descs+=( "${line%%$'\t'*}:${line#*$'\t'}" )
    done
    (( $#names )) || return 1
    _describe -t $tag $descr descs names
}

__worktrees_sync() {
    _arguments -s \
        '--json[sync state as data]' \
        '(-q --quiet)'{-q,--quiet}'[omit the summary]' \
        '(-v --verbose)'{-v,--verbose}'[print every command]' \
        '(-h --help)'{-h,--help}'[show the help and exit]'
}

__worktrees_version() {
    _arguments '(-h --help)'{-h,--help}'[show the help and exit]'
}

__worktrees_prune() {
    _arguments -s \
        '--table[assess without prompting; uncoloured table]' \
        '*--only[filter assessment; rejects --yes]:verdict:(remove empty keep unknown)' \
        '(-y --yes)'{-y,--yes}'[approve removal, including with --json or --table]' \
        '*--delete[permit deletion categories]:value:(empty ignored-files stashes all)' \
        '--json[assess as JSON; --yes approves removal]' \
        '(-q --quiet)'{-q,--quiet}'[verdicts only]' \
        '(-v --verbose)'{-v,--verbose}'[print every git command]' \
        '--explain[print every git command and exit]' \
        '(-h --help)'{-h,--help}'[show the help and exit]'
}

__worktrees_add() {
    _arguments -s \
        '--json[the result as data]' \
        '(-q --quiet)'{-q,--quiet}'[the path alone]' \
        '(-v --verbose)'{-v,--verbose}'[print every git command]' \
        '--explain[print every git command and exit]' \
        '(-h --help)'{-h,--help}'[show the help and exit]'
}

__worktrees_list() {
    _arguments -s \
        '--table[print them all and pick none]' \
        '--json[the result as data]' \
        '--explain[print every git command and exit]' \
        '(-h --help)'{-h,--help}'[show the help and exit]' \
        '1:worktree:{__worktrees_candidates worktrees worktree list}'
}

__worktrees_select-main() {
    _arguments -s \
        '--json[the result as data]' \
        '--explain[print every git command and exit]' \
        '(-h --help)'{-h,--help}'[show the help and exit]'
}

# `gw remove --complete` prints the worktrees remove can reach, which is not
# list's list: the main checkout is never one.
__worktrees_remove() {
    _arguments -s \
        '--keep-branch[retain the branch and its stashes]' \
        '*--delete[permit deletion categories]:value:(empty ignored-files stashes all)' \
        '--table[assess without prompting; uncoloured table]' \
        '(-y --yes)'{-y,--yes}'[approve removal, including with --json or --table]' \
        '--json[assess as JSON; --yes approves removal]' \
        '(-q --quiet)'{-q,--quiet}'[verdicts only]' \
        '(-v --verbose)'{-v,--verbose}'[print every git command]' \
        '--explain[print every git command and exit]' \
        '(-h --help)'{-h,--help}'[show the help and exit]' \
        '1:worktree:{__worktrees_candidates worktrees worktree remove}'
}

__worktrees_fast-forward() {
    _arguments -s \
        '--from[start that branch from this branch or commit]:branch or commit: ' \
        '--autostash[carry uncommitted changes across]' \
        '--json[the result as data]' \
        '(-q --quiet)'{-q,--quiet}'[omit summaries and progress]' \
        '(-v --verbose)'{-v,--verbose}'[print every git command]' \
        '--explain[print every git command and exit]' \
        '(-h --help)'{-h,--help}'[show the help and exit]'
}

__worktrees_ci-checks() {
    _arguments -s \
        '--json[the snapshot as data]' \
        '--wait[read again until no check is running]' \
        '(-v --verbose)'{-v,--verbose}'[print every forge command]' \
        '(-h --help)'{-h,--help}'[show the help and exit]'
}

__worktrees_branch_create() {
    _arguments -s \
        '--json[the result as data]' \
        '(-q --quiet)'{-q,--quiet}'[omit summaries and progress]' \
        '(-v --verbose)'{-v,--verbose}'[print every git command]' \
        '--explain[print every git command and exit]' \
        '(-h --help)'{-h,--help}'[show the help and exit]'
}

__worktrees_branch_status() {
    _arguments -s \
        '*--only[show only rows with this verdict]:verdict:(remove empty keep unknown)' \
        '--json[verdicts and evidence as data]' \
        '(-q --quiet)'{-q,--quiet}'[omit the summary]' \
        '--explain[print every git command and exit]' \
        '(-h --help)'{-h,--help}'[show the help and exit]' \
        '*:branch:{__worktrees_candidates branches branch branch status}'
}

__worktrees_branch_prune() {
    _arguments -s \
        '--all[assess all local branches for deletion]' \
        '--dry-run[report without deleting]' \
        '(-y --yes)'{-y,--yes}'[skip deletion confirmation]' \
        '*--delete[permit deletion categories]:value:(empty stashes all)' \
        '--json[verdicts and evidence as data]' \
        '(-q --quiet)'{-q,--quiet}'[omit the summary]' \
        '(-v --verbose)'{-v,--verbose}'[print every command]' \
        '--explain[print every git command and exit]' \
        '(-h --help)'{-h,--help}'[show the help and exit]' \
        '*:branch:{__worktrees_candidates branches branch branch prune}'
}

_gw() {
    local curcontext="$curcontext" state line
    local -a fixed

    case $service in
        gwa) fixed=(add) ;;
        gwbc) fixed=(branch create) ;;
        gwbp) fixed=(branch prune) ;;
        gwbs) fixed=(branch status) ;;
        gwff) fixed=(fast-forward) ;;
        gwl) fixed=(list) ;;
        gwm) fixed=(select-main) ;;
        gwp) fixed=(prune) ;;
        'gwp!') fixed=(prune --yes) ;;
        gwpa) fixed=(prune --delete all) ;;
        'gwpa!') fixed=(prune --delete all --yes) ;;
        gwr) fixed=(remove) ;;
        'gwr!') fixed=(remove --yes) ;;
        gws) fixed=(sync) ;;
    esac
    if (( $#fixed )); then
        words=(gw $fixed "${(@)words[2,-1]}")
        (( CURRENT += $#fixed ))
        service=gw
    fi

    _arguments -C \
        '(-h --help)'{-h,--help}'[show the help and exit]' \
        '1: :->command' \
        '*:: :->rest'

    case $state in
        command) __worktrees_candidates commands 'gw command' ;;
        rest)
            case $words[1] in
                sync) __worktrees_sync ;;
                version|v) __worktrees_version ;;
                prune|p) __worktrees_prune ;;
                add|a) __worktrees_add ;;
                list|l) __worktrees_list ;;
                select-main|main|m) __worktrees_select-main ;;
                remove|rm) __worktrees_remove ;;
                fast-forward|ff) __worktrees_fast-forward ;;
                ci-checks|ci) __worktrees_ci-checks ;;
                branch)
                    _arguments -C \
                        '(-h --help)'{-h,--help}'[show the help and exit]' \
                        '1: :->action' \
                        '*:: :->args'
                    case $state in
                        action) __worktrees_candidates commands 'branch command' branch ;;
                        args)
                            case $words[1] in
                                create) __worktrees_branch_create ;;
                                status) __worktrees_branch_status ;;
                                prune) __worktrees_branch_prune ;;
                            esac
                            ;;
                    esac
                    ;;
            esac
            ;;
    esac
}

_gw "$@"
