package com.njcn.user.util;

import com.njcn.user.constant.SmsConstant;
import com.njcn.web.utils.RestTemplateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 短信接口管理静态工具类
 * 统一管理各种短信接口操作
 * 
 * @date 2025-08-25
 */
@Component
public class SmsApiUtil {
    
    @Resource
    private RestTemplateUtil restTemplateUtil;

    private static final Logger log = LoggerFactory.getLogger(SmsApiUtil.class);

    
    /**
     * 群发短信接口
     * 
     * @param mobiles 手机号列表，多个手机号用逗号分隔
     * @param content 短信内容
     * @return 发送结果
     */
    public  ResponseEntity<String> sendBatchSms(String mobiles, String content) {
        return sendBatchSms(SmsConstant.SMS_ACCOUNT, SmsConstant.SMS_PASSWORD, mobiles, content);
    }
    
    /**
     * 群发短信接口
     * 
     * @param account 账号
     * @param password 密码
     * @param mobiles 手机号列表，多个手机号用逗号分隔
     * @param content 短信内容
     * @return 发送结果
     */
    public  ResponseEntity<String> sendBatchSms(String account, String password, String mobiles, String content) {
        try {
            String[] mobileArray = mobiles.split(",");
            log.info("开始群发短信，手机号数量: {}, 扩展号码: {}", mobileArray.length, SmsConstant.SMS_ACCESS_CODE);
            
            // 构建请求参数
            Map<String, Object> request = new HashMap<>();
            request.put("action", "send");
            request.put("account", account);
            request.put("password", password);
            request.put("mobile", mobiles);
            request.put("content", content);
            request.put("extno", SmsConstant.SMS_ACCESS_CODE);
            
            // 设置请求头
            HttpHeaders headers = new HttpHeaders();
            headers.set("Content-Type", SmsConstant.SMS_CONTENT_TYPE);
            
            // 发送请求
            ResponseEntity<String> response = restTemplateUtil.post(
                SmsConstant.SMS_API_URL,
                request,
                headers,
                String.class
            );
            
            log.info("群发短信完成，响应状态: {}, 响应内容: {}", 
                response.getStatusCode(), response.getBody());
            
            return response;
            
        } catch (Exception e) {
            log.error("群发短信失败: {}", e.getMessage(), e);
            throw new RuntimeException("群发短信失败", e);
        }
    }
    

    /**
     * 消息状态报告查询接口
     * 
     * @param size 查询数量，建议不超过1000
     * @return 状态报告查询结果
     */
    public  ResponseEntity<String> messageReport(Integer size) {
        return messageReport(SmsConstant.SMS_ACCOUNT, SmsConstant.SMS_PASSWORD, size);
    }
    
    /**
     * 消息状态报告查询接口
     * 
     * @param account 账号
     * @param password 密码
     * @param size 查询数量，建议不超过1000
     * @return 状态报告查询结果
     */
    public  ResponseEntity<String> messageReport(String account, String password, Integer size) {
        try {
            log.info("开始查询消息状态报告，查询数量: {}", size);
            
            // 构建请求参数
            Map<String, Object> request = new HashMap<>();
            request.put("action", "report");
            request.put("account", account);
            request.put("password", password);
            request.put("size", size != null ? size : 1000);
            
            // 设置请求头
            HttpHeaders headers = new HttpHeaders();
            headers.set("Content-Type", SmsConstant.SMS_CONTENT_TYPE);
            
            // 发送请求
            ResponseEntity<String> response = restTemplateUtil.post(
                SmsConstant.SMS_API_URL,
                request,
                headers,
                String.class
            );
            
            log.info("消息状态报告查询完成，响应状态: {}, 响应内容: {}", 
                response.getStatusCode(), response.getBody());
            
            return response;
            
        } catch (Exception e) {
            log.error("消息状态报告查询失败: {}", e.getMessage(), e);
            throw new RuntimeException("消息状态报告查询失败", e);
        }
    }
}