package com.cusc.nirvana.user.rnr.fp.controller;

import com.alibaba.fastjson.JSON;
import com.cusc.nirvana.common.result.Response;
import com.cusc.nirvana.user.exception.CuscUserException;
import com.cusc.nirvana.user.rnr.fp.common.ResponseCode;
import com.cusc.nirvana.user.rnr.fp.dto.SmsRequestDTO;
import com.cusc.nirvana.user.rnr.fp.dto.SmsResponseDTO;
import com.cusc.nirvana.user.rnr.fp.service.ShortMessageCheckService;
import com.cusc.nirvana.user.util.CuscStringUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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;

@Slf4j
@RestController
@RequestMapping("/check")
public class ShortMessageCheckController {

    @Autowired
    private ShortMessageCheckService shortMessageCheckService;

    @PostMapping("/sendSms")
    @ApiOperation(value = "发送短信验证码", notes = "发送短信验证码")
    public Response<SmsResponseDTO> sendSmsCaptcha(@RequestBody SmsRequestDTO bean) {
        if (bean == null || CuscStringUtils.isEmpty(bean.getPhone()) || CuscStringUtils.isEmpty(bean.getBizType())) {
            return Response.createError(ResponseCode.PARAMETER_NULL.getMsg(),
                    ResponseCode.PARAMETER_NULL.getCode());
        }
        //其他参数配置
        if (CuscStringUtils.isEmpty(bean.getTenantNo())) {
            throw new CuscUserException(ResponseCode.APPLICATION_TENANT_REQUIRED.getCode(),
                    ResponseCode.APPLICATION_TENANT_REQUIRED.getMsg());
        }

        log.info("sendSmsCaptcha request url: /user-auth/login/sendSmsCaptcha , param: {}",
                JSON.toJSON(bean.getPhone()));
        return shortMessageCheckService.sendSmsCaptcha(bean);
    }

    @PostMapping("/checkSms")
    @ApiOperation(value = "验证短信验证码", notes = "验证短信验证码")
    public Response checkSmsCaptcha(@RequestBody SmsRequestDTO bean) {
        return shortMessageCheckService.checkSmsCaptcha(bean);
    }
}
