package com.sino.mci.send.mq;

import com.sino.mci.mq.config.MqConfig;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RequiredArgsConstructor
public class SendMessageProducer {

    private final RabbitTemplate rabbitTemplate;

    public void publishSendMessage(SendRecordMessage message) {
        log.info("发布异步发送消息: recordId={}, taskId={}", message.getRecordId(), message.getTaskId());
        rabbitTemplate.convertAndSend(MqConfig.SEND_EXCHANGE, MqConfig.SEND_ROUTING_KEY, message);
    }

    public void publishRetryMessage(SendRecordMessage message, int delaySeconds) {
        long delayMillis = delaySeconds * 1000L;
        log.info("发布重试消息: recordId={}, attempt={}, delay={}ms", message.getRecordId(), message.getAttempt(), delayMillis);
        rabbitTemplate.convertAndSend(MqConfig.SEND_DLX_EXCHANGE, MqConfig.SEND_DLX_ROUTING_KEY, message, msg -> {
            msg.getMessageProperties().setExpiration(String.valueOf(delayMillis));
            return msg;
        });
    }
}
