# shellcheck shell=sh disable=SC2039,3043,2154
___x_cmd_gl_repo_release_attachment(){
    param:subcmd ___x_cmd_gl_repo_release_attachment                        \
        ls              "List repo release attachment links"                \
        info            "Show detailed information of release attachment"   \
        create          "Create repo release link"                          \
        "edit|ed"       "Edit repo release attachment link information"     \
        rm              "Remove repo release attachment link"               \
        upload          "Upload file as asset and create release asset link"
    param:subcmd:try
    param:run

    ___x_cmd_gl_repo_release_attachment _param_help_doc
    return 1
}

# Section: List
# https://docs.gitlab.com/ee/api/releases/links.html#get-links
___x_cmd_gl_repo_release_attachment_ls(){
    param:scope     ___x_cmd_gl
    param:dsl    '
options:
    --tag_name      "The Git tag the release is associated with"                        <>
    --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
    if [ -n "$json" ] || [ -n "$ENFORCE_JSON" ]; then
        ___x_cmd_gl_curl get "/projects/${owner_repo}/releases/${tag_name}/assets/links"
    else
        ___x_cmd_gl_curl get "/projects/${owner_repo}/releases/${tag_name}/assets/links" | \
            x jo 2c             .id .name .link_type .url | \
            x csv header --add   ID  Name  LinkType  Url | \
            x csv static_tab
    fi
}
# EndSection

# Section: Info
# https://docs.gitlab.com/ee/api/releases/links.html#get-a-link
___x_cmd_gl_repo_release_attachment_info(){
    param:scope     ___x_cmd_gl
    param:dsl    '
options:
    #1             "The ID of the link"                                                               <>
    --tag_name     "The Git tag the release is associated with"                                       <>
    --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_curl get "/projects/${owner_repo}/releases/${tag_name}/assets/links/$1" | ______x_cmd_gl_repo_release_attachment_ui_utils Info
}
# EndSection

# Section: Create
# https://docs.gitlab.com/ee/api/releases/links.html#create-a-link
___x_cmd_gl_repo_release_attachment_create(){
    param:scope     ___x_cmd_gl
    param:dsl    '
options:
    --tag_name      "The Git tag the release is associated with"                                        <>
    --repo|-r       "<owner_path>/<repo_path> or .id=<repo_id>"                                         <>
    --name          "The name of the link. Link names must be unique in the release"                    <>
    --url           "The URL of the link"                                                               <>

    --filepath      "Optional path for a direct asset link"                                             <>=""
    --link_type     "The type of the link"                                                              <>="other"   = other runbook image package
    --json|-j       "output raw JSON data"
'
    param:run
    local gen_gl_json
    gen_gl_json="$(param:option2json -repo -tag_name)"
    gl:debug "$gen_gl_json"
    ___x_cmd_gl_param_init_owner_repo
    ___x_cmd_gl_curl post "/projects/${owner_repo}/releases/$tag_name/assets/links" "gen_gl_json" | \
        ______x_cmd_gl_repo_release_attachment_ui_utils Creating
}
# EndSection

# Section: Edit
# https://docs.gitlab.com/ee/api/releases/links.html#update-a-link
___x_cmd_gl_repo_release_attachment_edit(){
    param:scope     ___x_cmd_gl
    param:dsl    '
options:
    #1              "The ID of the link"                                                                <>
    --tag_name      "The Git tag the release is associated with"                                        <>
    --repo|-r       "<owner_path>/<repo_path> or .id=<repo_id>"                                         <>

    --name          "The name of the link. Link names must be unique in the release"                    <>=""
    --url           "The URL of the link"                                                               <>=""
    --link_type     "The type of the link"                                                              <>=""  =  "" other runbook image package
    --json|-j       "output raw JSON data"
'
    param:run
    local gen_gl_json
    gen_gl_json="$(param:option2json -repo -tag_name)"
    gl:debug "$gen_gl_json"
    ___x_cmd_gl_param_init_owner_repo
    ___x_cmd_gl_curl put "/projects/${owner_repo}/releases/$tag_name/assets/links/$1" "gen_gl_json" | \
        ______x_cmd_gl_repo_release_attachment_ui_utils Edit
}
# EndSection

# Section: Remove
# https://docs.gitlab.com/ee/api/releases/links.html#delete-a-link
___x_cmd_gl_repo_release_attachment_rm(){
    param:scope  ___x_cmd_gl
    param:dsl    '
options:
    #1              "The ID of the link"                                                                <>
    --repo|-r       "<owner_path>/<repo_path> or .id=<repo_id>"                                         <>
    --tag_name      "The Git tag the release is associated with"                                        <>
    --yes|-y        "Ignore remove prompt interception"
'
    param:run

    ___x_cmd_gl_param_init_owner_repo
    local _link_id
    for _link_id in "$@"; do
        [ "$yes" = "true" ] || ___x_cmd_ui_yesno "Are you sure you want to remove link $(___x_cmd_ui bold red "${_link_id}") on the $repo $tag_name ?" || return
        ___x_cmd_gl_curl del "/projects/${owner_repo}/releases/${tag_name}/assets/links/${_link_id}" | (
            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 repo release link ${_link_id}"
            else
                ___x_cmd_ui_tf false "Remove repo release link ${_link_id} on $owner_repo $tag_name failure:"
                ___x_cmd_gl____handle_resp
                return 1
            fi
        )
    done

}
# EndSection

# Section: Upload
# https://docs.gitlab.com/ee/api/projects.html#upload-a-file
# https://docs.gitlab.com/ee/api/releases/links.html#create-a-link
___x_cmd_gl_repo_release_attachment_upload(){
    param:scope  ___x_cmd_gl
    param:dsl    '
options:
    #1              "Target upload attachment file path"                                                <>
    --repo|-r       "<owner_path>/<repo_path> or .id=<repo_id>"                                         <>
    --tag_name      "The Git tag the release is associated with"                                        <>
    --name          "The name of the link. (default: filename)"                                         <>=""
    --filepath      "Optional path for a direct asset link. (default: /filename)"                       <>=""
    --link_type     "The type of the link."                                                             <>="other"   = other runbook image package
'
    param:run

    ___x_cmd_gl_param_init_owner_repo
    local _file_path="${1?:"Please provide target upload attachment file path"}"
    [ "$_file_path" != '-' ] || _file_path="$(___x_cmd_cmds_cat)"

    gl:info "Uploading file $_file_path ..."
    ___x_cmd_gl_curl upload "/projects/${owner_repo}/uploads" "$_file_path" | (
        x jo env . gl_resp_err=.error gl_resp_err=.message full_path=.full_path
        if [ -n "$full_path" ]; then
            local _current_domain=""; _current_domain="$(___x_cmd_gl___get_web_endpoint)";
            local _target_url="${_current_domain:-"https://gitlab.com"}${full_path}"
            [ -n "$name" ] || name="${_target_url##*/}"
            [ -n "$filepath" ] || filepath="/${_target_url##*/}"

            gl:info "Creating release attachment link. Please wait a moment..."
            if ___x_cmd_gl_repo_release_attachment_create --repo "${repo}" --tag_name "$tag_name" --name "$name" \
                --url "$_target_url" --filepath "$filepath" --link_type "$link_type" 1>/dev/null; then
                ___x_cmd_ui_tf  true "[Success]: Upload repo attachment"
            else
                ___x_cmd_ui_tf false "[Fail]: Creating $_target_url release attachment link failure.$___X_CMD_UNSEENCHAR_NEWLINE            repo ==> $repo. tag_name ==> $tag_name. path ==> $_file_path"
                return 1
            fi
        else
            ___x_cmd_ui_tf false "Upload $_file_path release attachment to $repo repo $tag_name failure:"
            ___x_cmd_gl____handle_resp
            return 1
        fi
    )
}
# EndSection

# Section: release asset UI utils
______x_cmd_gl_repo_release_attachment_ui_utils(){
    if [ -n "$ENFORCE_JSON" ] || [ -n "$json" ]; then
        ___x_cmd_cmds_cat
        ___x_cmd_gl_http_error
        return
    fi
    (
        case "$1" in
            Creating|Edit)
                _inf_msg="$1 repo release link successfully"
                _err_msg="$1 repo release link failure"
                ;;
            Info)
                _inf_msg="Getting repo release link info successfully"
                _err_msg="Getting repo release link info failure"
                ;;
        esac
        local _id=""
        x jo env . _id=.id gl_resp_err=.error gl_resp_msg=.message \
            name=.name external=.external url=.url link_type=.link_type
        if [ -n "$_id" ]; then
            ___x_cmd_ui_tf  true "${_inf_msg}:" ${_id:+"Link_ID: $_id"} ${external:+"External: $external"} ${link_type:+"Link_Type: $link_type"} ${url:+"URL: $url"}
        else
            ___x_cmd_ui_tf false "${_err_msg}:"
            ___x_cmd_gl____handle_resp
            return 1
        fi
    )
}
# EndSection