package com.njcn.msgpush.module.push.checker;

import com.njcn.msgpush.module.push.checker.impl.BlacklistChecker;
import com.njcn.msgpush.module.push.checker.impl.IdempotencyChecker;
import com.njcn.msgpush.module.push.checker.impl.QuotaChecker;
import com.njcn.msgpush.module.push.checker.impl.RateLimitChecker;
import com.njcn.msgpush.module.push.controller.admin.message.vo.MessageRecordSendReqVO;

import java.util.ArrayList;
import java.util.List;

/**
 * @author caozehui
 * @data 2026-02-28
 * @description 检查链
 */
public class MsgPushGuardChain {
    private final List<IChecker> checkers;

    public MsgPushGuardChain() {
        this.checkers = new ArrayList<>();
        this.checkers.add(new IdempotencyChecker());
        this.checkers.add(new BlacklistChecker());
        this.checkers.add(new QuotaChecker());
        this.checkers.add(new RateLimitChecker());
    }

    public boolean checkAll(MessageRecordSendReqVO reqVO) {
        for (IChecker checker : checkers) {
            boolean result = checker.check(reqVO);
            if (!result) {
                // 任何一层检查失败，立即返回拒绝
                logRejection(reqVO);
                return result;
            }
        }
        return true;
    }

    private void logRejection(MessageRecordSendReqVO reqVO) {
        // 记录拒绝日志，用于监控和分析
        System.out.printf("消息请求被拒绝: receiver=%s, messageId=%s, reason=%s%n", reqVO.getReceiver(), reqVO.getMessageId());
    }
}
