package com.spot.config;

import com.spot.interceptor.AuthInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * Spring MVC 配置，注册全局拦截器
 *
 * @author jean
 * @date 2026-05-06
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Resource
    private AuthInterceptor authInterceptor;

    /**
     * 注册鉴权拦截器，拦截所有接口
     * 如需新增免登录接口，在 excludePathPatterns 中追加路径
     *
     * @param registry 拦截器注册表
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("*")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(3600);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(
                        // Swagger
                        "/v2/api-docs",
                        "/swagger-resources",
                        "/swagger-resources/**",
                        "/swagger-ui.html",
                        "/webjars/**",
                        // 系统
                        "/error",
                        // 公开配置接口（无需登录）
                        "/v1/config/**",
                        // 管理后台内部接口（由 doge admin 服务端调用，不携带用户 session）
                        "/v1/admin/**"
                );
    }
}
