# shellcheck shell=sh
# shellcheck disable=SC2039,3043

___x_cmd_gcode_user(){
    param:scope     ___x_cmd_gcode
    param:subcmd ___x_cmd_gcode_user            \
        info        "Show information"          \
        email       "get current user email"
    param:subcmd:try
    param:run

    ___x_cmd_gcode_user _param_help_doc
    return 1
}

# Section: Info
# shellcheck disable=SC2154
# http://localhost:4000/13.7/ee/api/users.html#single-user
# http://localhost:4000/13.7/ee/api/users.html#for-normal-users
___x_cmd_gcode_user_info(){
    param:scope     ___x_cmd_gcode
    param:dsl       '
options:
    #1              "<user_login_path> or .id=<user_id>"            <>=""
    --json|-j       "output json data"
'
    param:run

    local _url=""
    case $1 in
        "")
            _url="/user"
            ;;
        *)
            local _avt_name="${1}"
            ___x_cmd_gcode____transform_avt_name || return
            _url="/users/$_avt_name"
            ;;
    esac

    ___x_cmd_gcode_curl get "$_url" | ___x_cmd_gcode_user_info_json_status_handler
}

# shellcheck disable=SC2154
___x_cmd_gcode_user_info_json_status_handler(){
    if [ -n "$ENFORCE_JSON" ] || [ -n "$json" ]; then
        ___x_cmd_cmds_cat
        return
    fi
    (
        local _id=""
        x jo env . _id=.id name=.username email=.email web_url=.web_url state=.state gcode_resp_err=.error gcode_resp_msg=.message
        if [ -n "$_id" ]; then
            ___x_cmd_ui_tf  true "Getting user info successfully:" "ID: $_id" "Name: $name" "Email: $email" "State: $state" "Url: $web_url"
        else
            ___x_cmd_ui_tf false "Getting user info failure:"
            ___x_cmd_gcode____handle_resp
            return 1
        fi
    )
}
# EndSection

# Section: Email
# http://localhost:4000/13.7/ee/api/users.html#list-emails
___x_cmd_gcode_user_email(){
    param:scope     ___x_cmd_gcode
    param:dsl       '
options:
    --json|-j       "output json data"
'
    param:run
    if [ -n "$json" ] || [ -n "$ENFORCE_JSON" ]; then
        ___x_cmd_gcode_curl get "/user/emails"
    else
        ___x_cmd_gcode_curl get "/user/emails" | \
            x jo 2c             .id .email .confirmed_at | \
            x csv header --add   ID  Email  Confirmed_at | \
            x csv static_tab
    fi
}
# EndSection
