# shellcheck shell=sh
# shellcheck disable=SC2039,3043,SC2034
# https://docs.gitlab.com/ee/api/group_level_variables.html
___x_cmd_gl_group_variable(){
    param:subcmd ___x_cmd_gl_group_variable        \
        ls          "List groups variable"         \
        info        "Show groups variable info"    \
        create      "Create groups variable"       \
        "edit|ed"   "Update groups variable"       \
        rm          "Remove groups variable"
    param:subcmd:try
    param:run

    ___x_cmd_gl_group_variable _param_help_doc
    return 1
}
# Section: variable ls
# https://docs.gitlab.com/ee/api/group_level_variables.html#list-group-variables
___x_cmd_gl_group_variable_ls(){
    param:scope     ___x_cmd_gl
    param:dsl       '
options:
    --group|-g      "<group_path> or .id=<group_id>"                                                          <>
    --per_page      "Results per page"                                                                        <>=""
    --page          "Page number of the results to fetch."                                                    <>=""
    --json|-j       "output raw JSON data"
'
    param:run
    [ -n "$group" ] || M='accepts option --group (<group_path> or .id=<group_id>), received empty' arg:ret:64
    local _avt_group="$group"
    ___x_cmd_gl____transform_avt_group || return
     if [ -n "$json" ] || [ -n "$ENFORCE_JSON" ]; then
        ___x_cmd_gl_get_multi "/groups/$_avt_group/variables" query user_ids
    else
        ___x_cmd_gl_get_multi "/groups/$_avt_group/variables" query user_ids | \
            x jo 2c             .key  .value .variable_type .environment_scope | \
            x csv header --add   Key   Value  Variable_Type  Environment_Scope | \
            x csv static_tab
    fi
}
# EndSection

# Section: info
# https://docs.gitlab.com/ee/api/group_level_variables.html#show-variable-details
___x_cmd_gl_group_variable_info(){
    param:scope     ___x_cmd_gl
    param:dsl       '
options:
    #1          "The key of a variable"                                                                     <>
    --groups    "The ID of a group or URL-encoded path of the group owned by the authenticated user"        <>
    --json|-j   "output raw JSON data"
'
    param:run

    local gen_gl_json
    gen_gl_json="$(param:option2json -groups)"
    gl:debug "$gen_gl_json"

    ___x_cmd_gl_curl get "/groups/$groups/variables/$1" | _____x_cmd_gl_group_variable_ui_utils Info
}
# EndSection

# Section: create && edit
# https://docs.gitlab.com/ee/api/group_level_variables.html#create-variable
___x_cmd_gl_group_variable_create(){
    param:scope     ___x_cmd_gl
    param:dsl       '
options:
    --groups              "The ID of a group or URL-encoded path of the group owned by the authenticated user"      <>
    --key                 "The key of a variable."                                                                  <>
    --value               "The value of a variable."                                                                <>
    --variable_type       "The type of the variable."                                                               <>="env_var"     = env_var file
    --protected           "Whether the variable is protected."                                                      <>:bool="false"  = true false
    --masked              "Whether the variable is masked."                                                         <>:bool="false"  = true false
    --environment_scope   "The environment scope of the variable."                                                  <>
    --json|-j             "output raw JSON data"
'
    param:run

    local gen_gl_json
    gen_gl_json="$(param:option2json -groups)"
    gl:debug "$gen_gl_json"

    ___x_cmd_gl_curl post "/groups/$groups/variables" "gen_gl_json" | _____x_cmd_gl_group_variable_ui_utils Create
}

# https://docs.gitlab.com/ee/api/group_level_variables.html#update-variable
___x_cmd_gl_group_variable_edit(){
    param:scope     ___x_cmd_gl
    param:dsl       '
options:
    #1                    "The key of a variable"                                                                   <>
    --groups              "The ID of a group or URL-encoded path of the group owned by the authenticated user"      <>
    --value               "The value of a variable."                                                                <>
    --variable_type       "The type of a variable."                                                                 <>="env_var"    = env_var file
    --protected           "Whether the variable is protected."                                                      <>:bool="false" = true false
    --masked              "Whether the variable is masked."                                                         <>:bool="false" = true false
    --environment_scope   "The environment scope of the variable."                                                  <>
    --raw                 "Whether the variable is expandable"                                                      <>:bool="false" = true false
    --json|-j             "output raw JSON data"
'
    param:run

    local gen_gl_json
    gen_gl_json="$(param:option2json -groups)"
    gl:debug "$gen_gl_json"

    ___x_cmd_gl_curl put "/groups/$groups/variables/$1" "gen_gl_json" | _____x_cmd_gl_group_variable_ui_utils Edit
}
# EndSection

# Section: rm
# https://docs.gitlab.com/ee/api/group_level_variables.html#remove-variable
___x_cmd_gl_group_variable_rm(){
    param:scope     ___x_cmd_gl
    param:dsl       '
options:
    #1          "The key of a variable"                                                                   <>
    --groups    "The ID of a group or URL-encoded path of the group owned by the authenticated user"      <>

    --yes|-y    "Ignore remove prompt interception"
'
    param:run

    local key_name
    for key_name in "$@"; do
        [ "$yes" = "true" ] || ___x_cmd_ui_yesno "Are you sure you want to delete variable : $(___x_cmd_ui bold red "$key_name") ?" || continue
        ___x_cmd_gl_curl del "/groups/$groups/variables/$key_name" | (
            x jo env . error=.
            if  ___x_cmd_gl_http_error; then
                ___x_cmd_ui_tf true "[Success]: Remove variable $key_name"
            else
                ___x_cmd_ui_tf false "Remove variable $key_name failure"
                return 1
            fi
        )
    done
}
# EndSection

# Section: groups variable utils
_____x_cmd_gl_group_variable_ui_utils(){

    if [ -n "$ENFORCE_JSON" ] || [ -n "$json" ]; then
        ___x_cmd_cmds_cat
        ___x_cmd_gl_http_error
        return
    fi
    (
        local _key=""
        x jo env . _key=.key gl_resp_err=.error gl_resp_msg=.message \
            value=.value variable_type=.variable_type environment_scope=.environment_scope
        case "$1" in
            Info)
                _inf_msg="Getting groups variable information successfully"
                _err_msg="Getting groups variable information failure"
                ;;
            Create|Edit)
                _inf_msg="$1 groups variable successfully"
                _err_msg="$1 groups variable failure"
                ;;
        esac
        if [ -n "$Key" ]; then
            ___x_cmd_ui_tf  true "${_inf_msg}:" ${Key+"Key: $Key"} ${value+"Value: $value"} ${variable_type+"Variable_type: $variable_type"} \
                ${environment_scope+"Environment_scope: $environment_scope"}
        else
            ___x_cmd_ui_tf false "${_err_msg}:"
            ___x_cmd_gl____handle_resp
            return 1
        fi
    )
}
# EndSection