# shellcheck shell=sh disable=SC2154,SC3043

___x_cmd_gt_repo_commit(){
    param:scope         ___x_cmd_gt
    param:subcmd ___x_cmd_gt_repo_commit                               \
        ls         "List repo all commit information"                  \
        info       "Show repo detail commit information"               \
        diff       "Show diff between commit information"
    param:subcmd:try
    param:run

    ___x_cmd_gt_repo_push _param_help_doc
    return 1
}

# Section: List

# https://gitee.com/api/v5/swagger#/getV5ReposOwnerRepoCommits
# TODO: since and until timer
___x_cmd_gt_repo_commit_ls(){
    param:scope     ___x_cmd_gt
    param:dsl       '
options:
    --repo|-r      "<owner_path>/<repo_path>"                                   <>:RepoPath
    #1             "base commit SHA or branch name"                             <>=""
    -p|--path      "filter by repo file path"                                   <>=""
    --author       "filter by user email or username"                           <>=""
    --page         "page"                                                       <>="1"
    --per_page     "per_page"                                                   <>="30"

    --json|-j           "output origin json data"
    --csv               "output csv data"
    --yml               "output yml data"
'
    param:run
    local sha="$1"
    [ -n "$sha" ] || unset sha
    ___x_cmd_gt_param_init_owner_repo
    if ___x_cmd_gx_output_is_format; then
        ___x_cmd_gt_get_multi "/repos/${owner_repo}/commits" sha p author | ___x_cmd_gx_output_format
    else
        ___x_cmd_gt_get_multi "/repos/${owner_repo}/commits" sha p author | \
            x jo 2c             .sha        .commit.message | \
            x csv header --add   SHA         Message        | \
            if [ -n "$csv" ]; then  ___x_cmd_cmds cat
            else                    x csv static_tab
            fi
    fi
}

# EndSection

# Section: Info

# https://gitee.com/api/v5/swagger#/getV5ReposOwnerRepoCommitsSha
# shellcheck disable=SC2034
___x_cmd_gt_repo_commit_info(){
    param:scope     ___x_cmd_gt
        param:dsl       '
    options:
        #1            "Provide commit SHA or branch name"           <>
        --repo|-r     "<owner_path>/<repo_path>"                    <>:RepoPath
        --json|-j      "output origin json data"
    '
    param:run
    [ -n "$1" ] || M='accepts 1 arg(commit SHA or branch name), received empty' arg:ret:64
    local sha="$1"
    ___x_cmd_gt_param_init_owner_repo

    ___x_cmd_gt_curl get "/repos/${owner_repo}/commits/${sha}" | \
        ___x_cmd_gt_repo_commit____ui_handler Info
}
# EndSection

# Section: diff

# https://gitee.com/api/v5/swagger#/getV5ReposOwnerRepoCompareBase...Head
# shellcheck disable=SC2034
___x_cmd_gt_repo_commit_diff(){
    param:scope     ___x_cmd_gt
        param:dsl       '
    options:
        --repo|-r     "<owner_path>/<repo_path>"                <>:RepoPath
        --base     "base   commit SHA or branch name"           <>
        --head     "target commit SHA or branch name"           <>
    '
    param:run
    ___x_cmd_gt_param_init_owner_repo
    ___x_cmd_gt_curl get "/repos/${owner_repo}/compare/${base}...${head}" | \
        ___x_cmd_gt_repo_commit____ui_handler Diff
}
# EndSection

# Section: repo commit UI
___x_cmd_gt_repo_commit____ui_handler(){
    if [ -n "$ENFORCE_JSON" ] || [ -n "$json" ]; then
        ___x_cmd_cmds_cat
        ___x_cmd_gt_http_error
        return
    fi
    (
        local _sha=""
        case "$1" in
            Info)
                x jo env . _sha=.sha url=.html_url \
                    gt_resp_err=.message gt_resp_err=.error
                _inf_msg="$owner_repo commit information"
                _err_msg="No find any repo commit data by $owner_repo"
                [ -z "$_sha" ] || {
                    ___x_cmd_ui_tf  true "${_inf_msg}:" ${_sha:+"SHA: $_sha"} ${url:+"URL: $url"}
                    return 0
                }
            ;;
            Diff)
                x jo env . _sha=.base_commit.sha url=.base_commit.html_url head_sha=.merge_base_commit.sha head_url=.merge_base_commit.html_url \
                    gt_resp_err=.message gt_resp_err=.error
                _inf_msg="$owner_repo diff information"
                _err_msg="No find any repo diff data by $owner_repo"
                [ -z "$_sha" ] || {
                    ___x_cmd_ui_tf  true "${_inf_msg}:" ${_sha:+"Base_SHA: $_sha"} ${head_sha:+"Head_SHA: $head_sha"} ${url:+"Base_URL: $url"} ${head_url:+"Head_URL: $head_url"}
                    return 0
                }
            ;;
        esac

        ___x_cmd_ui_tf false "${_err_msg}:" >&2
        ___x_cmd_gt____handle_resp
        return 1
    )
}
# EndSection
