# shellcheck shell=dash
xrc ourl

___x_cmd_openrouter_model(){
    [ $# -gt 0 ]    ||      set - ls

    local op="$1";  shift
    case "$op" in
        ls|set|get)
            ___x_cmd_openrouter_model_"$op" "$@"
            ;;
        -h|--help)
            ___x_cmd help -m openrouter model "$@"
            ;;
    esac
}

___x_cmd_openrouter_model_set(){
    local model="$1"
    [ -n "$model" ] || openrouter:error "Please provide a model name"

    ___x_cmd_openrouter_cfg set model="$model" || return $?
    openrouter:info "The default model has been set as $model"
}

___x_cmd_openrouter_model_get(){
    local model=""
    ___x_cmd_openrouter_cur model:=  2>/dev/null
    printf "%s\n" "$model"
}

___x_cmd_openrouter_model_ls(){
    [ $# -gt 0 ]    ||      set - --app
    local op="$1"
    local format=
    case "$op" in
        --csv)      format=csv       ;;
        --app)      format=app       ;;
        -j|--json)  format=json      ;;
        -y|--yml)   format=yml       ;;
        -h|--help)  ___x_cmd help -m openrouter model ls  "$@"; return ;;
        *)          M="Not support $op" N=openrouter log:ret:1 ;;
    esac

    "___x_cmd_openrouter_model_ls___${format}"
}

___x_cmd_openrouter_model_ls___app(){
    if ! { ___x_cmd_is_stdout2tty && ___x_cmd_runmode_allow_manual; }; then
        ___x_cmd_openrouter_model_ls___csv "$@"
        return $?
    fi

    local ___X_CMD_CSV_APP_DATA_CURROW=
    local data; data="$( ___x_cmd_openrouter_model_ls___csv )" || ourl:data:ret
    ___x_cmd_pipevar    data    ___x_cmd csv --app --return line

    if [ -n "$___X_CMD_CSV_APP_DATA_CURROW" ] ; then
        local select_id="${___X_CMD_CSV_APP_DATA_CURROW%%,*}"
        local id=""
        openrouter:info "Select model -> $select_id"
        ___x_cmd ui select id "What to do NEXT" \
        "set default model: $select_id"    \
        "EXIT"     || return $?

        case "$id" in
            1)  ___x_cmd_openrouter_model_set "$select_id" ;;
            2)
                openrouter:info "EXIT [code=0]"
                return 0 ;;
        esac
    fi
}

___x_cmd_openrouter_model_ls___csv(){
    local data; data="$( ___x_cmd_openrouter_model_ls_raw )" || ourl:data:ret
    ___x_cmd_pipevar    data    ___x_cmd_openrouter_model_ls___parse_json2csv
}
___x_cmd_openrouter_model_ls___json(){
    local data; data="$( ___x_cmd_openrouter_model_ls_raw )" || ourl:data:ret
    ___x_cmd_pipevar    data    ___x_cmd jo fmt
}
___x_cmd_openrouter_model_ls___yml(){
    local data; data="$( ___x_cmd_openrouter_model_ls_raw )" || ourl:data:ret
    ___x_cmd_pipevar    data    ___x_cmd j2y
}

___x_cmd_openrouter_model_ls___parse_json2csv(){
    ___x_cmd jo .data | ___x_cmd ja 2c .id .name .description .context_length .architecture.modality .architecture.input_modalities .architecture.output_modalities .architecture.tokenizer \
        .pricing.prompt .pricing.completion .pricing.request .pricing.image .pricing.audio .pricing.web_search .pricing.internal_reasoning \
        .top_provider.context_length .top_provider.max_completion_tokens .top_provider.is_moderated .supported_parameters | \
        ___x_cmd csv header --add  Id Name Description ContextLength Modality InputModalities OutputModalities Tokenizer \
        PricingPrompt PricingCompletion PricingRequest PricingImage PricingAudio PricingWebSearch PricingInternalReasoning \
        ContextLength MaxCompletionTokens IsModerated SupportedParameters
}

___x_cmd_openrouter_model_ls_raw(){
    ___x_cmd ccmd 1d -- ___x_cmd_openrouter_model_ls_raw___inner
}

___x_cmd_openrouter_model_ls_raw___inner(){
    local data=; data="$(
        ___x_cmd curl -sS  "$___X_CMD_OPENROUTER_DEFAULT_ENDPOINT/v1/models"
    )" || {
        openrouter:error -m "$data"
        return 1
    }

    printf "%s\n" "$data" | ___x_cmd jo fmt
}
