package tech.easyflow.system.util;

import tech.easyflow.common.web.exceptions.BusinessException;

import java.util.regex.Pattern;

/**
 * 用户密码策略。
 */
public final class SysPasswordPolicy {

    public static final String STRONG_PASSWORD_MESSAGE = "密码必须至少8位，且包含大写字母、小写字母、数字和特殊字符";

    private static final Pattern STRONG_PASSWORD_PATTERN = Pattern.compile(
            "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[^A-Za-z\\d]).{8,}$"
    );

    private SysPasswordPolicy() {
    }

    public static boolean isStrongPassword(String password) {
        return password != null && STRONG_PASSWORD_PATTERN.matcher(password).matches();
    }

    public static void validateStrongPassword(String password) {
        if (!isStrongPassword(password)) {
            throw new BusinessException(STRONG_PASSWORD_MESSAGE);
        }
    }
}
