# shellcheck shell=sh
# shellcheck disable=SC2039,3043

___x_cmd_gh_package_tag(){
    param:scope     ___x_cmd_github
    param:subcmd ___x_cmd_gh_package_tag               \
        ls          "List packages tag by owner"       \
        info        "get user packages info"           \
        rm          "remove user packages"
    param:subcmd:try
    param:run

    ___x_cmd_gh_package_tag _param_help_doc
    return 1
}


# Section: List
# https://docs.github.com/en/rest/packages#list-package-versions-for-a-package-owned-by-a-user
# shellcheck disable=SC2154
___x_cmd_gh_package_tag_ls() {
    param:scope     ___x_cmd_github
    param:dsl       '
options:
    --package_name  "The name of the package."                  <>
    --owner         "Specify an organization or user."          <>
    --package_type  "The type of supported package."            <> = container npm maven rubygems docker nuget
    --per_page      "Results per page"                          <>="30"
    --page          "Page number of the results to fetch."      <>="1"

    --json|-j       "output origin json data"
    --csv           "output csv data"
    --yml           "output yml data"
'
    param:run
    local owner_type=""
    owner_type="$(___x_cmd_gh_owner_type_query "$owner" 2>/dev/null)"
    [ -n "$owner" ] || owner="$(___x_cmd_gh_cfg get owner)"

    if ___x_cmd_gx_output_is_format; then
        ___x_cmd_gh_get_multi "/${owner_type}s/${owner}/packages/${package_type}/${package_name}/versions" | ___x_cmd_gx_output_format
    else
        ___x_cmd_gh_get_multi "/${owner_type}s/${owner}/packages/${package_type}/${package_name}/versions" | \
            x jo 2c             .id .name .html_url | \
            x csv header --add   ID  Name  URL | \
            if [ -n "$csv" ]; then  ___x_cmd_cmds cat
            else                    x csv static_tab
            fi
    fi
}
# EndSection

# Section: Info
# https://docs.github.com/en/rest/packages#get-a-package-version-for-a-user
___x_cmd_gh_package_tag_info() {
    param:scope     ___x_cmd_github
    param:dsl       '
options:
    #1              "Unique identifier of the package version ID" <>
    --package_name  "The name of the package."                    <>
    --owner         "Specify an organization or user."            <>
    --package_type  "The type of supported package."              <> = container npm maven rubygems docker nuget
'
    param:run

    local owner_type=""
    owner_type="$(___x_cmd_gh_owner_type_query "$owner" 2>/dev/null)"
    [ -n "$owner" ] || owner="$(___x_cmd_gh_cfg get owner)"
    ___x_cmd_gh_curl get "/${owner_type}s/${owner}/packages/${package_type}/${package_name}/versions/$1" | ___x_cmd_package_tag_____ui_handler Info "$1"
}
# EndSection

# Section: Remove
# https://docs.github.com/en/rest/packages#delete-package-version-for-a-user
# shellcheck disable=SC2154
___x_cmd_gh_package_tag_rm() {
    param:scope     ___x_cmd_github
    param:dsl       '
options:
    #1              "Unique identifier of the package version ID"   <>
    --package_name  "The name of the package."                      <>
    --owner         "Specify an organization or user."              <>
    --package_type  "The type of supported package."                <> = container npm maven rubygems docker nuget
    --yes|-y        "Ignore remove prompt interception"
'
    param:run
    local owner_type=""
    owner_type="$(___x_cmd_gh_owner_type_query "$owner" 2>/dev/null)"
    [ -n "$owner" ] || owner="$(___x_cmd_gh_cfg get owner)"

    [ "$yes" = "true" ] || ___x_cmd_ui_yesno "Are you sure you want to delete this packages: $(___x_cmd_ui bold red "$1") ?" || return
    ___x_cmd_gh_curl del "/${owner_type}s/${owner}/packages/${package_type}/${package_name}/versions/$1" | (
            [ -z "$___X_CMD_GH_IN_TEST" ] || { ___x_cmd_cmds_cat; return; }
            x jo env . gh_resp_msg=.message gh_resp_err=.errors
            if ___x_cmd_gh_http_error; then
                ___x_cmd_ui_tf  true "[Success]: Remove $1 packages"
            else
                ___x_cmd_ui_tf false "Remove $1 packages failure:" >&2
                ___x_cmd_gh____handle_resp
                return 1
            fi
        )
}
# EndSection

# Section: package UI
___x_cmd_package_tag_____ui_handler(){
    if  [ -n "$ENFORCE_JSON" ] || [ -n "$json" ]; then
        ___x_cmd_cmds_cat
        ___x_cmd_gh_http_error
        return
    fi
    (
        case "$1" in
            Info)
                _inf_msg="Getting $package_name package tag $2 information successfully"
                _err_msg="Getting $package_name package tag $2 information failure"
                ;;
        esac
        local _id=""
        x jo env . _id=.id gh_resp_msg=.message gh_resp_err=.errors \
            name=.name url=.html_url license=.license package_type=.metadata.package_type created_at=.created_at updated_at=.updated_at
        if [ -n "$_id" ]; then
            ___x_cmd_ui_tf  true "${_inf_msg}:" ${_id:+"ID: $_id"} ${name:+"Name: $name"} \
                ${license:+"License: $license"} ${package_type:+"Package_Type: $package_type"} \
                ${created_at:+"Created_At: $created_at"} ${updated_at:+"Updated_At: $updated_at"} ${url:+"Url: $url"}
        else
            ___x_cmd_ui_tf false "${_err_msg}:" >&2
            ___x_cmd_gh____handle_resp
            return 1
        fi
    )
}
# EndSection