package com.schisandra.auth.application.controller;


import com.schisandra.auth.application.context.SmsConfigContext;
import com.schisandra.auth.application.dto.SchisandraCaptchaDTO;
import com.schisandra.auth.application.utils.CheckRouteCaptcha;
import com.schisandra.auth.common.entity.CaptchaResult;
import com.schisandra.auth.common.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.dromara.sms4j.api.entity.SmsResponse;
import org.dromara.sms4j.comm.utils.SmsUtils;
import org.dromara.sms4j.core.factory.SmsFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import static java.util.concurrent.TimeUnit.SECONDS;

@RestController
@RequestMapping("/sms/")
@Slf4j
public class SchisandraSmsController {
    @Resource
    private RedisUtil redisUtil;

    private final String AUTH_PHONE_PREFIX = "auth.phone";
    private final String SMS_CONFIG_PREFIX = "sys.config";

    @Resource
    CheckRouteCaptcha checkRouteCaptcha;

    /**
     * @description: 发送短信验证码
     * @param: [phone]
     * @return: com.schisandra.auth.common.bo.Result
     * @author zlg
     * @date: 2024/5/8 22:53
     */
    @PostMapping("/sendByTemplate")
    public CaptchaResult sendByTemplate(@RequestBody SchisandraCaptchaDTO schisandraCaptchaDTO) {

        if (schisandraCaptchaDTO.getToken() == null || schisandraCaptchaDTO.getDeg() == null || schisandraCaptchaDTO.getPhone() == null) {
            return CaptchaResult.fail("验证失败！");
        }
        String token = schisandraCaptchaDTO.getToken();
        String phone = schisandraCaptchaDTO.getPhone();
        Double deg = schisandraCaptchaDTO.getDeg();
        CaptchaResult captchaResult = checkRouteCaptcha.checkCaptcha(token, deg);
        if (captchaResult.getCode() == 0) {
            String prefix = redisUtil.buildKey(AUTH_PHONE_PREFIX, phone);
            String code = SmsUtils.getRandomInt(4);
            if (!redisUtil.exist(prefix)) {
                String key = redisUtil.buildKey(SMS_CONFIG_PREFIX, SmsConfigContext.SMS_CONFIG_KEY);
                String configId = redisUtil.get(key);
                if (configId == null) {
                    log.error("短信配置不存在！");
                    CaptchaResult.fail("短信接口异常！");
                }
                SmsResponse smsResponse = SmsFactory.getSmsBlend(configId).sendMessage(phone, code);
                if (smsResponse.isSuccess()) {
                    redisUtil.setNx(prefix, code, 60L * 5, SECONDS);
                    return CaptchaResult.ok("短信发送成功，5 分钟内有效，请注意查收！");
                } else {
                    return CaptchaResult.fail("短信发送失败，请重试！");
                }
            } else {
                return CaptchaResult.ok("短信已发送，请注意查看或稍后重试！");
            }
        } else {
            return CaptchaResult.fail("验证失败！");
        }

    }

}
