package cn.meowrain.aioj.backend.framework.security.autoconfigure;

import cn.meowrain.aioj.backend.framework.security.filter.JwtAuthenticationFilter;
import cn.meowrain.aioj.backend.framework.security.properties.JwtPropertiesConfiguration;
import cn.meowrain.aioj.backend.framework.security.utils.JwtUtil;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * Spring Security 自动配置类
 *
 * 引入 aioj-backend-common-security 依赖后，会自动启用：
 * - JWT 认证过滤器
 * - Spring Security 基本配置
 *
 * 配置项（application.yml）：
 * jwt:
 *   enabled: true          # 是否启用 JWT 认证（默认 true）
 *   secret: your-secret    # JWT 密钥
 *   access-expire: 7200000 # Access Token 过期时间
 *   refresh-expire: 604800000 # Refresh Token 过期时间
 */
@AutoConfiguration
@EnableConfigurationProperties(JwtPropertiesConfiguration.class)
public class SecurityAutoConfiguration {

	@Bean
	@ConditionalOnMissingBean
	@ConditionalOnProperty(prefix = "jwt", name = "enabled", havingValue = "true", matchIfMissing = true)
	public JwtUtil jwtUtil(JwtPropertiesConfiguration jwtConfig) {
		return new JwtUtil(jwtConfig);
	}

	@Bean
	@ConditionalOnMissingBean
	@ConditionalOnProperty(prefix = "jwt", name = "enabled", havingValue = "true", matchIfMissing = true)
	public JwtAuthenticationFilter jwtAuthenticationFilter(JwtUtil jwtUtil) {
		return new JwtAuthenticationFilter(jwtUtil);
	}

}
