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

___x_cmd_gh_repo_pr(){
    param:scope  ___x_cmd_github
    param:subcmd ___x_cmd_gh_repo_pr            \
        ls              "list pull requests"    \
        info            "pull request info"     \
        create          "create pull request"   \
        "edit|ed"       "update pull requests"  \
        merge           "merge pull requests"
    param:subcmd:try
    param:run

    ___x_cmd_gh_repo_pr _param_help_doc
    return 1
}

# Section: List
# https://docs.github.com/en/rest/pulls/pulls#list-pull-requests
# shellcheck disable=SC2154
___x_cmd_gh_repo_pr_ls() {

    param:scope     ___x_cmd_github
    param:dsl       '
options:
    --repo|-r       "<owner_path>/<repo_path>"                                          <>:RepoName
    --base          "Filter pulls by base branch name"                                  <>=""
    --head          "Filter pulls by head user or head organization and branch name"    <>=""
    --state         "Filter pulls by issue state"                                       <>:PrState="open"
    --sort          "List sort way"                                                     <>:PrSort="created"
    --direction     "List direction"                                                    <>:Direction="desc"
    --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

    ___x_cmd_gh_param_init_owner_repo
    [ -n "$owner_repo" ] || M='Please provide --repo <owner/repo>"' arg:ret:64
    if ___x_cmd_gx_output_is_format; then
        ___x_cmd_gh_get_multi "/repos/$owner_repo/pulls" state base head sort direction | ___x_cmd_gx_output_format
    else
        ___x_cmd_gh_get_multi "/repos/$owner_repo/pulls" state base head sort direction | \
            x jo 2c             .number  .state  .title   | \
            x csv header --add   Number   State   Title   | \
            if [ -n "$csv" ]; then  ___x_cmd_cmds cat
            else                    ___x_cmd csv static_tab
            fi
    fi
}
# EndSection

# Section: Info
# https://docs.github.com/en/rest/pulls/pulls#get-a-pull-request
___x_cmd_gh_repo_pr_info() {
    param:scope     ___x_cmd_github
    param:dsl       '
options:
    #1              "pr number"                 <>:Number
    --repo|-r       "<owner_path>/<repo_path>"  <>:RepoName

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

    ___x_cmd_gh_param_init_owner_repo
    ___x_cmd_gh_curl get "/repos/${owner_repo}/pulls/$1" | ___x_cmd_gh_pr____ui_handler Info
}
# EndSection

# Section: Create
# https://docs.github.com/en/rest/pulls/pulls#create-a-pull-request
___x_cmd_gh_repo_pr_create() {
    param:scope     ___x_cmd_github
    param:dsl       '
type:
    bool = true false
options:
    --repo|-r               "<owner_path>/<repo_path>"                                          <>:RepoName
    --title                 "pr title"                                                          <>
    --head                  "source branch. Format: [username:]<branch>"                        <>
    --base                  "target branch. Format: [username:]<branch>"                        <>
    --body                  "pull request content"                                              <>=""
    --issue                 "association issue"                                                 <>=""
    --draft                 "is draft"
    --no_maintainer_modify  "Not allow maintainers modify the pull request"
    --json|-j               "output json data"
'
    param:run
    ___x_cmd_gh_param_init_owner_repo
    local gen_gh_json=""
    gen_gh_json="$(param:option2json -repo -no_maintainer_modify 'draft=^^draft' 'maintainer_can_modify=^no_maintainer_modify')"
    gh:debug "$gen_gh_json"

    ___x_cmd_gh_curl post "/repos/$owner_repo/pulls" "gen_gh_json" | ___x_cmd_gh_pr____ui_handler Creating
}
# EndSection

# Section: Edit
# https://docs.github.com/en/rest/pulls/pulls#update-a-pull-request
___x_cmd_gh_repo_pr_edit() {
    param:scope     ___x_cmd_github
    param:dsl       '
type:
    State = closed open
options:
    #1                      "pr number"                                                         <>:number
    --repo|-r               "<owner_path>/<repo_path>"                                          <>:RepoName
    --state                 "state"                                                             <>:State
    --base                  "target branch. Format: [username:]<branch>"                        <>:name=""
    --title                 "pr title"                                                          <>=""
    --body                  "pull request content"                                              <>=""
    --no_maintainer_modify  "Not allow maintainers modify the pull request."
    --json|-j               "output json data"
'
    param:run

    ___x_cmd_gh_param_init_owner_repo
    local gen_gh_json=""
    gen_gh_json="$(param:option2json -repo -no_maintainer_modify maintainer_can_modify=^^no_maintainer_modify)"
    gh:debug "$gen_gh_json"

    ___x_cmd_gh_curl patch "/repos/${owner_repo}/pulls/$1" "gen_gh_json" | ___x_cmd_gh_pr____ui_handler Edit
}
# EndSection

# Section: Merge
# https://docs.github.com/en/rest/pulls/pulls#merge-a-pull-request
# shellcheck disable=2154
___x_cmd_gh_repo_pr_merge() {
    param:scope     ___x_cmd_github
    param:dsl       '
type:
    merge = squash merge rebase
options:
    #1                      "pr id"                                                             <>:Number
    --repo|-r               "<owner_path>/<repo_path>"                                          <>:RepoName
    --merge_method          "merge method(squash,merge)"                                        <>:merge="merge"
    --commit_title          "Title for the automatic commit message."                           <>=""
    --commit_message        "Extra detail to append to automatic commit message."               <>=""
'
    param:run

    ___x_cmd_gh_param_init_owner_repo
    local gen_gh_json=""
    gen_gh_json="$(param:option2json -repo)"
    gh:debug "$gen_gh_json"

    ___x_cmd_gh_curl put "/repos/${owner_repo}/pulls/$1/merge" "gen_gh_json" | (
            [ -z "$___X_CMD_GH_IN_TEST" ] || { ___x_cmd_cmds_cat; return; }
            local merged=""
            x jo env . merged=.merged gh_resp_msg=.message gh_resp_err=.errors
            if [ "$merged" = "true" ]; then
                ___x_cmd_ui_tf  true "Pull Request $1 merge to repo ${owner_repo} successfully:"
            else
                ___x_cmd_ui_tf false "Pull Request $1 merge to repo ${owner_repo} failure:" >&2
                ___x_cmd_gh____handle_resp
                return 1
            fi
        )
}
# EndSection

# Section: pr UI
___x_cmd_gh_pr____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="Successfully obtained PR information"
                _err_msg="Failed to get PR information"
                ;;
            Creating)

                _inf_msg="Successfully obtained PR information"
                _err_msg="Failed to get PR information"
                ;;
            Edit)
                _inf_msg="Edit PR success"
                _err_msg="Failed to Edit PR"
                ;;
        esac
        local id=""
        x jo env . id=.id gh_resp_msg=.message gh_resp_err=.errors \
            url=.html_url state=.state title=.title body=.body
        if [ -n "$id" ]; then
            ___x_cmd_ui_tf  true "${_inf_msg}:" ${url:+"Url: $url"} ${state:+"State: $state"} ${title:+"Title: $title"} ${body:+"Body: $body"}
        else
            ___x_cmd_ui_tf false "${_err_msg}:" >&2
            ___x_cmd_gh____handle_resp
            return 1
        fi
    )
}

# EndSection
