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

___x_cmd_gt_repo_tag(){
    param:subcmd ___x_cmd_gt_repo_tag                   \
        ls              "List repo tag"                 \
        create          "Create  repo tag"
    param:subcmd:try
    param:run

    ___x_cmd_gt_repo_tag _param_help_doc
    return 1
}

# shellcheck disable=SC2154,SC2086,SC2034
# https://gitee.com/api/v5/swagger#/getV5ReposOwnerRepoTags
___x_cmd_gt_repo_tag_ls(){
    param:scope     ___x_cmd_gt
    param:dsl    '
options:
    --repo|-r       "<owner_path>/<repo_path>"   <>:RepoPath

    --json|-j       "output origin json data"
    --csv           "output csv data"
    --yml           "output yml data"
'
    param:run
    ___x_cmd_gt_param_init_owner_repo
    if ___x_cmd_gx_output_is_format; then
        ___x_cmd_gt_curl get "/repos/${owner_repo}/tags" | ___x_cmd_gx_output_format
    else
        ___x_cmd_gt_curl get "/repos/${owner_repo}/tags" | \
            x jo 2c           .name    .message .commit.sha .commit.date | \
            x csv header --add TagName  Message  Commit      Time        | \
            if [ -n "$csv" ]; then  ___x_cmd_cmds cat
            else                    x csv static_tab
            fi
    fi
}


# shellcheck disable=SC2154,SC2181,SC2034
# https://gitee.com/api/v5/swagger#/postV5ReposOwnerRepoTags
___x_cmd_gt_repo_tag_create(){
    param:scope     ___x_cmd_gt
    param:dsl    '
options:
    #1              "tag name"                                          <>
    --repo|-r       "<owner_path>/<repo_path>"                          <>:RepoPath
    --refs          "starting point branch name"                        <>="master"
    --message       "tag message"                                       <>=""
    --json|-j       "output origin json data"
'
    param:run
    [ -n "$1" ] || M='accepts 1 arg(tag name), received empty' arg:ret:64
    local tag_name="$1"

    ___x_cmd_gt_param_init_owner_repo
    local gen_gt_json=""
    gen_gt_json="$(param:option2json +tag_name tag_message=message -message)"
    gt:debug "$gen_gt_json"

    ___x_cmd_gt_curl post "/repos/${owner_repo}/tags" "gen_gt_json" |  ___x_cmd_gt_repo_tag____ui_handler Create
}

___x_cmd_gt_repo_tag____ui_handler(){
    if [ -n "$json" ] || [ -n "$ENFORCE_JSON" ]; then
        ___x_cmd_cmds_cat
        ___x_cmd_gt_http_error
        return
    fi
    (
        case "$1" in
            Create)
                _inf_msg="$1 $owner_repo tag $tag_name successfully"
                _err_msg="$1 $owner_repo tag $tag_name failure"
            ;;
        esac

        local commit_sha=""
        x jo env . commit_sha=.commit.sha gt_resp_err=.message gt_resp_err=.error
        if [ -n "$commit_sha" ]; then
            ___x_cmd_ui_tf  true "${_inf_msg}:" "commit_sha: $commit_sha"
        else
            ___x_cmd_ui_tf false "${_err_msg}:" >&2
            ___x_cmd_gt____handle_resp
            return 1
        fi
    )
}
