#!/usr/bin/env bash

# bash completion for mbake

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

    # Available commands
    cmds="init config validate format update completions"

    # Available options for main command
    opts="--version --help"

    if ((COMP_CWORD == 1)); then
        if [[ "$cur" == -* ]]; then
            COMPREPLY=($(compgen -W "$opts" -- "$cur"))
            return 0
        fi
        COMPREPLY=($(compgen -W "$cmds" -- "$cur"))
        return 0
    fi

    # Command-specific options
    local cmd="${COMP_WORDS[1]}"
    if [[ "$cur" == -* ]]; then
        case "$cmd" in
            init)
                COMPREPLY=($(compgen -W "--force --config --help" -- "${cur}"))
                return 0
                ;;
            config)
                COMPREPLY=($(compgen -W "--path --config --help" -- "${cur}"))
                return 0
                ;;
            validate)
                COMPREPLY=($(compgen -W "--config --verbose -v --help" -- "${cur}"))
                return 0
                ;;
            format)
                COMPREPLY=($(compgen -W "--check -c --diff -d --verbose -v --debug --config --backup -b --validate --help" -- "${cur}"))
                return 0
                ;;
            update)
                COMPREPLY=($(compgen -W "--force --check --yes -y --help" -- "${cur}"))
                return 0
                ;;
            completions)
                COMPREPLY=($(compgen -W "--help" -- "${cur}"))
                return 0
                ;;
        esac
    fi

    case "$prev" in
        completions)
            COMPREPLY=($(compgen -W "bash zsh fish" -- "$cur"))
            return 0
            ;;
    esac
} \
    && complete -o default -F _mbake_completion mbake
