package com.moka.gdtauto.config;

import com.moka.gdtauto.interceptor.LoginTokenInterceptor;
import com.moka.gdtauto.interceptor.TraceIdInterceptor;
import com.moka.gdtauto.resolver.AuthUserArgumentResolver;
import com.plumelog.core.PlumeLogTraceIdInterceptor;

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * Web MVC 配置
 * 
 * @author moka
 * @since 2026-02-09
 */
@Configuration
@RequiredArgsConstructor
public class WebMvcConfig implements WebMvcConfigurer {

    private final LoginTokenInterceptor loginTokenInterceptor;
    private final TraceIdInterceptor traceIdInterceptor;
    private final AuthUserArgumentResolver authUserArgumentResolver;

    private static final String[] IGNORE_URI = {
        "/api/user/login", 
        "/api/user/user-token-callback",
        "/api/gdt/auth/callback", 
        "/health",
        "/doc.html",
        "/swagger-ui/**",
        "/v3/api-docs/**",
        "/mcp/**"
    };
    /**
     * 添加拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginTokenInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(IGNORE_URI);
        registry.addInterceptor(traceIdInterceptor);
    }

    /**
     * 添加参数解析器
     * 注册 @AuthUser 注解的参数解析器
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(authUserArgumentResolver);
    }
}
