package com.cf.imes.module.system.mq.producer.advertisement;

import com.cf.imes.module.system.mq.message.advertisement.AdvertisementSendMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.web.context.request.RequestContextHolder;

import jakarta.annotation.Resource;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.UUID;

import static com.cf.imes.framework.mq.rabbitmq.constant.RabbitMqConstants.SYSTEM_ADVERTISEMENT_UPDATE_DELAYED_EXCHANGE;
import static com.cf.imes.framework.mq.rabbitmq.constant.RabbitMqConstants.SYSTEM_ADVERTISEMENT_UPDATE_DELAYED_ROUTING_KEY;

/**
 * Advertisement 广告相关消息的 Producer
 *
 */
@Slf4j
@Component
public class AdvertisementProducer {

    @Resource
    private RabbitTemplate rabbitTemplate;


    /**
     * 发送 {@link AdvertisementSendMessage} 消息
     *
     * @param adId         广告id
     * @param action       类型
     * @param triggerTime        到期时间
     */

    public String sendAdvertisementSendMessage(Long adId, String action, LocalDate triggerTime) {

        String messageId = UUID.randomUUID().toString();


        long delay = Duration.between(LocalDateTime.now(), triggerTime).toMillis();

        if (delay > 0) {
            RequestContextHolder.setRequestAttributes(RequestContextHolder.getRequestAttributes(), true);
            AdvertisementSendMessage message = new AdvertisementSendMessage(adId, messageId, action);

            if (TransactionSynchronizationManager.isActualTransactionActive()) {
                TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
                    @Override
                    public void afterCommit() {
                        // 数据库操作全部完成发送消息
                        rabbitTemplate.convertAndSend(
                                SYSTEM_ADVERTISEMENT_UPDATE_DELAYED_EXCHANGE,
                                SYSTEM_ADVERTISEMENT_UPDATE_DELAYED_ROUTING_KEY,
                                message,
                                correlationData -> {
                                    correlationData.getMessageProperties().setMessageId(messageId);
                                    correlationData.getMessageProperties().setDelayLong(Long.valueOf(delay));
                                    return correlationData;
                                });
                    }
                });
            } else {
                // 数据库操作全部完成发送消息
                rabbitTemplate.convertAndSend(
                        SYSTEM_ADVERTISEMENT_UPDATE_DELAYED_EXCHANGE,
                        SYSTEM_ADVERTISEMENT_UPDATE_DELAYED_ROUTING_KEY,
                        message,
                        correlationData -> {
                            correlationData.getMessageProperties().setMessageId(messageId);
                            correlationData.getMessageProperties().setDelayLong(Long.valueOf(delay));
                            return correlationData;
                        });
            }
        }

        return messageId;
    }
}
