package com.wteam.controller.sms;

import com.wteam.framework.common.cache.limit.annotation.LimitPoint;
import com.wteam.framework.common.enums.ResultCode;
import com.wteam.framework.common.enums.ResultUtil;
import com.wteam.framework.common.exception.ServiceException;
import com.wteam.framework.common.vo.ResultMessage;
import com.wteam.framework.modules.sms.SmsUtil;
import com.wteam.framework.modules.sms.entity.dto.MemberSmsLoginDto;
import com.wteam.framework.modules.sms.service.SmsCheckService;
import com.wteam.framework.modules.verification.entity.enums.VerificationEnums;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.nfunk.jep.function.Sum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * @author doncic
 * @date 2023/9/16 17:18
 */
@Slf4j
@Tag(name = "短信相关接口管理")
@RestController
@RequestMapping("/v1/sms")
public class GetSmsController {

    @Autowired
    private SmsUtil smsUtil;

    @Autowired
    private SmsCheckService smsCheckService;

    /**
     * 发送短信验证码接口
     * hong
     *
     * @param uuid
     * @param mobile
     * @param verificationEnums
     * @return
     */
    @LimitPoint(name = "sms_send", key = "sms")
    @Parameters({
            @Parameter(in = ParameterIn.PATH, schema = @Schema(type = "string"), name = "mobile", description = "手机号"),
            @Parameter(in = ParameterIn.HEADER, schema = @Schema(type = "string"), name = "uuid", description = "uuid"),
    })
    @GetMapping("/{verificationEnums}/{mobile}")
    @Operation(summary = "发送短信验证码,一分钟同一个ip请求1次")
    public ResultMessage<MemberSmsLoginDto> getSmsCode(
            @RequestHeader String uuid,
            @PathVariable String mobile,
            @PathVariable VerificationEnums verificationEnums) {
        smsUtil.sendSmsCode(mobile, verificationEnums, uuid);
        return ResultUtil.success(ResultCode.VERIFICATION_SEND_SUCCESS);
    }

    /**
     * 根据验证码认证手机号
     *
     * @return
     */
    @Operation(summary = "校验短信是否正确")
    @PostMapping("/checkSms")
    @Parameters({
            @Parameter(name = "mobile", description = "手机号", required = true, in = ParameterIn.QUERY),
            @Parameter(name = "code", description = "验证码", required = true, in = ParameterIn.QUERY)
    })
    public ResultMessage checkSms(@NotNull @RequestParam String mobile,
                                  @NotNull @RequestParam String code,
                                  @RequestHeader String uuid) {
        if (smsUtil.verifyCode(mobile, VerificationEnums.LOGIN, uuid, code)) {
            return ResultUtil.success();
        } else {
            throw new ServiceException(ResultCode.VERIFICATION_SMS_CHECKED_ERROR);
        }
    }
}
