# shellcheck shell=sh
# shellcheck disable=SC2039,3043
# https://docs.gitlab.com/ee/api/projects.html#hooks
___x_cmd_gl_repo_hook(){
    param:subcmd ___x_cmd_gl_repo_hook                      \
        ls              "List repo hook"                    \
        info            "Output repo hook information"      \
        create          "Create repo hook"                  \
        "edit|ed"       "Edit repo hook"                    \
        rm              "Remove repo hook"
    param:subcmd:try
    param:run

    ___x_cmd_gl_repo_hook _param_help_doc
    return 1
}

# Section: List
# https://docs.gitlab.com/ee/api/projects.html#list-project-hooks
___x_cmd_gl_repo_hook_ls() {
    param:scope     ___x_cmd_gl
    param:dsl       <<A
options:
    --repo|-r   "<owner_path>/<repo_path> or .id=<repo_id>"       <>
    --json|-j   "output raw JSON data"
A
    param:run
    ___x_cmd_gl_param_init_owner_repo
    if [ -n "$json" ] || [ -n "$ENFORCE_JSON" ]; then
        ___x_cmd_gl_curl get "/projects/${owner_repo}/hooks"
    else
        ___x_cmd_gl_curl get "/projects/${owner_repo}/hooks" | \
            x jo 2c             .id .url .push_events | \
            x csv header --add   ID  URL  PushEvents  | \
            x csv static_tab
    fi
}
# EndSection

# Section: info & rm
# https://docs.gitlab.com/ee/api/projects.html#get-project-hook
___x_cmd_gl_repo_hook_info() {
    param:scope     ___x_cmd_gl
    param:dsl       <<A
options:
    #1          "The unique identifier of the hook."            <>:Number
    --repo|-r   "<owner_path>/<repo_path> or .id=<repo_id>"     <>

    --json|-j   "output raw JSON data"
A
    param:run
    ___x_cmd_gl_param_init_owner_repo
    ___x_cmd_gl_curl get "/projects/${owner_repo}/hooks/$1" | _____x_cmd_gl_hook_ui_utils Info
}

# https://docs.gitlab.com/ee/api/projects.html#delete-project-hook
___x_cmd_gl_repo_hook_rm() {
    param:scope     ___x_cmd_gl
    param:dsl       <<A
options:
    #1          "The unique identifier of the hook."                <>:Number
    --repo|-r   "<owner_path>/<repo_path> or .id=<repo_id>"         <>
    --yes|-y    "Ignore remove prompt interception"
A
    param:run
    ___x_cmd_gl_param_init_owner_repo
    [ "$yes" = "true" ] || ___x_cmd_ui_yesno "Are you sure you want to remove the hook $1 in the $owner_repo ?" || return
    ___x_cmd_gl_curl del "/projects/${owner_repo}/hooks/$1" | (
        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 hook $1 on $repo"
        else
            ___x_cmd_ui_tf false "Remove repo hook $1 on $repo failure:"
            ___x_cmd_gl____handle_resp
            return 1
        fi
    )
}
# EndSection

# Section: add & update
# https://docs.gitlab.com/ee/api/projects.html#add-project-hook
___x_cmd_gl_repo_hook_create() {
    param:scope     ___x_cmd_gl
    param:dsl       '
options:
    --repo|-r                      "<owner_path>/<repo_path> or .id=<repo_id>"                                      <>
    --url                          "The hook URL"                                                                   <>
    --token                        "Secret token to validate received payloads"                                     <>=""
    --push_events_branch_filter    "Trigger hook on push events for matching branches only"                         <>=""
    --deployment_events            "Trigger hook on deployment events"
    --issues_events                "Trigger hook on issues events"
    --job_events                   "Trigger hook on job events"
    --note_events                  "Trigger hook on note events"
    --push_events                  "Trigger hook on push events"
    --pipeline_events              "Trigger hook on pipeline events"
    --releases_events              "Trigger hook on release events"
    --tag_push_events              "Trigger hook on tag push events"
    --wiki_page_events             "Trigger hook on wiki events"
    --merge_requests_events        "Trigger hook on merge requests events"
    --enable_ssl_verification      "Do SSL verification when triggering the hook"
    --confidential_issues_events   "Trigger hook on confidential issues events"
    --confidential_note_events     "Trigger hook on confidential note events"

    --json|-j                      "output raw JSON data"
'
    param:run

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

    ___x_cmd_gl_param_init_owner_repo
    ___x_cmd_gl_curl post "/projects/${owner_repo}/hooks" "gen_gl_json" | _____x_cmd_gl_hook_ui_utils Creating
}

# https://docs.gitlab.com/ee/api/projects.html#edit-project-hook
___x_cmd_gl_repo_hook_edit() {
    param:scope     ___x_cmd_gl
    param:dsl       '
options:
    #1                             "The unique identifier of the hook."                                             <>:Number
    --repo|-r                      "<owner_path>/<repo_path> or .id=<repo_id>"                                      <>
    --url                          "The hook URL"                                                                   <>
    --token                        "Secret token to validate received payloads"                                     <>=""
    --push_events_branch_filter    "Trigger hook on push events for matching branches only"                         <>=""
    --deployment_events            "Trigger hook on deployment events"
    --issues_events                "Trigger hook on issues events"
    --job_events                   "Trigger hook on job events"
    --note_events                  "Trigger hook on note events"
    --push_events                  "Trigger hook on push events"
    --pipeline_events              "Trigger hook on pipeline events"
    --releases_events              "Trigger hook on release events"
    --tag_push_events              "Trigger hook on tag push events"
    --wiki_page_events             "Trigger hook on wiki events"
    --merge_requests_events        "Trigger hook on merge requests events"
    --enable_ssl_verification      "Do SSL verification when triggering the hook"
    --confidential_issues_events   "Trigger hook on confidential issues events"
    --confidential_note_events     "Trigger hook on confidential note events"

    --json|-j                      "output raw JSON data"
'
    param:run
    local gen_gl_json
    gen_gl_json="$(param:option2json -repo)"
    gl:debug "$gen_gl_json"

    ___x_cmd_gl_param_init_owner_repo
    ___x_cmd_gl_curl put "/projects/${owner_repo}/hooks/$1" "gen_gl_json" | _____x_cmd_gl_hook_ui_utils Edit
}

# EndSection

# Section: hook utils
_____x_cmd_gl_hook_ui_utils(){
    if [ -n "$ENFORCE_JSON" ] || [ -n "$json" ]; then
        ___x_cmd_cmds_cat
        ___x_cmd_gl_http_error
        return
    fi
    (
        case "$1" in
            Info)
                _inf_msg="Getting repo webhook info successfully"
                _err_msg="Getting repo webhook info failure"
                ;;
            Creating|Edit)
                _inf_msg="$1 repo webhook successfully"
                _err_msg="$1 repo webhook failure"
                ;;
        esac
        local _id=""
        x jo env . _id=.id gl_resp_err=.error gl_resp_msg=.message \
            url=.url push_events=.push_events
        if [ -n "$_id" ]; then
            ___x_cmd_ui_tf  true "${_inf_msg}:" "Id: $_id" ${push_events+"Push_Events: $push_events"} ${url+"URL: $url"}
        else
            ___x_cmd_ui_tf false "${_err_msg}:"
            ___x_cmd_gl____handle_resp
            return 1
        fi
    )
}
# EndSection