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

    ___x_cmd_gl_repo_variable _param_help_doc

    return 1
}
# Section: variable ls
# https://docs.gitlab.com/ee/api/project_level_variables.html#list-project-variables
___x_cmd_gl_repo_variable_ls(){
    param:scope     ___x_cmd_gl
    param:dsl       '
options:
    --repo|-r       "<owner_path>/<repo_path> or .id=<repo_id>"                             <>
    --per_page      "Results per page"                                                      <>=""
    --page          "Page number of the results to fetch."                                  <>=""
    --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/variables"
    else
        ___x_cmd_gl_get_multi "/projects/$owner_repo/variables" | \
            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
#TODO: filter curl --globoff
# Section: info
# https://docs.gitlab.com/ee/api/project_level_variables.html#get-a-single-variable
___x_cmd_gl_repo_variable_info(){
    param:scope     ___x_cmd_gl
    param:dsl       '
options:
    #1          "The key of a variable"                                                         <>
    --repo|-r   "<owner_path>/<repo_path> or .id=<repo_id>"                                     <>
    --filter    "Available filters: [environment_scope]."                                       <>:hash=""
    --json|-j   "output raw JSON data"
'
    param:run
    ___x_cmd_gl_param_init_owner_repo
    ___x_cmd_gl_curl get "/projects/$owner_repo/variables/$1" | _____x_cmd_gl_repo_variable_ui_utils Info
}
# EndSection

# Section: create && edit
# https://docs.gitlab.com/ee/api/project_level_variables.html#create-a-variable
___x_cmd_gl_repo_variable_create(){
    param:scope     ___x_cmd_gl
    param:dsl       '
options:
    --repo|-r             "<owner_path>/<repo_path> or .id=<repo_id>"           <>
    --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

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

    ___x_cmd_gl_curl post "/projects/$owner_repo/variables" "gen_gl_json" | _____x_cmd_gl_repo_variable_ui_utils Create
}

# https://docs.gitlab.com/ee/api/project_level_variables.html#update-a-variable
___x_cmd_gl_repo_variable_edit(){
    param:scope     ___x_cmd_gl
    param:dsl       '
options:
    #1                    "The key of a variable"                               <>
    --repo|-r             "<owner_path>/<repo_path> or .id=<repo_id>"           <>
    --value               "The value of a variable."                            <>
    --variable_type       "The type of a variable. "                            <>=""    = 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."              <>=""
    --filter              "Available filters: [environment_scope]."             <>:hash=""
    --json|-j             "output raw JSON data"
'
    param:run

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

    ___x_cmd_gl_curl put "/projects/$owner_repo/variables/$1" "gen_gl_json" | _____x_cmd_gl_repo_variable_ui_utils Edit
}
# EndSection

# Section: rm
# https://docs.gitlab.com/ee/api/project_level_variables.html#delete-a-variable
___x_cmd_gl_repo_variable_rm(){
    param:scope     ___x_cmd_gl
    param:dsl       '
options:
    #1          "The key of a variable"                                                         <>
    --repo|-r   "<owner_path>/<repo_path> or .id=<repo_id>"                                     <>
    --filter    "Available filters: [environment_scope]."                                       <>:hash=""

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

    ___x_cmd_gl_param_init_owner_repo
    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 "/projects/$owner_repo/variables/$key_name" | (
            x jo env .  gl_resp_err=.error gl_resp_msg=.message
            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:"
                ___x_cmd_gl____handle_resp
                return 1
            fi
        )
    done
}
# EndSection

# Section: repo variable utils
_____x_cmd_gl_repo_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_err=.message \
            key=.key value=.value variable_type=.variable_type environment_scope=.environment_scope
        case "$1" in
            Info)
                _inf_msg="Getting repo variable information successfully"
                _err_msg="Getting repo variable information failure"
                ;;
            Create|Edit)
                _inf_msg="$1 repo variable successfully"
                _err_msg="$1 repo 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