package org.xyzh.common.auth.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 认证配置属性类
 * 用于配置认证相关的属性，包括白名单路径
 * 
 * @author yslg
 */
@Component
@ConfigurationProperties(prefix = "auth")
public class AuthProperties {
    
    /**
     * 是否启用认证过滤器
     * 默认启用
     */
    private Boolean enabled = true;

    /**
     * 登录接口路径
     * 支持不同服务自定义登录地址
     */
    private String loginPath = "/urban-lifeline/auth/login";

    /**
     * 登出接口路径
     */
    private String logoutPath = "/urban-lifeline/auth/logout";

    /**
     * 验证码获取接口路径
     */
    private String captchaPath = "/urban-lifeline/auth/captcha";

    /**
     * 刷新 Token 接口路径
     */
    private String refreshPath = "/urban-lifeline/auth/refresh";

    /**
     * 通用白名单路径列表（非认证接口）
     * 例如：Swagger、静态资源、健康检查等
     */
    private List<String> whitelist = new ArrayList<>();

    /**
     * Token 请求头名称
     * 默认: Authorization
     */
    private String tokenHeader = "Authorization";

    /**
     * Token 前缀
     * 默认: Bearer 
     */
    private String tokenPrefix = "Bearer ";

    public AuthProperties() {
        // 默认通用白名单：Swagger 及静态资源等
        whitelist.add("/swagger-ui/**");
        whitelist.add("/swagger-ui.html");
        whitelist.add("/v3/api-docs/**");
        whitelist.add("/webjars/**");
        whitelist.add("/favicon.ico");
        whitelist.add("/error");
    }
    
    public Boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public String getLoginPath() {
        return loginPath;
    }

    public void setLoginPath(String loginPath) {
        this.loginPath = loginPath;
    }

    public String getLogoutPath() {
        return logoutPath;
    }

    public void setLogoutPath(String logoutPath) {
        this.logoutPath = logoutPath;
    }

    public String getCaptchaPath() {
        return captchaPath;
    }

    public void setCaptchaPath(String captchaPath) {
        this.captchaPath = captchaPath;
    }

    public String getRefreshPath() {
        return refreshPath;
    }

    public void setRefreshPath(String refreshPath) {
        this.refreshPath = refreshPath;
    }

    public List<String> getWhitelist() {
        return whitelist;
    }

    public void setWhitelist(List<String> whitelist) {
        this.whitelist = whitelist;
    }

    /**
     * 认证相关接口路径集合（login / logout / captcha / refresh）
     * 供 SecurityConfig 和 JwtAuthenticationFilter 统一放行
     */
    public List<String> getAuthPaths() {
        List<String> authPaths = new ArrayList<>();
        if (StringUtils.hasText(loginPath)) {
            authPaths.add(loginPath);
        }
        if (StringUtils.hasText(logoutPath)) {
            authPaths.add(logoutPath);
        }
        if (StringUtils.hasText(captchaPath)) {
            authPaths.add(captchaPath);
        }
        if (StringUtils.hasText(refreshPath)) {
            authPaths.add(refreshPath);
        }
        return authPaths;
    }

    public String getTokenHeader() {
        return tokenHeader;
    }

    public void setTokenHeader(String tokenHeader) {
        this.tokenHeader = tokenHeader;
    }

    public String getTokenPrefix() {
        return tokenPrefix;
    }

    public void setTokenPrefix(String tokenPrefix) {
        this.tokenPrefix = tokenPrefix;
    }
}

