package com.ai.da.common.RabbitMQ;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MQConfig {

    @Autowired
    private RabbitMQProperties rabbitMQProperties;

    @Bean
    public Queue generateQueue() {
        return new Queue(rabbitMQProperties.getQueues().getGenerate());
    }

    @Bean
    public Queue SRQueue() {
        return new Queue(rabbitMQProperties.getQueues().getSr());
    }

    @Bean
    public Queue SRResultQueue() {
        return new Queue(rabbitMQProperties.getQueues().getSrResult());
    }

    @Bean
    public Queue generateResultQueue() {
        return new Queue(rabbitMQProperties.getQueues().getGenerateResult());
    }

    @Bean
    public Queue toProductImageResultQueue() {
        return new Queue(rabbitMQProperties.getQueues().getToProductImageResult());
    }

    @Bean
    public Queue relightResultQueue() {
        return new Queue(rabbitMQProperties.getQueues().getRelightResult());
    }

    @Bean
    public Queue poseTransformQueue() {
        return new Queue(rabbitMQProperties.getQueues().getPoseTransform());
    }

    @Bean
    public Queue mailRetryQueue() {
        // 普通队列，不绑定DLX（首次失败后才进入MQ）
        // durable 持久化队列
        return QueueBuilder.durable(rabbitMQProperties.getQueues().getEmailRetry())
                // 关键参数：绑定死信交换机
                .withArgument("x-dead-letter-exchange", rabbitMQProperties.getDeadLetter().getExchange())
                // 可选：指定死信路由键（默认使用原消息的路由键）
                .withArgument("x-dead-letter-routing-key", rabbitMQProperties.getDeadLetter().getRoutingKey())
                .build();
    }

    // 新增死信交换机
    @Bean
    public DirectExchange deadLetterExchange() {
        return new DirectExchange(rabbitMQProperties.getDeadLetter().getExchange());
    }

    // 新增死信队列
    @Bean
    public Queue deadLetterQueue() {
        return QueueBuilder.durable(rabbitMQProperties.getDeadLetter().getQueue()).build();
    }

    // 绑定死信队列
    @Bean
    public Binding deadLetterBinding() {
        return BindingBuilder.bind(deadLetterQueue())
                .to(deadLetterExchange())
                .with(rabbitMQProperties.getDeadLetter().getRoutingKey());
    }


}
