package com.corewing.app.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import com.corewing.app.common.page.PageInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Sa-Token 配置类
 */
@Configuration
public class SaTokenConfig implements WebMvcConfigurer {

    /**
     * 注册 Sa-Token 拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 分页参数拦截
        registry.addInterceptor(new PageInterceptor());
        // 注册 Sa-Token 拦截器，校验规则为 StpUtil.checkLogin() 登录校验。
        registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin()))
                // 拦截所有路由
                .addPathPatterns("/**")
                // 排除登录、注册、发送验证码, 忘记密码接口
                .excludePathPatterns("/user/login", "/user/register", "/user/sendCode", "/user/forgetPassword", "/user/codeLogin")
                // 排除后台管理登录接口
                .excludePathPatterns("/sys/user/login")
                //排除QC接口
                .excludePathPatterns("/api/qc/**")
                // 排除反馈接口（支持匿名提交）
                .excludePathPatterns("/feedback", "/feedback/**")
                // 排除教程接口（支持匿名查询）
                .excludePathPatterns("/tutorial", "/tutorial/**")
                // 排除隐私政策接口（支持匿名查询）
                .excludePathPatterns("/privacy_policy", "/privacy_policy/**")
                // 排除固件查询接口（不需要登录）
                .excludePathPatterns("/firmware/**")
                // 排除AAT固件版本接口
                .excludePathPatterns("/aat_version/**")
                // 排除Sirius固件接口
                .excludePathPatterns("/sirius_firmware/**")
                // 排除公共固件
                .excludePathPatterns("/public_firmware/**")
                // 排除系统登录页（不需要登录）
//                .excludePathPatterns("/loading.html", "/admin/login.html")
                // 排除静态资源
                .excludePathPatterns("/", "/*.html", "/*.css", "/*.js", "/*.ico", "/static/**", "/assets/**")
                // 排除接口静态资源
                .excludePathPatterns("/doc.html", "/webjars/**")
                .excludePathPatterns("/v3/api-docs/swagger-config", "/v3/api-docs/**", "/swagger-resources")
                // 排除 Druid 监控
                .excludePathPatterns("/druid/**")
                // 排除错误页面
                .excludePathPatterns("/error", "/error/**")
                // 排除校验更新接口
                .excludePathPatterns("/app_version/**")
                // 排除获取最新app接口
                .excludePathPatterns("/api/app", "/api/app/getAppVersion")
                .excludePathPatterns("/api/website/**")
                .excludePathPatterns("/api/app/access_statistics/accumulate")
                .excludePathPatterns("/api/app/resource_category/**")
                .excludePathPatterns("/api/app/resource/**")
                .excludePathPatterns("/api/app/device/factoryActivation")
                // 排除模型接口
                .excludePathPatterns("/model/page", "/model/list", "/model/detail/**", "/model/category/**")
                // 排除咨询接口
                .excludePathPatterns("/contactMsg", "/contactMsg/**");
    }

    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/META-INF/resources/webjars/");
    }

}
