package com.tashow.cloud.app.mq.config;

import com.tashow.cloud.app.interceptor.BuriedPointInterceptor;
import com.tashow.cloud.app.mq.message.BuriedMessages;
import com.tashow.cloud.app.mq.producer.buriedPoint.BuriedPointProducer;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
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;

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

    private final BuriedPointProducer buriedPointProducer;

    /**
     * 创建埋点队列
     */
    @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/**",
                        "/card",
                        "/error"
                );
    }
}