package com.haha.admin.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;
import java.util.List;

@Slf4j
@Configuration
public class SaTokenConfig implements WebMvcConfigurer {

    private static final List<String> EXCLUDE_PATHS = Arrays.asList(
        "/login/**",
        "/replenisher/login/**",
        "/health/**",
        "/static/**",
        "/favicon.ico",
        "/logo.svg",
        "/platform-config.json",
        "/index.html",
        "/audio/**",
        "/html/**",
        "/wasm/**",
        "/",
        "/error"
    );

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SaInterceptor(handle -> {
            String tokenValue = StpUtil.getTokenValue();
            if (tokenValue != null && !tokenValue.isEmpty()) {
                log.debug("当前请求token: {}, 是否登录: {}", tokenValue, StpUtil.isLogin());
            } else {
                log.debug("当前请求未携带token");
            }
            StpUtil.checkLogin();
        }))
        .addPathPatterns("/**")
        .excludePathPatterns(EXCLUDE_PATHS);
    }
}
