# shellcheck shell=sh
# shellcheck disable=SC2039,3043
# TODO: need to update $O : https://gitee.com/api => https://api.gitee.com

___x_cmd_gt_enterprise_team(){
    param:subcmd ___x_cmd_gt_enterprise_team                        \
        ls              "List enterprise team"                      \
        add             "Create a team"                             \
        info            "Show team info"                            \
        update          "Setup team info"                           \
        rm              "Remove the team"                           \
        repo            "List enterprise team repo"                 \
        member          "enterprise team member management"
    param:subcmd:try
    param:run

    if [ -z "$PARAM_SUBCMD" ]; then
        ___x_cmd_gt_enterprise_team _param_help_doc
        return 0
    fi

    "___x_cmd_gt_enterprise_team_$PARAM_SUBCMD" "$@"
}

# Section: list & info
# @see: https://gitee.com/api/v8/swagger#/getEnterpriseIdGroups
# shellcheck disable=SC2154
___x_cmd_gt_enterprise_team_ls(){
    param:scope     ___x_cmd_gt
    param:dsl       '
type:
    QueryType = "" path
    Sort = created_at updated_at
    Direction = asc desc
    Scope = "" admin
options:
    #1|--enterprise_id    "provide enterprise id"       <>
    --qt                  "query type"                  <>:QueryType=""
    --sort                "sort by time type"           <>:Sort="created_at"
    --direction           "sort direction"              <>:Direction="asc"
    --search              "search by keywords"          <>=""
    --scope               "query type"                  <>:Scope=""
    --page                "page"                        <>=""
    --per_page            "per_page"                    <>=""
'
    param:run
    ___x_cmd_gt_get_multi "/enterprises/${enterprise_id}/groups"     qt sort direction search scope
}

# shellcheck disable=SC2154
# https://gitee.com/api/v8/swagger#/getEnterpriseIdGroupsGroupId
___x_cmd_gt_enterprise_team_info(){
    param:scope     ___x_cmd_gt
    param:dsl       '
type:
    QueryType = "" path
options:
    #1|--enterprise_id    "provide enterprise id"       <>
    --qt                  "query type"                  <>:QueryType=""
    --group_id            "team id or team path"        <>

'
    param:run
    ___x_cmd_gt_curl get "/enterprises/${enterprise_id}/groups/${group_id}" qt
}

___x_cmd_gt_enterprise_team_repo() {
    param:void
    :
}
# EndSection

# Section: add & update & rm
# https://gitee.com/api/v8/swagger#/postEnterpriseIdGroups
___x_cmd_gt_enterprise_team_add(){
    param:scope     ___x_cmd_gt
    param:dsl       '
options:
    #1|--enterprise_id    "provide enterprise id"       <>
    --path                "team path"                   <>
    --name                "team name"                   <>
    --public              "team name"                   <>
    --owner_id            "owner user id"               <>

    --description         "team description"            <>=""
    --user_ids            "team member id('\;'split)"   <>=""
'
    param:run
    ___x_cmd_gt_curl post "/enterprises/${enterprise_id}/groups" path name public owner_id description user_ids
}

# shellcheck disable=SC2034,SC2154
# https://gitee.com/api/v8/swagger#/putEnterpriseIdGroupsGroupId
___x_cmd_gt_enterprise_team_update(){
    param:scope     ___x_cmd_gt
    param:dsl       '
type:
    Access  =   private public outsourcing
options:
    #1|--enterprise_id    "provide enterprise id"       <>
    --group_id            "team id"                     <>

    --access              "team type"                   <>:Access=private
    --name                "team name"                   <>=""
    --description         "team description"            <>=""
    --owner_id            "owner user id"               <>=""
    --user_ids            "team member id(','split)"    <>=""
'
    param:run
    local public
    case "$access" in
        private)        public=0 ;;
        public)         public=1 ;;
        innerSource)    public=2 ;;
    esac

    ___x_cmd_gt_curl put "/enterprises/${enterprise_id}/groups/${group_id}" -- public name description user_ids
}

# TODO: strange parameters： https://gitee.com/api/v8/swagger#/deleteEnterpriseIdGroupsGroupId
___x_cmd_gt_enterprise_team_rm(){
    param:scope     ___x_cmd_gt
    param:dsl       '
options:
    #1|--enterprise_id    "provide enterprise id"       <>
    --group_id            "team id"                     <>

    --validate_type       "ways of identifying"         <>=""
    --sms_captcha         "SMS verification code"       <>=""
    --password            "user password"               <>=""
'
    param:run
    ___x_cmd_gt_curl del "/enterprises/${enterprise_id}/groups/${group_id}" validate_type sms_captcha password
}
# EndSection

# Section: member
___x_cmd_gt_enterprise_team_member(){
    param:subcmd ___x_cmd_gt_enterprise_team_member                 \
        ls              "List enterprise team member"               \
        rm              "Remove member of team"
    param:subcmd:try
    param:run

    ___x_cmd_gt_enterprise_team_member _param_help_doc
    return 1
}

# https://gitee.com/api/v8/swagger#/getEnterpriseIdGroupsGroupIdMembers
___x_cmd_gt_enterprise_team_member_ls(){
    param:scope     ___x_cmd_gt
    param:dsl       '
type:
    QueryType = "" path
    Sort = created_at updated_at
    Direction = asc desc
options:
    #1|--enterprise_id    "provide enterprise id"       <>
    --qt                  "query type"                  <>:QueryType=""
    --group_id            "team id or team path"        <>


    --sort                "sort by time type"           <>:Sort="created_at"
    --direction           "sort direction"              <>:Direction="asc"
    --search              "search by keywords"          <>=""
    --page                "page"                        <>=""
    --per_page            "per_page"                    <>=""
'
    param:run
    ___x_cmd_gt_get_multi "/enterprises/${enterprise_id}/groups/${group_id}/members"  qt sort direction search
}

# https://gitee.com/api/v8/swagger#/deleteEnterpriseIdGroupsGroupIdMembers
___x_cmd_gt_enterprise_team_member_rm(){
    param:scope     ___x_cmd_gt
    param:dsl       '
options:
    #1|--enterprise_id    "provide enterprise id"       <>
    --group_id            "team id"                     <>

    --user_ids            "team member id(','split)"    <>
'
    param:run
    ___x_cmd_gt_curl del "/enterprises/${enterprise_id}/groups/${group_id}/members" user_ids
}
# EndSection