package com.njcn.msgpush.module.push.client.channel.mail.impl;

import com.alibaba.fastjson.JSON;
import com.aliyun.dm20151123.Client;
import com.aliyun.dm20151123.models.QueryMailAddressByParamRequest;
import com.aliyun.dm20151123.models.QueryMailAddressByParamResponse;
import com.aliyun.dm20151123.models.SingleSendMailRequest;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.njcn.msgpush.module.push.client.channel.mail.MailClient;
import com.njcn.msgpush.module.push.client.setting.mail.AliYunMailSetting;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @author caozehui
 * @data 2026-02-11
 */
@Slf4j
@Service
public class AliYunMailClient implements MailClient {
    private static final String ACCOUNT_NAME = "accountName";
    private static final Integer ADDRESS_TYPE = 0;
    private static final String REPLY_TO_ADDRESS = "replyToAddress";
    private static final String HTML_BODY = "htmlBody";
    private static final String TEXT_BODY = "textBody";
    private static final String TO_ADDRESS = "toAddress";
    private static final String SUBJECT = "subject";
    private static final String CLICK_TRACE = "1";

    @Autowired
    private AliYunMailSetting aliYunMailSetting;

    private Client client;

    @PostConstruct
    public void init() {
        Config config = new Config()
                .setAccessKeyId(aliYunMailSetting.getAccessKeyId())
                .setAccessKeySecret(aliYunMailSetting.getAccessKeySecret())
                .setRegionId(aliYunMailSetting.getRegionId())
                .setEndpoint(aliYunMailSetting.getEndpoint());

        try {
            this.client = new Client(config);
        } catch (Exception e) {
            log.error("阿里云-邮件服务初始化失败，请检查配置信息");
            throw new RuntimeException(e);
        }
    }

    @Override
    public void sendMail(String accountName, Integer addressType, Boolean replyToAddress, String toAddress, String subject, String htmlBody) {
        RuntimeOptions runtimeOptions = new RuntimeOptions();
        // 设置自动重试，默认是不开启的。重试次数默认是3次
        runtimeOptions.autoretry = true;
        SingleSendMailRequest request = new SingleSendMailRequest()
                .setAccountName(accountName)
                .setAddressType(addressType)
                .setReplyToAddress(replyToAddress)
                .setToAddress(toAddress)
                .setSubject(subject)
                .setHtmlBody(htmlBody);
        try {
            client.singleSendMailWithOptions(request, runtimeOptions);
        } catch (Exception e) {
            throw new RuntimeException("阿里云-邮件服务发送失败");
        }
    }

    @Override
    public void queryMailAddressByParam() {
        QueryMailAddressByParamRequest queryMailAddressByParamRequest = null;
        try {
            queryMailAddressByParamRequest = QueryMailAddressByParamRequest.build(new HashMap<>());
            QueryMailAddressByParamResponse response = client.queryMailAddressByParam(queryMailAddressByParamRequest);
            System.out.println(JSON.toJSON(response));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
