package com.njcn.msgpush.module.push.sms;

import com.alibaba.fastjson.JSON;
import com.njcn.msgpush.module.push.client.factory.MessageProviderFactory;
import com.njcn.msgpush.module.push.client.factory.MessageProviderFactoryRegistry;
import com.njcn.msgpush.module.push.client.sender.Sender;
import com.njcn.msgpush.module.push.client.sender.SmsSender;
import com.njcn.msgpush.module.push.controller.admin.message.vo.MessageRecordReqVO;
import com.njcn.msgpush.module.push.controller.admin.message.vo.MessageSendResultVO;
import com.njcn.msgpush.module.push.dal.dataobject.channel.ChannelProviderConfigDO;
import com.njcn.msgpush.module.push.enums.ChannelTypeEnum;
import com.njcn.msgpush.module.push.enums.ProviderTypeEnum;
import com.njcn.msgpush.module.push.service.message.MessageRecordService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author caozehui
 * @data 2026-02-06
 */
@Slf4j
@SpringBootTest
@EnableScheduling
public class MsgPushClientTest {

    @Autowired
    private MessageRecordService messageRecordService;

    @Autowired
    private MessageProviderFactoryRegistry messageProviderFactoryRegistry;

    @Autowired
    private Sender sender;

    @Test
    public void testSend() throws InterruptedException {
        List<MessageRecordReqVO> messageIdList = new ArrayList<>();
//        for (int i = 0; i < 5; i++) {
//            MessageRecordReqVO message = new MessageRecordReqVO();
//            message.setMessageId(i + "c518e2a3c");
//            message.setAppName("NPQS-9500");
//            message.setChannel(MsgPushConstant.CHANNEL_SMS);
//            message.setTitle("灿能云");
//            message.setReceiver("18839431215");
//            message.setTemplateCode("SMS_481710295");
//            message.setTemplateParams("{\"code\":\"65432" + i + "\"}");
//            message.setProviderType(MsgPushConstant.PROVIDER_TYPE_ALI_YUN);
//            messageIdList.add(message);
//        }
//        for (int i = 0; i < 1; i++) {
//            MessageRecordReqVO message = new MessageRecordReqVO();
//            message.setId(1234567890L);
//            message.setChannel(ChannelTypeEnum.SMS.getMsg());
//            message.setReceiver("18839431215");
//            message.setContent("【南京灿能电力】测试短信" + i + "，请忽略。" + LocalDateTime.now().format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//            messageIdList.add(message);
//        }
//        List<MessageSendResultVO> sendResult = messageRecordService.send(messageIdList, ChannelTypeEnum.SMS);
//
//        Thread.sleep(9000);
//        System.out.println(JSON.toJSONString(sendResult));
    }

    @Test
    public void templateSelect() {
        MessageProviderFactory messageProviderFactory = messageProviderFactoryRegistry.getFactory(ProviderTypeEnum.TELECOM.getCode());
        ChannelProviderConfigDO config = new ChannelProviderConfigDO();
        config.setApiUrl("https://sms.ymeeting.cn/smsv2");
        config.setAppKey("925631");
        config.setAppSecret("AMW2pOVrdky");
        config.setExtno("106905631");
        SmsSender smsSender = messageProviderFactory.createSmsSender(config, sender);
        String templateIdentifier = "SMS_481710295";
        smsSender.queryTemplate(templateIdentifier);
    }
}
