# shellcheck shell=dash

# x gh , 'add collaborator ljh to repo x-cmd/abc ( push permission )'
# X_COCO_MSHOT_CMD="command seed ..." ___x_cmd_co --mshot "<USER-REQUSET>"


___x_cmd_coco_context(){
    [ "$#" -gt 0 ] || {
        ___x_cmd help -m coco -c >&2
        return 64
    }

    local X_COCO_MSHOT_CMD="x $1"
    shift; ___x_cmd_coco___mshot "$@"
}

___x_cmd_coco___mshot(){
    local X_COCO_MSHOT_CMD="$X_COCO_MSHOT_CMD"
    local provider=""
    local model=""
    # [ $# -gt 0 ]    ||  set -- -h
    while [ "$#" -gt 0 ]; do
        case "$1" in
            --provider)     provider="$2"; [ -n "$2" ] || N=coco M="Please provide provider value" log:ret:64;    shift 2 ;;
            --model)        model="$2";    [ -n "$2" ] || N=coco M="Please provide model value" log:ret:64;       shift 2 ;;
            -h|--help)      ___x_cmd help -m coco --mshot; return 0 ;;
            *)              break ;;
        esac
    done

    [ -n "$X_COCO_MSHOT_CMD" ] || N=coco M="Please provide command seed, X_COCO_MSHOT_CMD is empty" log:ret:64
    coco:debug --X_COCO_MSHOT_CMD "$X_COCO_MSHOT_CMD" --provider "$provider" --model "$model" mshot

    local user_request="$*";
    [ -n "$user_request" ] || {
        { ___x_cmd_is_stdout2tty && ___x_cmd_runmode_allow_chatty; } || N=coco M="Please provide user request" log:ret:64
        user_request="$(___x_cmd ted)" || return $?
    }
    eval set -- "$X_COCO_MSHOT_CMD"

    local x_
    x_=; ___x_cmd_coco___mshot_prepare_context_ "$provider" "$model" "$user_request" "$@" || return $?
    local help_document="$x_"

    local options_before_argument="
Note: The commands you generate must put OPTIONS before ARGS. Do not put ARGS before OPTIONS.
"



    x_=""; x os name_
    local OS="$x_"

    LOCALSHELL="$___X_CMD_SHELL" OS="$OS" \
    basic_cmd="$*" \
    help_document="$help_document" \
    task="$user_request" \
    options_before_argument="$options_before_argument" \
    ___x_cmd_coco___mshot_llm "$provider" "$model" --minion "$___X_CMD_ROOT_MOD/coco/lib/minion/context.yml"

}

___x_cmd_coco___mshot_prepare_context_(){
    ___x_cmd_coco___mshot_prepare_context___dfs_ "$@"
}

___x_cmd_coco___mshot_prepare_context___dfs_(){
    local provider="$1";
    local model="$2"
    local user_request="$3";    shift 3
    coco:info "Now generate the context based on -> $*"
    x_="$( ___x_cmd advise simple --dfs "$@" | ___x_cmd_cmds head -n 500 )"
}

___x_cmd_coco___mshot_llm(){
    local provider="$1";
    local model="$2";   shift 2
    set -- --type "coco-mshot" "$@"
    [ -z "$model" ] || set -- --model "$model" "$@"
    [ -z "$provider" ] || set -- --provider "$provider" "$@"
    ___x_cmd chat --send --without-repl --history 0 "$@"
}
