package com.simuwang.deploy.config;

import com.simuwang.base.config.DaqProperties;
import com.simuwang.deploy.components.LoggingAspect;
import com.simuwang.deploy.components.RequestIdInterceptor;
import com.simuwang.logging.LoggingService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
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;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    private final LoggingService loggingService;

    public WebConfig(LoggingService loggingService) {
        this.loggingService = loggingService;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new RequestIdInterceptor()).addPathPatterns("/**");
    }

    @Bean
    @ConditionalOnProperty(prefix = DaqProperties.DAQ_CONFIG_PREFIX, name = "enable-logging", havingValue = "true")
    public LoggingAspect loggingAspect() {
        return new LoggingAspect(this.loggingService);
    }
}
