package sysUser

import (
	"context"

	v1 "kami/api/sysUser/v1"
	"kami/internal/consts"
	"kami/internal/errHandler"
	"kami/internal/model"
	"kami/internal/service"

	"github.com/gogf/gf/v2/errors/gcode"
)

func (c *ControllerV1) UserSuspendOrContinue(ctx context.Context, req *v1.UserSuspendOrContinueReq) (res *v1.UserSuspendOrContinueRes, err error) {
	_, err = service.SysAuth().LoginOnlyIFrame(ctx)
	if err != nil {
		err = errHandler.WrapError(ctx, gcode.CodeNotAuthorized, err, "权限不足")
		return
	}
	userService := service.SysUser()
	user, err := userService.GetUserById(ctx, req.ID)
	if err != nil {
		err = errHandler.WrapError(ctx, gcode.CodeInternalError, err, "获取账户失败")
		return
	}
	switch user.UserStatus {
	case uint(consts.SysUserStatusNormal):
		err = userService.ChangeUserStatus(ctx, &model.UserStatusInput{
			UserStatusReq: v1.UserStatusReq{
				CommonStrId: req.CommonStrId,
				UserStatus:  consts.SysUserStatusForbidden,
			},
		})
	case uint(consts.SysUserStatusForbidden):
		err = userService.ChangeUserStatus(ctx, &model.UserStatusInput{
			UserStatusReq: v1.UserStatusReq{
				CommonStrId: req.CommonStrId,
				UserStatus:  consts.SysUserStatusNormal,
			},
		})
	}
	if err != nil {
		err = errHandler.WrapError(ctx, gcode.CodeInternalError, err, "修改状态失败")
	}
	return
}
