package top.tqx.demo_1.config;

import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@ConfigurationProperties(prefix = "jwt")
public class JwtProperties {

    private String secret;

    private Long expiration;

    private String header;

    private String prefix;

    @Value("${spring.profiles.active:dev}")
    private String activeProfile;

    @PostConstruct
    public void validateSecret() {
        if (secret == null || secret.isEmpty()) {
            throw new IllegalStateException("JWT Secret不能为空");
        }

        if ("prod".equals(activeProfile) || "production".equals(activeProfile)) {
            if (secret.contains("change-in-production") || secret.length() < 64) {
                log.error("生产环境JWT Secret不符合安全要求");
                throw new IllegalStateException("生产环境必须配置强密码(JWT Secret至少64字符且不包含默认值)");
            }
        }

        log.info("JWT配置已加载, expiration={}, header={}, prefix={}", expiration, header, prefix);
    }

    public String getSecret() {
        return secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    public Long getExpiration() {
        return expiration;
    }

    public void setExpiration(Long expiration) {
        this.expiration = expiration;
    }

    public String getHeader() {
        return header;
    }

    public void setHeader(String header) {
        this.header = header;
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }
}
