
package com.pavis.admin.auth.handler;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.pavis.admin.system.model.entity.UserRoleDO;
import com.pavis.admin.system.service.UserRoleService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Component;
import com.pavis.admin.auth.AbstractLoginHandler;
import com.pavis.admin.auth.enums.AuthTypeEnum;
import com.pavis.admin.auth.model.req.PhoneLoginReq;
import com.pavis.admin.auth.model.resp.LoginResp;
import com.pavis.admin.common.constant.CacheConstants;
import com.pavis.admin.system.model.entity.user.UserDO;
import com.pavis.admin.system.model.resp.ClientResp;
import top.continew.starter.cache.redisson.util.RedisUtils;
import top.continew.starter.core.validation.ValidationUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 手机号登录处理器
 *
 * 
 *
 * @since 2024/12/22 14:59
 */
@Component
public class PhoneLoginHandler extends AbstractLoginHandler<PhoneLoginReq> {
    @Resource
    private UserRoleService userRoleService;

    @Override
    public LoginResp login(PhoneLoginReq req, ClientResp client, HttpServletRequest request) {
        // 验证手机号
        UserDO user = userService.getByPhone(req.getPhone());
        //        ValidationUtils.throwIfNull(user, "此手机号未绑定本系统账号");

        // 手机号未绑定用户则自动注册
        if (ObjectUtil.isNull(user)) {
            String randomStr = RandomUtil.randomString(RandomUtil.BASE_CHAR, 5);

            // 设置基本信息
            user = new UserDO();
            String username = "user_" + randomStr + IdUtil.fastSimpleUUID();
            user.setPhone(req.getPhone());
            user.setUsername(username);
            user.setNickname(username);
            user.setIsFron(1); // 为前台角色

            // 保存用户并获取用户id
            userService.save(user);
            user = userService.getByPhone(req.getPhone());

            // 设置“插件用户角色”
            UserRoleDO userRole = new UserRoleDO(user.getId(), 698168080014012469L);
            List<UserRoleDO> users = new ArrayList<>();
            users.add(userRole);
            userRoleService.saveBatch(users);
        }

        // 检查用户状态
        super.checkUserStatus(user);
        // 执行认证
        String token = super.authenticate(user, client);
        return LoginResp.builder().token(token).build();
    }
    // @Override
    // public LoginResp login(PhoneLoginReq req, ClientResp client, HttpServletRequest request) {
    //     // 验证手机号
    //     UserDO user = userService.getByPhone(req.getPhone());
    //     ValidationUtils.throwIfNull(user, "此手机号未绑定本系统账号");
    //     // 检查用户状态
    //     super.checkUserStatus(user);
    //     // 执行认证
    //     String token = super.authenticate(user, client);
    //     return LoginResp.builder().token(token).build();
    // }

    @Override
    public void preLogin(PhoneLoginReq req, ClientResp client, HttpServletRequest request) {
        String phone = req.getPhone();
        String captchaKey = CacheConstants.CAPTCHA_KEY_PREFIX + phone;
        String captcha = RedisUtils.get(captchaKey);
        ValidationUtils.throwIfBlank(captcha, CAPTCHA_EXPIRED);
        ValidationUtils.throwIfNotEqualIgnoreCase(req.getCaptcha(), captcha, CAPTCHA_ERROR);
        RedisUtils.delete(captchaKey);
    }

    @Override
    public AuthTypeEnum getAuthType() {
        return AuthTypeEnum.PHONE;
    }
}