# bash completion for lazypto
# Source this file or copy to /etc/bash_completion.d/ or ~/.local/share/bash-completion/completions/

_lazypto_completions() {
    local cur prev words cword
    _init_completion || return

    local subcommands="add list clean config"
    local add_flags="-description -from -to -import"
    local list_flags="-old -format"
    local config_subcommands="show set reset"
    local config_keys="lookahead_days archive_after_days non_working_days"
    local format_values="csv json"

    # Complete subcommands at position 1
    if [ $cword -eq 1 ]; then
        COMPREPLY=($(compgen -W "$subcommands" -- "$cur"))
        return
    fi

    local subcommand="${words[1]}"

    case "$subcommand" in
        add)
            case "$prev" in
                -description)
                    # No completion for description (free text)
                    return
                    ;;
                -from|-to)
                    # Suggest today's date in YYYY-MM-DD format
                    COMPREPLY=($(compgen -W "$(date +%Y-%m-%d)" -- "$cur"))
                    return
                    ;;
                -import)
                    # Complete file paths
                    _filedir
                    return
                    ;;
                *)
                    COMPREPLY=($(compgen -W "$add_flags" -- "$cur"))
                    return
                    ;;
            esac
            ;;
        list)
            case "$prev" in
                -format)
                    COMPREPLY=($(compgen -W "$format_values" -- "$cur"))
                    return
                    ;;
                *)
                    COMPREPLY=($(compgen -W "$list_flags" -- "$cur"))
                    return
                    ;;
            esac
            ;;
        clean)
            # clean command has no flags
            return
            ;;
        config)
            # Handle config subcommands
            if [ $cword -eq 2 ]; then
                COMPREPLY=($(compgen -W "$config_subcommands" -- "$cur"))
                return
            fi

            local config_cmd="${words[2]}"
            case "$config_cmd" in
                set)
                    if [ $cword -eq 3 ]; then
                        COMPREPLY=($(compgen -W "$config_keys" -- "$cur"))
                        return
                    elif [ $cword -eq 4 ]; then
                        local key="${words[3]}"
                        case "$key" in
                            non_working_days)
                                COMPREPLY=($(compgen -W "Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday" -- "$cur"))
                                return
                                ;;
                            lookahead_days|archive_after_days)
                                # Suggest some common values
                                COMPREPLY=($(compgen -W "30 60 90 120 180 365" -- "$cur"))
                                return
                                ;;
                        esac
                    fi
                    ;;
                show|reset)
                    # No additional arguments
                    return
                    ;;
            esac
            ;;
    esac
}

complete -F _lazypto_completions lazypto
