package com.xindazhou.framework.security.config;

import com.xindazhou.framework.security.core.jwt.OAuth2JwtUtil;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.core.io.ResourceLoader;

/**
 * OAuth2 JWT 自动配置
 *
 * @author xindazhou
 */
@Slf4j
@AutoConfiguration
@ConditionalOnProperty(prefix = "xdz.security.oauth2-jwt", name = "enabled", havingValue = "true", matchIfMissing = false)
public class OAuth2JwtAutoConfiguration {

    @Value("${xdz.security.oauth2-jwt.private-key-path:}")
    private String privateKeyPath;

    @Value("${xdz.security.oauth2-jwt.public-key-path:}")
    private String publicKeyPath;

    @Value("${xdz.security.oauth2-jwt.expire-time:7200}")
    private Long expireTime;

    private final ResourceLoader resourceLoader;

    public OAuth2JwtAutoConfiguration(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    @PostConstruct
    public void init() {
        OAuth2JwtUtil.init(privateKeyPath, publicKeyPath, expireTime, resourceLoader);
        log.info("OAuth2 JWT 自动配置完成: expireTime={}s", expireTime);
    }

}

