package com.dora.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.resource.PathResourceResolver;

import java.io.IOException;

/**
 * Web配置类
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    private static final String STATIC_RESOURCE_LOCATION = "classpath:/static/";
    
    private final ApiKeyInterceptor apiKeyInterceptor;

    public WebConfig(ApiKeyInterceptor apiKeyInterceptor) {
        this.apiKeyInterceptor = apiKeyInterceptor;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册API密钥拦截器，只对特定的API路径生效
        registry.addInterceptor(apiKeyInterceptor)
                .addPathPatterns("/user/v1/external/**")  // 外部API接口路径
                .excludePathPatterns("/user/v1/api-key/**"); // 排除API密钥管理接口
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置静态资源处理
        registry.addResourceHandler("/static/**")
                .addResourceLocations(STATIC_RESOURCE_LOCATION);
        
        // 配置Swagger UI资源
        registry.addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/springdoc-openapi-ui/");
        
        // 配置API文档资源
        registry.addResourceHandler("/v3/api-docs/**")
                .addResourceLocations("classpath:/META-INF/resources/");
        
        // 配置根目录下的静态文件
        registry.addResourceHandler("/*.html", "/*.js", "/*.css", "/*.ico", "/*.png", "/*.jpg", "/*.jpeg", "/*.gif")
                .addResourceLocations(STATIC_RESOURCE_LOCATION);
        
        // 配置SPA路由fallback - 对于所有非API请求，返回index.html
        registry.addResourceHandler("/**")
                .addResourceLocations(STATIC_RESOURCE_LOCATION)
                .resourceChain(true)
                .addResolver(new PathResourceResolver() {
                    @Override
                    protected Resource getResource(String resourcePath, Resource location) throws IOException {
                        Resource requestedResource = location.createRelative(resourcePath);
                        
                        // 如果请求的资源存在，直接返回
                        if (requestedResource.exists() && requestedResource.isReadable()) {
                            return requestedResource;
                        }
                        
                        // 排除API请求路径，避免影响API响应
                        if (resourcePath.startsWith("user/") || 
                            resourcePath.startsWith("admin/") || 
                            resourcePath.startsWith("api/") ||
                            resourcePath.startsWith("swagger-ui/") ||
                            resourcePath.startsWith("v3/api-docs/") ||
                            resourcePath.startsWith("webjars/")) {
                            return null; // 让API请求正常处理，不返回index.html
                        }
                        
                        // 对于前端路由（非API请求），返回index.html
                        return new ClassPathResource("/static/index.html");
                    }
                });
        
        // 排除管理端上传API路径，不让它被当作静态资源处理
        // 这样 /admin/upload/** 路径就不会被静态资源处理器拦截
    }
} 