# shellcheck shell=sh
# shellcheck disable=SC2039,3043
___x_cmd_gcode_repo_release(){
    param:subcmd ___x_cmd_gcode_repo_release                 \
        ls                  "List repo release"                      \
        info                "Show detailed information of repo"      \
        create              "Create repo release"                    \
        "edit|ed"           "Edit release information"                  \
        rm                  "Remove the repo release"                \
        "attachment|asset"  "release attachment management"
    param:subcmd:try
    param:run

    ___x_cmd_gcode_repo_release _param_help_doc
    return 1
}

# Section: List
# http://localhost:4000/13.7/ee/api/releases/#list-releases
___x_cmd_gcode_repo_release_ls() {
    param:scope     ___x_cmd_gcode
    param:dsl    '
options:
    --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" sort order_by
    else
        ___x_cmd_gcode_curl get "/projects/${owner_repo}/releases" sort order_by | \
            x jo 2c             .name .tag_name .commit.short_id  ._links.self   | \
            x csv header --add   Name  TagName  Commit             Url           | \
            x csv static_tab
    fi
}
# EndSection

# Section: Info
# http://localhost:4000/13.7/ee/api/releases/#get-a-release-by-a-tag-name
___x_cmd_gcode_repo_release_info() {
    param:scope     ___x_cmd_gcode
    param:dsl    '
options:
    #1                           "The Git tag the release is associated with"                                        <>
    --repo|-r                    "<owner_path>/<repo_path> or .id=<repo_id>"                                         <>
    --include_html_description   "If true, a response includes HTML rendered Markdown of the release description."
    --json|-j                    "output json data"
'
    param:run
    ___x_cmd_gcode_param_init_owner_repo
    ___x_cmd_gcode_curl get "/projects/${owner_repo}/releases/$1" | _____x_cmd_gcode_release_ui_utils Info
}
# EndSection

# Section: Create
# http://localhost:4000/13.7/ee/api/releases/#create-a-release
___x_cmd_gcode_repo_release_create() {
    param:scope     ___x_cmd_gcode
    param:dsl    '
options:
    #1                          "The tag where the release is created from"                                       <>
    --repo|-r                   "<owner_path>/<repo_path> or .id=<repo_id>"                                       <>
    --ref                       "It can be a commit SHA, another tag name, or a branch name."                     <>=""
    --name                      "The release name"                                                                <>=""
    --description               "The description of the release. Can use Markdown str."                           <>=""
    --milestones                "The title of each milestone the release is associated with. "                    <>:array=""
    --assets_links              "An assets.links json object of preset assets links.(stdin -)"                    <>=""
    --released_at               "The date when the release is/was ready. Defaults to the current time. "          <>:datetime=""
    --json|-j                   "output json data"
'
    param:run
    [ -n "${1}${ref}" ] || {
        gcode:error "accepts 1 arg(tag name) or --ref <ref_info>. Use --help show help information"
        return 1
    }
    ___x_cmd_gcode_param_init_owner_repo
    [ -n "$1" ] || M='accepts 1 arg(tag name like v1.0.0), received empty' arg:ret:64
    local tag_name="$1"
    local milestones
    milestones=$(x jo "[$milestones]")

    [ "$assets_links" != '-' ] || assets_links="$(___x_cmd_cmds_cat)"
    local gen_gcode_json
    gen_gcode_json="$(param:option2json +tag_name -repo -assets_links ${assets_links+"assets=assets_links"} )"
    gcode:debug "$gen_gcode_json"
    ___x_cmd_gcode_curl post "/projects/${owner_repo}/releases" "gen_gcode_json" | _____x_cmd_gcode_release_ui_utils Creating
}
# EndSection

# Section: Edit
# http://localhost:4000/13.7/ee/api/releases/#update-a-release
___x_cmd_gcode_repo_release_edit() {
    param:scope  ___x_cmd_gcode
    param:dsl    '
options:
    #1                 "The Git tag the release is associated with"                                      <>
    --repo|-r          "<owner_path>/<repo_path> or .id=<repo_id>"                                       <>
    --name             "The release name."                                                               <>=""
    --description      "The description of the release. You can use Markdown."                           <>=""
    --milestones       "The title of each milestone to associate with the release"                       <>=""
    --released_at      "The date when the release is/was ready."                                         <>:datetime=""

    --json|-j          "output json data"
'
    param:run
    ___x_cmd_gcode_param_init_owner_repo
    local gen_gcode_json
    gen_gcode_json="$(param:option2json -repo)"
    gcode:debug "$gen_gcode_json"

    ___x_cmd_gcode_curl put "/projects/${owner_repo}/releases/${1}" "gen_gcode_json" | _____x_cmd_gcode_release_ui_utils Edit
}
# EndSection

# Section: Remove
# http://localhost:4000/13.7/ee/api/releases/#delete-a-release
# shellcheck disable=2154
___x_cmd_gcode_repo_release_rm() {
    param:scope  ___x_cmd_gcode
    param:dsl    '
options:
    #1              "The Git tag the release is associated with"            <>
    --repo|-r       "<owner_path>/<repo_path> or .id=<repo_id>"             <>
    --yes|-y        "Ignore remove prompt interception"
'
    param:run

    ___x_cmd_gcode_param_init_owner_repo

    [ "$yes" = "true" ] || ___x_cmd_ui_yesno "Are you sure you want to remove release $(___x_cmd_ui bold red "$1") on the $repo ?" || return
    ___x_cmd_gcode_curl del "/projects/${owner_repo}/releases/$1"| (
        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 $1 release of $repo repo"
        else
            ___x_cmd_ui_tf false "Remove repo release failure:"
            ___x_cmd_gcode____handle_resp
            return 1
        fi
    )
}
# EndSection

# Section: release UI utils
_____x_cmd_gcode_release_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 successfully"
                _err_msg="$1 repo release failure"
                ;;
            Info)
                _inf_msg="Getting repo release info successfully"
                _err_msg="Getting repo release info failure"
                ;;
        esac
        local _name=""
        x jo env . _name=.name gcode_resp_err=.error gcode_resp_msg=.message \
            tag_name=.tag_name commit=.commit.short_id asset_count=.assets.count released_at=.released_at url=._links.self
        if [ -n "$_name" ]; then
            ___x_cmd_ui_tf  true "${_inf_msg}:" ${_name:+"Name: $_name"} ${tag_name:+"Tag_Name: $tag_name"}  ${commit:+"Commit: $commit"} \
                ${asset_count:+"Asset_Count: $asset_count"} ${released_at:+"Released_At: $released_at"} ${url:+"URL: $url"}
        else
            ___x_cmd_ui_tf false "${_err_msg}:"
            ___x_cmd_gcode____handle_resp
            return 1
        fi
    )
}
# EndSection
