package com.njcn.user.controller.message;

import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.user.pojo.vo.app.MessageRecordReqVO;
import com.njcn.user.service.ISmsSendService;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author xy
 */
@Slf4j
@RestController
@RequestMapping("/sms")
@Api(tags = "短信发送管理")
@AllArgsConstructor
public class SmsSendController extends BaseController {

    private final ISmsSendService smsSendService;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/send")
    @ApiOperation("发送短信（同步，包含重试）")
    public HttpResult<String> sendSms(@RequestBody MessageRecordReqVO vo) {
        String methodDescribe = getMethodDescribe("sendSms");

        try {
            smsSendService.sendSmsWithRetry(vo);
            return HttpResultUtil.assembleCommonResponseResult(
                    CommonResponseEnum.SUCCESS,
                    "短信发送成功",
                    methodDescribe
            );
        } catch (Exception e) {
            log.error("短信发送失败", e);
            return HttpResultUtil.assembleCommonResponseResult(
                    CommonResponseEnum.FAIL,
                    e.getMessage(),
                    methodDescribe
            );
        }
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/send/simple")
    @ApiOperation("发送短信（简化参数）")
    public HttpResult<String> sendSmsSimple(
            @RequestParam String receiver,
            @RequestParam String content,
            @RequestParam(defaultValue = "verify_code") String messageType) {
        String methodDescribe = getMethodDescribe("sendSmsSimple");

        try {
            smsSendService.sendSmsWithRetry(receiver, content, messageType);
            return HttpResultUtil.assembleCommonResponseResult(
                    CommonResponseEnum.SUCCESS,
                    "短信发送成功",
                    methodDescribe
            );
        } catch (Exception e) {
            log.error("短信发送失败", e);
            return HttpResultUtil.assembleCommonResponseResult(
                    CommonResponseEnum.FAIL,
                    e.getMessage(),
                    methodDescribe
            );
        }
    }
}
