package com.zhentao.config;

import com.zhentao.constant.ChatConstants;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMQ配置类
 * 已禁用 - 如需启用请取消注释并在 application.yml 配置 RabbitMQ
 */
// @Configuration
public class RabbitMQConfig {

    /**
     * 聊天消息交换机（Direct类型）
     */
    @Bean
    public DirectExchange chatExchange() {
        return ExchangeBuilder
                .directExchange(ChatConstants.RabbitMQ.CHAT_EXCHANGE)
                .durable(true)
                .build();
    }

    /**
     * 死信交换机
     */
    @Bean
    public DirectExchange chatDlxExchange() {
        return ExchangeBuilder
                .directExchange(ChatConstants.RabbitMQ.CHAT_DLX_EXCHANGE)
                .durable(true)
                .build();
    }

    /**
     * 聊天消息队列
     */
    @Bean
    public Queue chatQueue() {
        return QueueBuilder
                .durable(ChatConstants.RabbitMQ.CHAT_QUEUE)
                .withArgument("x-dead-letter-exchange", ChatConstants.RabbitMQ.CHAT_DLX_EXCHANGE)
                .withArgument("x-dead-letter-routing-key", ChatConstants.RabbitMQ.CHAT_DLX_ROUTING_KEY)
                .withArgument("x-message-ttl", 300000)  // 消息TTL：5分钟
                .build();
    }

    /**
     * 死信队列
     */
    @Bean
    public Queue chatDlxQueue() {
        return QueueBuilder
                .durable(ChatConstants.RabbitMQ.CHAT_DLX_QUEUE)
                .build();
    }

    /**
     * 绑定聊天队列到交换机
     */
    @Bean
    public Binding chatBinding() {
        return BindingBuilder
                .bind(chatQueue())
                .to(chatExchange())
                .with(ChatConstants.RabbitMQ.CHAT_ROUTING_KEY);
    }

    /**
     * 绑定死信队列到死信交换机
     */
    @Bean
    public Binding chatDlxBinding() {
        return BindingBuilder
                .bind(chatDlxQueue())
                .to(chatDlxExchange())
                .with(ChatConstants.RabbitMQ.CHAT_DLX_ROUTING_KEY);
    }

    /**
     * JSON消息转换器
     */
    @Bean
    public Jackson2JsonMessageConverter jackson2JsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    /**
     * RabbitTemplate配置
     */
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMessageConverter(jackson2JsonMessageConverter());
        
        // 开启发布者确认
        template.setConfirmCallback((correlationData, ack, cause) -> {
            if (!ack) {
                System.err.println("消息发送失败: " + cause);
            }
        });
        
        // 开启发布者返回
        template.setReturnsCallback(returned -> {
            System.err.println("消息被退回: " + returned.getMessage());
        });
        
        return template;
    }

    /**
     * 消息监听器容器工厂配置
     */
    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(
            ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(jackson2JsonMessageConverter());
        factory.setConcurrentConsumers(3);
        factory.setMaxConcurrentConsumers(10);
        factory.setPrefetchCount(10);
        return factory;
    }
}



