package tech.easyflow.approval.support;

import tech.easyflow.approval.enums.ApprovalActionType;
import tech.easyflow.common.web.exceptions.BusinessException;

/**
 * 发布审批说明校验策略。
 */
public final class ApprovalApplicationReasonPolicy {

    /**
     * 审批说明最大长度。
     */
    public static final int MAX_LENGTH = 500;

    private ApprovalApplicationReasonPolicy() {
    }

    /**
     * 按审批匹配结果和动作类型规范化审批说明。
     *
     * @param approvalRequired 是否命中审批流
     * @param actionType 动作类型
     * @param applicationReason 原始审批说明
     * @return 需要说明时返回去除首尾空白的文本，否则返回 {@code null}
     * @throws BusinessException 必填说明为空或超过长度限制时抛出
     */
    public static String normalize(
            boolean approvalRequired,
            String actionType,
            String applicationReason
    ) {
        if (!approvalRequired || !ApprovalActionType.PUBLISH.getCode().equals(actionType)) {
            return null;
        }
        String normalized = applicationReason == null ? "" : applicationReason.trim();
        if (normalized.isEmpty()) {
            throw new BusinessException(400, 400, "请填写审批说明");
        }
        if (normalized.length() > MAX_LENGTH) {
            throw new BusinessException(400, 400, "审批说明不能超过500字");
        }
        return normalized;
    }
}
