package com.backendsys.modules.sms.controller;

import com.backendsys.exception.CustException;
import com.backendsys.modules.common.config.security.annotations.Anonymous;
import com.backendsys.modules.common.utils.Result;
import com.backendsys.modules.sms.entity.Sms;
import com.backendsys.modules.sms.service.SmsService;
import com.backendsys.modules.sdk.tencentcloud.sms.service.TencentSmsService;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.regex.Pattern;

@Validated
@RestController
public class SmsController {

    @Autowired
    private SmsService smsService;

    @Autowired
    private TencentSmsService tencentSmsService;

    /**
     * 发送短信
     */
    @Anonymous
    @PostMapping("/api/system/sms/getSMS")
    public Result getSMS(@Validated(Sms.Send.class) @RequestBody Sms sms) throws TencentCloudSDKException {

        // 当区号为86时，校验手机号码
        if (sms.getPhone_area_code() == 86) {
            if (!Pattern.matches("^1[3-9]\\d{9}$", sms.getPhone())) {
                throw new CustException("手机号码格式不正确");
            }
        }

        return Result.success().put("data", smsService.sendValidCode(sms));
    }

    /**
     * [腾讯云] 获得统计短信发送数据
     */
    @GetMapping("/api/system/sms/getSMSStatistics")
    public Result getSMSStatistics(String beginTime, String endTime) throws TencentCloudSDKException {
        return Result.success().put("data", tencentSmsService.getSMSStatistics(beginTime, endTime));
    }

    /**
     * [腾讯云] 获得回执数据统计
     */
    @GetMapping("/api/system/sms/getSMSCallbackStatistics")
    public Result getSMSCallbackStatistics(String beginTime, String endTime) throws TencentCloudSDKException {
        return Result.success().put("data", tencentSmsService.getSMSCallbackStatistics(beginTime, endTime));
    }

}
