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

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

    ___x_cmd_gh_user_gpg _param_help_doc
    return 1
}

# Section: List
# https://docs.github.com/en/rest/users/gpg-keys#list-gpg-keys-for-the-authenticated-user
# https://docs.github.com/en/rest/users/gpg-keys#list-gpg-keys-for-a-user
___x_cmd_gh_user_gpg_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/gpg_keys"
    [ -z "$1" ] || _url="/users/${1}/gpg_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         .name  .emails.1.email .key_id  .created_at .expires_at | \
                    x csv header --add  GPG_Key_ID   Name   Email           Key_ID   Create_At   Expires_At
                else
                    x jo 2c             .id         .name  .emails.1.email .key_id  .created_at .expires_at | \
                    x csv header --add  GPG_Key_ID   Name   Email           Key_ID   Create_At   Expires_At
                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/gpg-keys#create-a-gpg-key-for-the-authenticated-user
# shellcheck disable=2034
___x_cmd_gh_user_gpg_create(){
    param:scope     ___x_cmd_github
    param:dsl       '
options:
    #1              "A GPG key in ASCII-armored format. stdin(-)"                   <>
    --name          "A descriptive name for the new key"                            <>
    --json|-j       "output origin json data"
'
    param:run
    local armored_public_key="$1"
    [ "$armored_public_key" != '-' ] || armored_public_key="$(___x_cmd_cmds_cat)"

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

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

# Section: Remove
# https://docs.github.com/en/rest/users/gpg-keys#delete-a-gpg-key-for-the-authenticated-user
# shellcheck disable=SC2154
___x_cmd_gh_user_gpg_rm(){
    param:scope     ___x_cmd_github
    param:dsl       '
options:
    #1              "The GPG key ID"                                  <>
    --yes|-y        "Ignore remove prompt interception"
'
    param:run
    [ "$yes" = "true" ] || ___x_cmd_ui_yesno "Are you sure you want to remove the GPG key $(___x_cmd_ui bold red "$1") ?" || return
    ___x_cmd_gh_curl del "/user/gpg_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 GPG key $1"
            else
                ___x_cmd_ui_tf false "Remove GPG key $1 failure:" >&2
                ___x_cmd_gh____handle_resp
                return 1
            fi
        )
}
# EndSection

# Section: GPG UI
# shellcheck disable=2154
___x_cmd_gh_user_gpg____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 GPG key information successfully"
                _err_msg="$1 GPG key information failure"
                ;;
            Creating)
                _inf_msg="$1 a GPG key successfully"
                _err_msg="$1 a GPG key failure"
                ;;
        esac
        local _id=""
        x jo env . _id=.id gh_resp_msg=.message gh_resp_err=.errors \
            key_id=.key_id name=.name emails=.emails public_key=.public_key created_at=.created_at expires_at=.expires_at revoked=.revoked raw_key=.raw_key
        if [ -n "$_id" ]; then
            ___x_cmd_ui_tf  true "${_inf_msg}:" ${_id:+"GPG Key ID: $_id"} ${name:+"Name: $name"} ${emails:+"Emails: $emails"} ${key_id:+"Key ID: $key_id"} \
                ${public_key:+"Public Key: $public_key"} ${created_at:+"Created At: $created_at"} ${expires_at:+"Expires At: $expires_at"} ${revoked:+"Revoked: $revoked"} \
                "Raw Key: |"
            x str ml wrap "$(printf "%s\n" "$raw_key")" '      '
        else
            ___x_cmd_ui_tf false "${_err_msg}:" >&2
            ___x_cmd_gh____handle_resp
            return 1
        fi
    )
}

# EndSection