package com.cim.idm.config;

import com.cim.idm.filter.TokenAuthenticationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class FilterConfig {

    @Autowired
    private TokenAuthenticationFilter tokenAuthenticationFilter;

    @Value("${filter.exclusions:/webjars,/login/getORG,/api/wms,/login/LoginCheck,/api/menu/getHierarchicalMenuOfPermission,/swagger-ui.html,/v2/api-docs,/swagger-resources}")
    private String exclusionPaths;

    @Value("${filter.urlPatterns:/*}")
    private String urlPatterns;

    @Bean
    public FilterRegistrationBean<TokenAuthenticationFilter> loggingFilter() {
        if (tokenAuthenticationFilter == null) {
            throw new IllegalStateException("TokenAuthenticationFilter cannot be null");
        }

        FilterRegistrationBean<TokenAuthenticationFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(tokenAuthenticationFilter);
        registrationBean.addUrlPatterns(urlPatterns);
        registrationBean.addInitParameter("exclusions", exclusionPaths);

        return registrationBean;
    }
}
