package com.njcn.msgpush.module.push.client.channel.sms.Impl;

import cn.hutool.core.util.StrUtil;
import com.njcn.msgpush.module.push.client.channel.sms.SmsClient;
import com.njcn.msgpush.module.push.client.setting.sms.TelecomSmsSetting;
import com.njcn.msgpush.module.push.util.RestTemplateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author caozehui
 * @data 2026-02-11
 * @description 电信e企云短信服务实现
 */
@Slf4j
@Service
public class TelecomSmsClient implements SmsClient {

    /**
     * 短信接口地址
     */
    private static final String API_URL = "https://sms.ymeeting.cn/smsv2";
    /**
     * 虚拟接入码
     */
    private static final String ACCESS_CODE = "106905631";
    /**
     * 短信接口内容类型
     */
    private static final String CONTENT_TYPE = "application/json;charset=utf-8";

    //    public static final String ACCOUNT = "account";
//    public static final String PASSWORD = "password";
    public static final String CONTENT = "content";

    @Autowired
    private TelecomSmsSetting telecomSmsSetting;

    @Autowired
    private RestTemplateUtil restTemplateUtil;

    @Override
    public boolean sendSms(Map<String, Object> params, String phoneNumber) throws Exception {
        return this.sendBatchSms(params, List.of(phoneNumber));
    }

    @Override
    public boolean sendBatchSms(Map<String, Object> params, List<String> phoneNumbers) {
        // 构建请求参数
        Map<String, Object> request = new HashMap<>();
        request.put("action", "send");
        request.put("account", telecomSmsSetting.getAccount());
        request.put("password", telecomSmsSetting.getPassword());
        request.put("mobile", StrUtil.join(StrUtil.COMMA, phoneNumbers));
        request.put("content", params.get(CONTENT).toString());
        request.put("extno", ACCESS_CODE);

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Type", CONTENT_TYPE);

        // 发送请求
        ResponseEntity<String> response = restTemplateUtil.post(
                API_URL,
                request,
                headers,
                String.class
        );
        String body = response.getBody();

        if (body.contains("\"status\": 0")) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void querySmsTemplateList() {

    }
}
