# shellcheck shell=sh disable=SC3043

# ___x_cmd_llmf_model_download llava/v1.5-7b/q4_k.gguf
___x_cmd_llmf_model_download(){
    local model="$1"
    local x_=; ! ___x_cmd_llmf_model_which_ "$model" || {
        llmf:info --cache "$x_" "The [model=$model] is exist"
        return 0
    }

    if [ -z "$model" ]; then
        ___x_cmd_runmode_allow_chatty || N=llmf M="Please provide model <name/version/quant.format>" log:ret:64

        x_=""; ___x_cmd_llmf_model_ls___app_ --all || return $?
        model="$x_"
        [ -n "$model" ] || return $?

        llmf:info "Planning execution => \`x llmf model download $model\`"
        ___x_cmd ui yesno "Do you want to download [model=$model] ?" || return $?
        ___x_cmd_llmf_model_download "$model"
        return
    fi

    local tmpfile="$___X_CMD_ROOT_TMP/llmf/model/$model"
    local cachefile="$___X_CMD_LLMF_MODEL_DATA_CACHE/$model"

    x_=; ___x_cmd_llmf___model2hf_ "$model" || return $?
    llmf:info --hf "$x_" --cache "$cachefile" "Downloading [model=$model]"
    ___x_cmd ensurefp "$tmpfile"
    ___x_cmd huggingface --publicdownload "$x_" "$tmpfile" || N=llmf M="Fail to download [model=$model]" log:ret:1

    ___x_cmd ensurefp "$cachefile"
    ___x_cmd mv "$tmpfile" "$cachefile"

    ___x_cmd shortcut enable --by category llmf
}
