# shellcheck shell=sh disable=SC2039,3043,2154
___x_cmd_gcode_repo_release_attachment(){
    param:subcmd ___x_cmd_gcode_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_gcode_repo_release_attachment _param_help_doc
    return 1
}

# Section: List
# http://localhost:4000/13.7/ee/api/releases/links.html#get-links
___x_cmd_gcode_repo_release_attachment_ls(){
    param:scope     ___x_cmd_gcode
    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 json data"
'
    param:run

    ___x_cmd_gcode_param_init_owner_repo
    if [ -n "$json" ] || [ -n "$ENFORCE_JSON" ]; then
        ___x_cmd_gcode_curl get "/projects/${owner_repo}/releases/${tag_name}/assets/links"
    else
        ___x_cmd_gcode_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
# http://localhost:4000/13.7/ee/api/releases/links.html#get-a-link
___x_cmd_gcode_repo_release_attachment_info(){
    param:scope     ___x_cmd_gcode
    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 json data"
'
    param:run
    ___x_cmd_gcode_param_init_owner_repo
    ___x_cmd_gcode_curl get "/projects/${owner_repo}/releases/${tag_name}/assets/links/$1" | ______x_cmd_gcode_repo_release_attachment_ui_utils Info
}
# EndSection

# Section: Create
# http://localhost:4000/13.7/ee/api/releases/links.html#create-a-link
___x_cmd_gcode_repo_release_attachment_create(){
    param:scope     ___x_cmd_gcode
    param:dsl    '
options:
    --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. 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 json data"
'
    param:run
    local gen_gcode_json
    gen_gcode_json="$(param:option2json -repo -tag_name)"
    gcode:debug "$gen_gcode_json"
    ___x_cmd_gcode_param_init_owner_repo
    ___x_cmd_gcode_curl post "/projects/${owner_repo}/releases/$tag_name/assets/links" "gen_gcode_json" | \
        ______x_cmd_gcode_repo_release_attachment_ui_utils Creating
}
# EndSection

# Section: Edit
# http://localhost:4000/13.7/ee/api/releases/links.html#update-a-link
___x_cmd_gcode_repo_release_attachment_edit(){
    param:scope     ___x_cmd_gcode
    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 json data"
'
    param:run
    local gen_gcode_json
    gen_gcode_json="$(param:option2json -repo -tag_name)"
    gcode:debug "$gen_gcode_json"
    ___x_cmd_gcode_param_init_owner_repo
    ___x_cmd_gcode_curl put "/projects/${owner_repo}/releases/$tag_name/assets/links/$1" "gen_gcode_json" | \
        ______x_cmd_gcode_repo_release_attachment_ui_utils Edit
}
# EndSection

# Section: Remove
# http://localhost:4000/13.7/ee/api/releases/links.html#delete-a-link
___x_cmd_gcode_repo_release_attachment_rm(){
    param:scope  ___x_cmd_gcode
    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_gcode_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_gcode_curl del "/projects/${owner_repo}/releases/${tag_name}/assets/links/${_link_id}" | (
            x jo env . gcode_resp_err=.error gcode_resp_msg=.message
            if ___x_cmd_gcode_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_gcode____handle_resp
                return 1
            fi
        )
    done

}
# EndSection

# Section: Upload
# http://localhost:4000/13.7/ee/api/projects.html#upload-a-file
# http://localhost:4000/13.7/ee/api/releases/links.html#create-a-link
___x_cmd_gcode_repo_release_attachment_upload(){
    param:scope  ___x_cmd_gcode
    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_gcode_param_init_owner_repo
    [ -n "$1" ] || M='accepts 1 arg(target upload attachment file path), received empty' arg:ret:64
    local _file_path="$1"
    [ "$_file_path" != '-' ] || _file_path="$(___x_cmd_cmds_cat)"

    gcode:info "Uploading file $_file_path ..."
    ___x_cmd_gcode_curl upload "/projects/${owner_repo}/uploads" "$_file_path" | (
        local full_path=""
        x jo env . gcode_resp_err=.error gcode_resp_msg=.message full_path=.full_path
        if [ -n "$full_path" ]; then
            local _target_url="https://gitcode.net${full_path}"
            [ -n "$name" ] || name="${_target_url##*/}"
            [ -n "$filepath" ] || filepath="/${_target_url##*/}"

            gcode:info "Creating release attachment link. Please wait a moment..."
            if ___x_cmd_gcode_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_gcode____handle_resp
            return 1
        fi
    )
}
# EndSection

# Section: release asset UI utils
______x_cmd_gcode_repo_release_attachment_ui_utils(){
    if [ -n "$ENFORCE_JSON" ] || [ -n "$json" ]; then
        ___x_cmd_cmds_cat
        ___x_cmd_gcode_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 gcode_resp_err=.error gcode_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}:" >&2
            ___x_cmd_gcode____handle_resp
            return 1
        fi
    )
}
# EndSection