package com.tashow.cloud.app.mq.config;
import com.tashow.cloud.app.mq.interceptor.BuriedPointInterceptor;
import com.tashow.cloud.app.mq.message.BuriedMessages;
import com.tashow.cloud.app.mq.producer.buriedPoint.BuriedPointProducer;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import lombok.extern.slf4j.Slf4j;
import jakarta.annotation.PostConstruct;

/**
 * 埋点功能配置类
 */
@Slf4j
@Configuration
@RequiredArgsConstructor
public class BuriedPointConfiguration implements WebMvcConfigurer {

    private final BuriedPointProducer buriedPointProducer;
    private final RabbitTemplate rabbitTemplate;

    /**
     * RabbitTemplate初始化配置
     * 确保回调正确配置，以实现消息可靠性
     */
//    @PostConstruct
//    public void initRabbitTemplate() {
//        log.info("[埋点配置] 初始化RabbitTemplate: {}", rabbitTemplate);
//        rabbitTemplate.setMandatory(true);
//        rabbitTemplate.setReturnsCallback(returned -> {
//            log.error("[埋点配置] 消息路由失败: exchange={}, routingKey={}, replyCode={}, replyText={}, message={}",
//                    returned.getExchange(),
//                    returned.getRoutingKey(),
//                    returned.getReplyCode(),
//                    returned.getReplyText(),
//                    new String(returned.getMessage().getBody()));
//        });
//        rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
//            if (ack) {
//                log.debug("[埋点配置] 消息成功发送到交换机: {}", correlationData);
//            } else {
//                log.error("[埋点配置] 消息发送到交换机失败: cause={}, correlationData={}", cause, correlationData);
//            }
//        });
//
//        // 验证配置
//        if (rabbitTemplate.isConfirmListener()) {
//            log.info("[埋点配置] 确认回调已正确配置");
//        } else {
//            log.error("[埋点配置] 确认回调配置失败，请检查RabbitMQ配置！");
//        }
//    }

    /**
     * 创建埋点队列
     */
    @Bean
    public Queue buriedPointQueue() {
        return new Queue(BuriedMessages.QUEUE, true, false, false);
    }

    /**
     * 创建埋点交换机
     */
    @Bean
    public DirectExchange buriedPointExchange() {
        return new DirectExchange(BuriedMessages.EXCHANGE, true, false);
    }

    /**
     * 创建埋点绑定关系
     */
    @Bean
    public Binding buriedPointBinding() {
        return BindingBuilder.bind(buriedPointQueue())
                .to(buriedPointExchange())
                .with(BuriedMessages.ROUTING_KEY);
    }

    /**
     * 创建埋点拦截器
     */
    @Bean
    public BuriedPointInterceptor buriedPointInterceptor() {
        return new BuriedPointInterceptor(buriedPointProducer);
    }

    /**
     * 注册埋点拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册拦截器，拦截所有请求
        registry.addInterceptor(buriedPointInterceptor())
                // 可以根据需要添加或排除特定路径
                .addPathPatterns("/**")
                // 排除静态资源、Swagger等路径
                .excludePathPatterns(
                        "/swagger-ui/**",
                        "/swagger-resources/**",
                        "/v3/api-docs/**",
                        "/webjars/**",
                        "/static/**",
                        "/error"
                );
    }
} 