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

___x_cmd_gh_user_ssh(){
    param:scope  ___x_cmd_github
    param:subcmd ___x_cmd_gh_user_ssh                       \
        ls          "List public SSH keys for user"         \
        create      "Create a public SSH key for user"      \
        info        "Output a public SSH key information"   \
        rm          "Remove a public SSH key for user"
    param:subcmd:try
    param:run

    ___x_cmd_gh_user_ssh _param_help_doc
    return 1
}

# Section: List
# https://docs.github.com/en/rest/users/keys#list-public-ssh-keys-for-the-authenticated-user
# https://docs.github.com/en/rest/users/keys#list-public-keys-for-a-user
___x_cmd_gh_user_ssh_ls(){
    param:scope     ___x_cmd_github
    param:dsl       '
options:
    #1              "Specify username. default is current user" <>=""
    --per_page      "Results per page"                          <>:Per_page="30"
    --page          "Page number of the results to fetch."      <>:Numbers="1"

    --json|-j       "output origin json data"
    --csv           "output csv data"
    --yml           "output yml data"
'
    param:run
    local _url="/user/keys"
    [ -z "$1" ] || _url="/users/${1}/keys"
    if ___x_cmd_gx_output_is_format; then
        ___x_cmd_gh_get_multi "$_url" | ___x_cmd_gx_output_format
    else
        ___x_cmd_gh_get_multi "$_url" | \
            {
                if [ -z "$1" ]; then
                    x jo 2c             .id         .title  .key | \
                    x csv header --add  SSH_Key_ID   Title   SSH_Key
                else
                    x jo 2c             .id         .key      | \
                    x csv header --add  SSH_Key_ID   SSH_Key
                fi
            } | \
            if [ -n "$csv" ]; then  ___x_cmd_cmds cat
            else                    x csv static_tab
            fi
    fi
}
# EndSection

# Section: Create
# https://docs.github.com/en/rest/users/keys#create-a-public-ssh-key-for-the-authenticated-user
___x_cmd_gh_user_ssh_create(){
    param:scope     ___x_cmd_github
    param:dsl       '
options:
    #1              "The public SSH key. stdin(-)"                                  <>
    --title         "A descriptive name for the new key. default is key prefix"     <>=""
    --json|-j       "output origin json data"
'
    param:run
    local key="$1"
    [ "$key" != '-' ] || key="$(___x_cmd_cmds_cat)"
    [ -n "$title" ]   || title="$(printf "%s" "$key" | command cut -b 1-20)"

    local gen_gh_json=
    gen_gh_json="$(param:option2json -json +key)"
    gh:debug "$gen_gh_json"
    ___x_cmd_gh_curl post "/user/keys" "gen_gh_json" | ___x_cmd_gh_user_ssh____ui_handler Creating
}
# EndSection

# Section: Info
# https://docs.github.com/en/rest/users/keys#get-a-public-ssh-key-for-the-authenticated-user
___x_cmd_gh_user_ssh_info(){
    param:scope     ___x_cmd_github
    param:dsl       '
options:
    #1              "The public SSH key ID"                                  <>
    --json|-j       "output origin json data"
'
    param:run
    ___x_cmd_gh_curl get "/user/keys/${1}" | ___x_cmd_gh_user_ssh____ui_handler Getting
}
# EndSection

# Section: Remove
# https://docs.github.com/en/rest/users/keys#delete-a-public-ssh-key-for-the-authenticated-user
# shellcheck disable=SC2154
___x_cmd_gh_user_ssh_rm(){
    param:scope     ___x_cmd_github
    param:dsl       '
options:
    #1              "The public SSH key ID"                                  <>
    --yes|-y        "Ignore remove prompt interception"
'
    param:run
    [ "$yes" = "true" ] || ___x_cmd_ui_yesno "Are you sure you want to remove the SSH public key $(___x_cmd_ui bold red "$1") ?" || return
    ___x_cmd_gh_curl del "/user/keys/${1}" | (
            [ -z "$___X_CMD_GH_IN_TEST" ] || { ___x_cmd_cmds_cat; return; }
            x jo env . gh_resp_msg=.message gh_resp_err=.errors
            if ___x_cmd_gh_http_error; then
                ___x_cmd_ui_tf  true "[Success]: Remove public SSH key $1"
            else
                ___x_cmd_ui_tf false "Remove public SSH key $1 failure:" >&2
                ___x_cmd_gh____handle_resp
                return 1
            fi
        )
}
# EndSection

# Section: SSH UI
___x_cmd_gh_user_ssh____ui_handler(){
    if  [ -n "$ENFORCE_JSON" ] || [ -n "$json" ]; then
        ___x_cmd_cmds_cat
        ___x_cmd_gh_http_error
        return
    fi
    (
        case "$1" in
            Getting)
                _inf_msg="$1 public SSH key information successfully"
                _err_msg="$1 public SSH key information failure"
                ;;
            Creating)
                _inf_msg="$1 a public SSH key successfully"
                _err_msg="$1 a public SSH key failure"
                ;;
        esac
        local _id=""
        x jo env . _id=.id gh_resp_msg=.message gh_resp_err=.errors \
            title=.title verified=.verified read_only=.read_only created_at=.created_at key=.key
        if [ -n "$_id" ]; then
            ___x_cmd_ui_tf  true "${_inf_msg}:" ${_id:+"ID: $_id"} ${title:+"Title: $title"} ${verified:+"Verified: $verified"} \
                ${read_only:+"Read Only: $read_only"} ${created_at:+"Created At: $created_at"} ${key:+"Key: $key"}
        else
            ___x_cmd_ui_tf false "${_err_msg}:" >&2
            ___x_cmd_gh____handle_resp
            return 1
        fi
    )
}

# EndSection