# Bash completion for Satori CLI tools
# Compatible with bash-completion and zsh (via bashcompinit)

_satori_cached_problems() {
    local cache_dir="${HOME}/.cache/satori"
    [ -d "$cache_dir" ] || return 0
    grep -ho '"code": *"[^"]*"' "$cache_dir"/contest_*_problems.json 2>/dev/null \
        | sed 's/"code": *"\([^"]*\)"/\1/' | sort -u
}

_satori_cached_submits() {
    local cache_dir="${HOME}/.cache/satori"
    [ -d "$cache_dir" ] || return 0
    grep -ho '"id": *[0-9]*' "$cache_dir"/contest_*_results.json 2>/dev/null \
        | sed 's/"id": *//' | head -n 50
}

_satori_problems() {
    local cur prev words cword
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev=""
    [ "$COMP_CWORD" -gt 0 ] && prev="${COMP_WORDS[COMP_CWORD-1]}"

    local global_opts="-h --help -c --config -s --section -H --host -u --username -p --password -m --machine -S --ssl -l --loglevel"
    local subcommands="dispatchers reporters reporter list status statement download-statement downloadstatement results result download test testresult upload render downloadsubmit"

    # Find first non-option subcommand
    local subcmd=""
    local i=1
    while [ "$i" -lt "$COMP_CWORD" ]; do
        case "${COMP_WORDS[$i]}" in
            -c|--config|-s|--section|-H|--host|-u|--username|-p|--password|-m|--machine|-l|--loglevel|-C|--contest|-o|--output|-d|--dir|-n|--limit|-P|--page|-t|--time)
                i=$((i + 2))
                continue
                ;;
            -*)
                i=$((i + 1))
                continue
                ;;
            *)
                subcmd="${COMP_WORDS[$i]}"
                break
                ;;
        esac
    done

    case "$prev" in
        -o|--output)
            COMPREPLY=( $(compgen -f -- "$cur") )
            return 0
            ;;
        -d|--dir)
            COMPREPLY=( $(compgen -d -- "$cur") )
            return 0
            ;;
        -c|--config)
            COMPREPLY=( $(compgen -f -- "$cur") )
            return 0
            ;;
        -l|--loglevel)
            COMPREPLY=( $(compgen -W "DEBUG INFO WARNING ERROR CRITICAL" -- "$cur") )
            return 0
            ;;
    esac

    if [ -z "$subcmd" ]; then
        if [[ "$cur" == -* ]]; then
            COMPREPLY=( $(compgen -W "$global_opts" -- "$cur") )
        else
            COMPREPLY=( $(compgen -W "$subcommands" -- "$cur") )
        fi
        return 0
    fi

    case "$subcmd" in
        list)
            COMPREPLY=( $(compgen -W "-s --status -r --refresh -C --contest -h --help" -- "$cur") )
            ;;
        status)
            COMPREPLY=( $(compgen -W "-r --refresh -C --contest -h --help" -- "$cur") )
            ;;
        statement)
            if [[ "$cur" == -* ]]; then
                COMPREPLY=( $(compgen -W "-r --refresh -C --contest --pdf -o --output -h --help" -- "$cur") )
            else
                COMPREPLY=( $(compgen -W "$(_satori_cached_problems)" -- "$cur") )
            fi
            ;;
        download-statement|downloadstatement)
            if [[ "$cur" == -* ]]; then
                COMPREPLY=( $(compgen -W "-a --all -o --output -d --dir -C --contest -h --help" -- "$cur") )
            else
                COMPREPLY=( $(compgen -W "$(_satori_cached_problems)" -- "$cur") )
            fi
            ;;
        results)
            COMPREPLY=( $(compgen -W "-n --limit -P --page -a --all -r --refresh -C --contest -h --help" -- "$cur") )
            ;;
        result|downloadsubmit)
            if [[ "$cur" == -* ]]; then
                COMPREPLY=( $(compgen -W "-r --refresh -C --contest -h --help" -- "$cur") )
            else
                COMPREPLY=( $(compgen -W "$(_satori_cached_submits)" -- "$cur") )
            fi
            ;;
        test)
            if [[ "$cur" == -* ]]; then
                COMPREPLY=( $(compgen -W "-2 --results2d -t --time -v --verbose -o --override --store_io -h --help" -- "$cur") )
            else
                COMPREPLY=( $(compgen -f -- "$cur") )
            fi
            ;;
        upload|render)
            COMPREPLY=( $(compgen -f -- "$cur") )
            ;;
        *)
            COMPREPLY=( $(compgen -W "-h --help" -- "$cur") )
            ;;
    esac
}

_satori_download_statement() {
    local cur prev
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev=""
    [ "$COMP_CWORD" -gt 0 ] && prev="${COMP_WORDS[COMP_CWORD-1]}"

    case "$prev" in
        -o|--output)
            COMPREPLY=( $(compgen -f -- "$cur") )
            return 0
            ;;
        -d|--dir)
            COMPREPLY=( $(compgen -d -- "$cur") )
            return 0
            ;;
    esac

    if [[ "$cur" == -* ]]; then
        COMPREPLY=( $(compgen -W "-a --all -o --output -d --dir -C --contest -h --help" -- "$cur") )
    else
        COMPREPLY=( $(compgen -W "$(_satori_cached_problems)" -- "$cur") )
    fi
}

_satori_submit() {
    local cur prev
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev=""
    [ "$COMP_CWORD" -gt 0 ] && prev="${COMP_WORDS[COMP_CWORD-1]}"

    case "$prev" in
        -C|--contest|-t|--timeout|-c|--config|-s|--section|-H|--host|-u|--username|-p|--password|-m|--machine|-l|--loglevel)
            return 0
            ;;
    esac

    if [[ "$cur" == -* ]]; then
        COMPREPLY=( $(compgen -W "-C --contest -W --no-wait -t --timeout -h --help" -- "$cur") )
        return 0
    fi

    # Count positional arguments before cursor
    local pos=0
    local i=1
    while [ "$i" -lt "$COMP_CWORD" ]; do
        case "${COMP_WORDS[$i]}" in
            -C|--contest|-t|--timeout|-c|--config|-s|--section|-H|--host|-u|--username|-p|--password|-m|--machine|-l|--loglevel)
                i=$((i + 2))
                continue
                ;;
            -*)
                i=$((i + 1))
                continue
                ;;
            *)
                pos=$((pos + 1))
                i=$((i + 1))
                ;;
        esac
    done

    if [ "$pos" -eq 0 ]; then
        COMPREPLY=( $(compgen -W "$(_satori_cached_problems)" -- "$cur") )
    else
        COMPREPLY=( $(compgen -f -- "$cur") )
    fi
}

_satori_common() {
    local cur
    cur="${COMP_WORDS[COMP_CWORD]}"
    if [[ "$cur" == -* ]]; then
        COMPREPLY=( $(compgen -W "-h --help -c --config -s --section -H --host -u --username -p --password -m --machine -S --ssl -l --loglevel" -- "$cur") )
    fi
}

complete -F _satori_problems satori.problems
complete -F _satori_download_statement satori.download-statement satori.download_statement
complete -F _satori_submit satori.submit
complete -F _satori_common satori.passwd satori.login satori.change_contest satori.console
