package cn.axzo.msg.center.notices.service.v2;

import cn.axzo.foundation.result.ApiResult;
import cn.axzo.foundation.result.ResultCode;
import cn.axzo.msg.center.api.v2.message.feign.SmsApiV2;
import cn.axzo.msg.center.api.v2.message.req.SendSmsReq;
import cn.axzo.msg.center.notices.manager.api.dto.request.MnsRequestDto;
import cn.axzo.msg.center.notices.service.api.MessageService;
import cn.axzo.msg.center.notices.service.impl.MnsType;
import cn.hutool.json.JSONUtil;
import com.alibaba.nacos.common.utils.UuidUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author : zhanghonghao@axzo.cn
 * @since : 2025/1/13
 */
@Service
@Slf4j
@RestController
@RequiredArgsConstructor
public class SmsApiV2Impl implements SmsApiV2 {

    private final MessageService messageService;

    @Override
    public ApiResult<Void> sendSms(SendSmsReq request) {
        MnsRequestDto mnsRequestDto = new MnsRequestDto();
        BeanUtils.copyProperties(request, mnsRequestDto);
        log.info("request value={},mnsRequestDto value={}", JSONUtil.toJsonStr(request), JSONUtil.toJsonStr(mnsRequestDto));
        try {
            if (StringUtils.isBlank(mnsRequestDto.getRequestNo())) {
                mnsRequestDto.setRequestNo(UuidUtils.generateUuid());
            }
            mnsRequestDto.setInternalObj(MnsType.BIZ);
            messageService.sendMessage(mnsRequestDto);
            return ApiResult.success();
        } catch (Exception e) {
            return ApiResult.error(ResultCode.RUNTIME_EXCEPTION.getCode(), e.getMessage());
        }
    }

}
