package com.huimv.employment.common.util;

import com.huimv.employment.common.exception.BizException;
import com.huimv.employment.common.exception.ErrorCode;

/**
 * 身份证号校验与脱敏工具。
 */
public final class IdCardUtils {

    private static final String ID_CARD_PATTERN =
            "^[1-9]\\d{5}(18|19|20)\\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\\d|3[01])\\d{3}[\\dXx]$";

    private IdCardUtils() {
    }

    public static void validate(String idCardNo) {
        if (idCardNo == null || !idCardNo.matches(ID_CARD_PATTERN)) {
            throw new BizException(ErrorCode.BAD_REQUEST, "身份证号格式不正确");
        }
    }

    /**
     * 示例：310101199001011234 → 310***********1234
     */
    public static String mask(String idCardNo) {
        validate(idCardNo);
        return idCardNo.substring(0, 3) + "***********" + idCardNo.substring(idCardNo.length() - 4);
    }
}
