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

___x_cmd_gh_issue() {
    param:scope  ___x_cmd_github
    param:subcmd ___x_cmd_gh_issue                  \
        ls              "List repo issue"           \
        create          "create an issue"           \
        "edit|ed"       "update an issue"           \
        close           "Close issue"               \
        reopen          "Reopen issue"
    param:subcmd:try
    param:run


    ___x_cmd_gh_issue _param_help_doc
    return 1
}

# Section: List
# https://docs.github.com/en/rest/issues/issues#list-repository-issues
___x_cmd_gh_issue_ls(){
    param:scope     ___x_cmd_github
    param:dsl       '
options:
    --repo|-r       "<owner_path>/<repo_path>"                                              <>:RepoName
    --state         "Indicates the state of the issues to return."                          <>="open"        = open closed all
    --milestone     "milestone number"                                                      <>:number=""
    --assignee      "User responsible for this issue."                                      <>:string=""
    --creator       "The user that created the issue."                                      <>:string=""
    --mentioned     "A user that mentioned in the issue."                                   <>:string=""
    --labels        "Labels to associate with this issue.(separated by commas)"             <>:array=""
    --sort          "What to sort results by"                                               <>="updated"    = created updated comments
    --direction     "The direction to sort the results by."                                 <>="desc"       = asc 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}/issues" state milestone assignee creator mentioned labels sort direction | ___x_cmd_gx_output_format
    else
        ___x_cmd_gh_get_multi "/repos/${owner_repo}/issues" state milestone assignee creator mentioned labels 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 csv static_tab
            fi
    fi
}
# EndSection

# Section: Create
# https://docs.github.com/en/rest/issues/issues#create-an-issue
# shellcheck disable=SC2154
___x_cmd_gh_issue_create(){
    param:scope     ___x_cmd_github
    param:dsl       '
options:
    --repo|-r   "<owner_path>/<repo_path>"                                              <>:RepoName
    --title     "The title of the issue."                                               <>:string

    --body      "The contents of the issue."                                            <>:string=""
    --milestone "milestone number"                                                      <>:number=""
    --assignees "User responsible for this issue.(separated by commas)"                 <>:array=""
    --labels    "Labels to associate with this issue.(separated by commas)"             <>:array=""
    --json|-j   "output json data"
'
    param:run
    ___x_cmd_gh_param_init_owner_repo
    [ -n "$owner_repo" ] || M='Please provide --repo <owner/repo>"' arg:ret:64

    [ -z "$labels" ] || labels=$(x jo "[$labels]")
    [ -z "$assignees" ] || assignees=$(x jo "[$assignees]")
    local gen_gh_json=""
    gen_gh_json="$(param:option2json -repo -json)"
    gh:debug "$gen_gh_json"
    ___x_cmd_gh_curl post "/repos/${owner_repo}/issues" gen_gh_json | ___x_cmd_gh_issue____ui_handler Creating
}
# EndSection

# Section: Edit
# https://docs.github.com/en/rest/issues/issues#update-an-issue
# shellcheck disable=SC2034
___x_cmd_gh_issue_edit() {
    param:scope ___x_cmd_github
    param:dsl '
options:
    #1          "The number that identifies the issue."                                 <>:number
    --repo|-r   "<owner_path>/<repo_path>"                                              <>:RepoName

    --title     "The title of the issue."                                               <>:string=""
    --body      "The contents of the issue."                                            <>:string=""
    --state     "State of the issue"                                                    <>:string="" = open closed
    --assignees "User responsible for this issue."                                      <>:string=""
    --milestone "milestone number"                                                      <>:number=""
    --labels    "Labels to associate with this issue.(separated by commas)"             <>:array=""

    --json|-j   "output json data"
'
    param:run
    [ -z "$labels" ]    || labels=$(x jo "[$labels]")
    [ -z "$assignees" ] || assignees=$(x jo "[$assignees]")
    local gen_gh_json=""
    gen_gh_json="$(param:option2json -repo -json)"
    gh:debug "$gen_gh_json"
    ___x_cmd_gh_param_init_owner_repo

    ___x_cmd_gh_curl patch "/repos/${owner_repo}/issues/${1}" gen_gh_json | ___x_cmd_gh_issue____ui_handler Edit
}
# EndSection

# Section: Reopen
# https://docs.github.com/en/rest/issues/issues#update-an-issue
___x_cmd_gh_issue_reopen(){
    param:scope     ___x_cmd_github
    param:dsl       '
options:
    #1          "The number that identifies the issue."         <>:number
    --repo|-r   "<owner_path>/<repo_path>"                      <>:RepoName
    --json|-j   "output json data"
'
    param:run

    local state=open

    ___x_cmd_gh_param_init_owner_repo
    ___x_cmd_gh_curl patch "/repos/${owner_repo}/issues/${1}" -- state | ___x_cmd_gh_issue____ui_handler Reopen
}
# EndSection

# Section: Close
# https://docs.github.com/en/rest/issues/issues#update-an-issue
___x_cmd_gh_issue_close(){
     param:scope     ___x_cmd_github
    param:dsl       '
options:
    --repo|-r   "<owner_path>/<repo_path>"                  <>:RepoName
    #1          "The number that identifies the issue."     <>:number

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

    local state=closed

    ___x_cmd_gh_param_init_owner_repo
    ___x_cmd_gh_curl patch "/repos/${owner_repo}/issues/${1}" -- state | ___x_cmd_gh_issue____ui_handler Close
}
# EndSection

# Section: issue UI
# shellcheck disable=SC2154
___x_cmd_gh_issue____ui_handler(){
    if [ -n "$ENFORCE_JSON" ] || [ -n "$json" ]; then
        ___x_cmd_cmds_cat
        ___x_cmd_gh_http_error
        return
    fi
    (
        local _id=""
        case "$1" in
            Creating|Edit)
                x jo env . _id=.number gh_resp_msg=.message gh_resp_err=.errors \
                    title=.title url=.html_url state=.state
                ;;
            Reopen|Close)
                x jo env . _id=.number gh_resp_msg=.message gh_resp_err=.errors
                ;;
        esac
        if [ -n "$_id" ]; then
            ___x_cmd_ui_tf true "$1 issue successfully:" "number: $_id" ${title:+"title: $title"}  ${url:+"url: $url"} ${state:+"state: $state"}
        else
            ___x_cmd_ui_tf false "$1 issue failure:" >&2
            ___x_cmd_gh____handle_resp
            return 1
        fi
    )
}
# EndSection

# Section: User ISSUE
# https://docs.github.com/en/rest/issues/issues#list-issues-assigned-to-the-authenticated-user
___x_cmd_gh_user_issue(){
    param:scope     ___x_cmd_github
    param:dsl       '
type:
    Screen = all assigned created mentioned subscribed repos
    State = open closed all
    Sort = created updated comments
    Direction = desc asc
options:
    --filter        "filter(assigned,created,all)"                 <>:Screen="assigned"
    --state         "state(open,progerssing,closed,rejected)"      <>:State="open"
    --sort          "sort"                                         <>:Sort="created"
    --labels        "labels"                                       <>=""
    --direction     "direction(asc、desc)"                         <>:Direction="desc"
    --since         "updated date"                                 <>:ISOTime=""
    --page          "page"                                         <>:Numbers="1"
    --per_page      "per_page"                                     <>:Numbers="30"
    --json|-j       "output origin json data"
'
    param:run

    if [ -n "$json" ] || [ -n "$ENFORCE_JSON" ]; then
        ___x_cmd_gh_get_multi "/user/issues" filter state sort labels direction since
    else
        ___x_cmd_gh_get_multi "/user/issues" filter state sort labels direction since | \
            x jo 2c            .number       .repository.full_name .state .title  | \
            x csv header --add  Issue_Number  Repo_Name             State  Title | \
            x csv static_tab
    fi
}
# EndSection

# Section: Org Issue
# https://docs.github.com/en/rest/issues/issues#list-organization-issues-assigned-to-the-authenticated-user
# shellcheck disable=SC2154
___x_cmd_gh_org_issue(){
    param:scope     ___x_cmd_github
    param:dsl       '
type:
    Screen = all assigned created mentioned subscribed repos
    State = open closed all
    Sort = created updated comments
    Direction = desc asc
options:
    --org           "organization name"                            <>:Address
    --filter        "filter(assigned,created,all)"                 <>:Screen="assigned"
    --state         "state(open,progerssing,closed,rejected)"      <>:State="open"
    --labels        "labels"                                       <>=""
    --sort          "What to sort results by"                      <>:Sort="created"
    --direction     "direction(asc、desc)"                         <>:Direction="desc"
    --since         "updated date"                                 <>:ISOTime=""
    --page          "page"                                         <>:Numbers="1"
    --per_page      "per_page"                                     <>:Numbers="30"
    --json|-j       "output origin json data"
'
    param:run
    if [ -n "$json" ] || [ -n "$ENFORCE_JSON" ]; then
        ___x_cmd_gh_get_multi "/orgs/${org##*/}/issues" filter state sort labels direction since
    else
        ___x_cmd_gh_get_multi "/orgs/${org##*/}/issues" filter state sort labels direction since | \
            x jo 2c            .number       .repository.full_name .state .title  | \
            x csv header --add  Issue_Number  Repo_Name             State  Title | \
            x csv static_tab
    fi
}
# EndSection