package com.jeesharp.business.modules.contentsecurity.application.api;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.contentsecurity.api.ContentSecurityApi;
import com.jeesharp.business.modules.contentsecurity.application.ContentSecurityApplicationService;
import com.jeesharp.business.modules.contentsecurity.dto.ContentCheckRequest;
import com.jeesharp.business.modules.contentsecurity.dto.ContentCheckResult;
import com.jeesharp.business.modules.contentsecurity.enums.CheckDecisionEnum;
import com.jeesharp.framework.exception.BizErrorException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * {@link ContentSecurityApi} 实现，委托 {@link ContentSecurityApplicationService}。
 *
 * @author jeesharp
 * @since 2026-06-27
 */
@Service
@AllArgsConstructor
public class ContentSecurityApiImpl implements ContentSecurityApi {

    private final ContentSecurityApplicationService contentSecurityApplicationService;

    @Override
    public ContentCheckResult checkText(ContentCheckRequest request) {
        return contentSecurityApplicationService.checkText(request);
    }

    @Override
    public ContentCheckResult checkMedia(ContentCheckRequest request) {
        return contentSecurityApplicationService.checkMedia(request);
    }

    @Override
    public ContentCheckResult checkMediaBatch(List<ContentCheckRequest> requests) {
        return contentSecurityApplicationService.checkMediaBatch(requests);
    }

    @Override
    public void bindPendingTasks(String bizModule, String bizId, List<String> traceIds, List<String> auditLogIds) {
        contentSecurityApplicationService.bindPendingTasks(bizModule, bizId, traceIds, auditLogIds);
    }

    @Override
    public void handleWechatMediaCallback(String rawJson) {
        contentSecurityApplicationService.handleWechatMediaCallback(rawJson);
    }

    @Override
    public void assertPass(ContentCheckResult result) {
        if (result == null || result.getDecision() == null) {
            return;
        }
        if (result.getDecision() == CheckDecisionEnum.REJECT) {
            throw new BizErrorException(StrUtil.blankToDefault(result.getRejectReason(), "内容不符合规范，请修改后重试"));
        }
    }
}
