package service

import (
	"fmt"

	"be.ems/src/framework/constants/cachekey"
	"be.ems/src/framework/constants/common"
	"be.ems/src/framework/redis"
	"be.ems/src/framework/utils/parse"
	systemModel "be.ems/src/modules/system/model"
	systemService "be.ems/src/modules/system/service"
)

// 实例化服务层 RegisterImpl 结构体
var NewRegisterImpl = &RegisterImpl{
	sysUserService:   systemService.NewSysUserImpl,
	sysConfigService: systemService.NewSysConfigImpl,
	sysRoleService:   systemService.NewSysRoleImpl,
}

// 账号注册操作处理 服务层处理
type RegisterImpl struct {
	// 用户信息服务
	sysUserService systemService.ISysUser
	// 参数配置服务
	sysConfigService systemService.ISysConfig
	// 角色服务
	sysRoleService systemService.ISysRole
}

// ValidateCaptcha 校验验证码
func (s *RegisterImpl) ValidateCaptcha(code, uuid string) error {
	// 验证码检查，从数据库配置获取验证码开关 true开启，false关闭
	captchaEnabledStr := s.sysConfigService.SelectConfigValueByKey("sys.account.captchaEnabled")
	if !parse.Boolean(captchaEnabledStr) {
		return nil
	}
	if code == "" || uuid == "" {
		return fmt.Errorf("verification code information error")
	}
	verifyKey := cachekey.CAPTCHA_CODE_KEY + uuid
	captcha, err := redis.Get("", verifyKey)
	if err != nil {
		return fmt.Errorf("the verification code has expired")
	}
	redis.Del("", verifyKey)
	if captcha != code {
		return fmt.Errorf("verification code error")
	}
	return nil
}

// ByUserName 账号注册
func (s *RegisterImpl) ByUserName(username, password, userType string) (string, error) {
	// 是否开启用户注册功能 true开启，false关闭
	registerUserStr := s.sysConfigService.SelectConfigValueByKey("sys.account.registerUser")
	captchaEnabled := parse.Boolean(registerUserStr)
	if !captchaEnabled {
		return "", fmt.Errorf("failed to register user [%s]. Sorry, the system has closed the external user registration channel", username)
	}

	// 检查用户登录账号是否唯一
	uniqueUserName := s.sysUserService.CheckUniqueUserName(username, "")
	if !uniqueUserName {
		return "", fmt.Errorf("failed to register user [%s], registered account already exists", username)
	}

	sysUser := systemModel.SysUser{
		UserName: username,
		NickName: username,          // 昵称使用名称账号
		Password: password,          // 原始密码
		Status:   common.STATUS_YES, // 账号状态激活
		DeptID:   "100",             // 归属部门为根节点
		CreateBy: "register",        // 创建来源
	}
	// 标记用户类型
	if userType == "" {
		sysUser.UserType = "sys"
	}
	// 新增用户的角色管理
	sysUser.RoleIDs = s.registerRoleInit(userType)
	// 新增用户的岗位管理
	sysUser.PostIDs = s.registerPostInit(userType)

	insertId := s.sysUserService.InsertUser(sysUser)
	if insertId != "" {
		return insertId, nil
	}
	return "", fmt.Errorf("failed to register user [%s]. Please contact the system administrator", username)
}

// registerRoleInit 注册初始角色
func (s *RegisterImpl) registerRoleInit(userType string) []string {
	if userType == "sys" {
		return []string{}
	}
	return []string{}
}

// registerPostInit 注册初始岗位
func (s *RegisterImpl) registerPostInit(userType string) []string {
	if userType == "sys" {
		return []string{}
	}
	return []string{}
}
