package admin

import (
	"fmt"
	"net/http"

	"bindbox-game/internal/code"
	"bindbox-game/internal/pkg/core"
	"bindbox-game/internal/pkg/validation"
	adminsvc "bindbox-game/internal/service/admin"
)

type createAdminRequest struct {
	UserName string `json:"username" binding:"required"` // 用户名
	NickName string `json:"nickname" binding:"required"` // 昵称
	Mobile   string `json:"mobile"`                      // 手机号
	Password string `json:"password" binding:"required"` // 密码
	Avatar   string `json:"avatar"`                      // 头像
}

type createAdminResponse struct {
	Message string `json:"message"` // 提示信息
}

// CreateAdmin 新增客服
// @Summary 新增客服
// @Description 新增客服
// @Tags 管理端.客服管理
// @Accept json
// @Produce json
// @Param RequestBody body createAdminRequest true "请求参数"
// @Success 200 {object} createAdminResponse
// @Failure 400 {object} code.Failure
// @Router /api/admin/create [post]
// @Security LoginVerifyToken
func (h *handler) CreateAdmin() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(createAdminRequest)
		res := new(createAdminResponse)
		if err := ctx.ShouldBindJSON(req); err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusBadRequest,
				code.ParamBindError,
				validation.Error(err)),
			)
			return
		}

		if ctx.SessionUserInfo().IsSuper != 1 {
			ctx.AbortWithError(core.Error(
				http.StatusBadRequest,
				code.CreateAdminError,
				fmt.Sprintf("%s: %s", code.Text(code.CreateAdminError), "禁止操作")),
			)
			return
		}

		if err := h.svc.Create(ctx.RequestContext(), adminsvc.CreateInput{
			Username:  req.UserName,
			Nickname:  req.NickName,
			Mobile:    req.Mobile,
			Password:  req.Password,
			Avatar:    req.Avatar,
			CreatedBy: ctx.SessionUserInfo().UserName,
		}); err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusBadRequest,
				code.CreateAdminError,
				fmt.Sprintf("%s: %s", code.Text(code.CreateAdminError), err.Error())),
			)
			return
		}

		res.Message = "操作成功"
		ctx.Payload(res)
	}
}
