# shellcheck shell=sh
# shellcheck disable=SC2039,3043
___x_cmd_gl_repo_branch(){
    param:subcmd ___x_cmd_gl_repo_branch                \
        ls              "List repo branch"              \
        info            "Get repo branch info"          \
        rm              "Delete repo branch"            \
        default         "Set default branch"            \
        protection      "protection branch management"
    param:subcmd:try
    param:run

    ___x_cmd_gl_repo_branch _param_help_doc
    return 1
}

# Section: List
# https://docs.gitlab.com/ee/api/branches.html#list-repository-branches
___x_cmd_gl_repo_branch_ls(){
    param:scope     ___x_cmd_gl
    param:dsl       '
options:
    --repo|-r       "<owner_path>/<repo_path> or .id=<repo_id>"                         <>
    --search        "Return list of branches containing the search string"              <>=""

    --per_page      "Results per page"                                                  <>=30
    --page          "Page number of the results to fetch"                               <>=1
    --json|-j       "output raw JSON data"
'
    param:run

    ___x_cmd_gl_param_init_owner_repo
    if [ -n "$json" ] || [ -n "$ENFORCE_JSON" ]; then
        ___x_cmd_gl_get_multi "/projects/$owner_repo/repository/branches" search
    else
        ___x_cmd_gl_get_multi "/projects/$owner_repo/repository/branches" search | \
            x jo 2c             .name .protected .default .commit.short_id | \
            x csv header --add   Name  Protected  Default  LastCommit      | \
            x csv static_tab
    fi
}
# EndSection

# Section: Info
# https://docs.gitlab.com/ee/api/branches.html#get-single-repository-branch
___x_cmd_gl_repo_branch_info(){
    param:scope     ___x_cmd_gl
    param:dsl       '
options:
    #1              "branch name"                                       <>:Branch
    --repo|-r       "<owner_path>/<repo_path> or .id=<repo_id>"         <>
    --json|-j       "output raw JSON data"
'
    param:run

    ___x_cmd_gl_param_init_owner_repo
    ___x_cmd_gl_get_multi "/projects/${owner_repo}/repository/branches/$1" | _____x_cmd_gl_branch_ui_utils Info
}
# EndSection

# Section: Remove
# shellcheck disable=2154
___x_cmd_gl_repo_branch_rm(){
    param:scope     ___x_cmd_gl
    param:dsl       '
options:
    #1              "branch names"                                                              <>=""
    --repo|-r       "<owner_path>/<repo_path> or .id=<repo_id>"                                 <>
    --merged        "Delete all branches that are merged into the repo default branch"
'
    param:run
    ___x_cmd_gl_param_init_owner_repo
    local branch
    if [ -n "$merged" ]; then
        branch="all merged"
        [ "$yes" = "true" ] || ___x_cmd_ui_yesno "Are you sure you want to delete ${repo} repo $(___x_cmd_ui bold red "${branch}") branches ?" || return
        ___x_cmd_gl_curl del "/projects/${owner_repo}/repository/merged_branches" | ___x_cmd_gl_repo_branch_rm_status_handler
    else
        [ -n "$1" ] || { gl:error "Please provide at least one branch name"; return 1; }
        for branch in "$@"; do
            [ "$yes" = "true" ] || ___x_cmd_ui_yesno "Are you sure you want to delete ${repo} repo $(___x_cmd_ui bold red "${repo} ${branch}") branch ?" || continue
             ___x_cmd_gl_curl del "/projects/${owner_repo}/repository/branches/${branch}" | ___x_cmd_gl_repo_branch_rm_status_handler
        done
    fi
}

___x_cmd_gl_repo_branch_rm_status_handler(){
    (
        x jo env . gl_resp_err=.error gl_resp_msg=.message
        if ___x_cmd_gl_http_error; then
            ___x_cmd_ui_tf  true "[Success]: Delete ${repo} $branch branch"
        else
            ___x_cmd_ui_tf false "Delete ${repo} $branch branch failure:"
            ___x_cmd_gl____handle_resp
            return 1
        fi
    )
}
# EndSection

# Section: Defult
___x_cmd_gl_repo_branch_default(){
    param:dsl       '
options:
    #1          "branch name"                                       <>
    --repo|-r   "<owner_path>/<repo_path> or .id=<repo_id>"         <>:RepoName
    --json|-j   "output raw JSON data"
'
    param:run

    local default_branch="$1"
    local gen_gl_json
    gen_gl_json="$(param:option2json -repo +default_branch)"
    gl:debug "$gen_gl_json"

    ___x_cmd_gl_param_init_owner_repo
    ___x_cmd_gl_curl put "/projects/$owner_repo" gen_gl_json | _____x_cmd_gl_branch_ui_utils Default
}
# EndSection

# Section: branch utils
# shellcheck disable=2154
_____x_cmd_gl_branch_ui_utils(){
    if [ -n "$ENFORCE_JSON" ] || [ -n "$json" ]; then
        ___x_cmd_cmds_cat
        ___x_cmd_gl_http_error
        return
    fi

    (
        local _name=""
        case "$1" in
            Info)
                x jo env . _name=.name gl_resp_err=.error gl_resp_msg=.message \
                    _protected=.protected  sha=.commit.short_id merged=.merged url=.web_url
                _inf_msg="Getting repo $repo branch info successfully"
                _err_msg="Getting repo $repo branch info failure"
                ;;
            Default)
                x jo env . _name=.name gl_resp_err=.error gl_resp_msg=.message \
                    url=.web_url
                _inf_msg="Setting repo default branch successfully"
                _err_msg="Setting repo default branch failure"
                ;;
        esac
        if [ -n "$_name" ]; then
            ___x_cmd_ui_tf  true "${_inf_msg}:" "Name: $_name" ${Protected:+"Protected: $Protected"} ${merged:+"Merged: $merged"} \
                ${sha:+"LastCommit: $sha"} ${url:+"Url: $url"}
        else
            ___x_cmd_ui_tf false "${_err_msg}:"
            ___x_cmd_gl____handle_resp
            return 1
        fi
    )
}
# EndSection