package com.izouma.dingdong.web;

import com.izouma.dingdong.config.Constants;
import com.izouma.dingdong.exception.BusinessException;
import com.izouma.dingdong.service.sms.SmsService;
import com.izouma.dingdong.service.sms.TencentSmsService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.regex.Pattern;

@RestController
@RequestMapping("/sms")
@AllArgsConstructor
public class SmsController {
    private SmsService        smsService;
    private TencentSmsService tencentSmsService;

    @GetMapping("/sendVerify")
    public String sendVerify(@RequestParam String phone) {
        if (!Pattern.matches(Constants.Regex.PHONE, phone)) {
            throw new BusinessException("请输入正确的手机号");
        }
        return smsService.sendVerify(phone);
    }

    @GetMapping("/verify")
    public void verify(@RequestParam String phone, @RequestParam String code) throws SmsService.SmsVerifyException {
        smsService.verify(phone, code);
    }

    /**
     * 腾讯发送短信
     *
     * @param phone      需要加+86
     * @param templateId 短信模板
     *                   676939 -修改手机号
     *                   674906 -注册
     *                   674851 -通用
     *                   674832 -忘记密码
     *                   -----国际----
     *                   674908 -通用
     *                   674903 -注册
     * @return sessionId
     */
    @GetMapping("/tencentSend")
    public String tencentSend(@RequestParam String phone, String templateId) {
        if (!Pattern.matches(Constants.Regex.PHONE2, phone)) {
            throw new BusinessException("请输入正确的手机号");
        }
        return tencentSmsService.sendVerify(phone, templateId);
    }
}
