package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import shop.alien.entity.store.BarPerformance;
import shop.alien.mapper.BarPerformanceMapper;
import shop.alien.store.service.BarPerformanceAuditService;
import shop.alien.store.util.ai.AiContentModerationUtil;

/**
 * 酒吧演出审核服务实现类
 * 负责异步执行AI内容审核
 *
 * @author assistant
 * @since 2025-12-19
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class BarPerformanceAuditServiceImpl implements BarPerformanceAuditService {

    private final BarPerformanceMapper barPerformanceMapper;
    private final AiContentModerationUtil aiContentModerationUtil;

    /**
     * 异步执行AI内容审核
     * 审核演出名称、演出详情、演出须知等文本内容以及演出海报、详情图片
     *
     * @param performanceId 演出ID
     * @param barPerformance 演出信息
     */
    @Override
    @Async("taskExecutor")
    public void asyncAuditBarPerformance(Integer performanceId, BarPerformance barPerformance) {
        try {
            log.info("开始异步执行AI内容审核，演出ID：{}", performanceId);
            
            // 组装AI审核文本内容
            StringBuilder textContent = new StringBuilder();
            if (StringUtils.isNotEmpty(barPerformance.getPerformanceName())) {
                textContent.append(barPerformance.getPerformanceName()).append(" ");
            }
            if (StringUtils.isNotEmpty(barPerformance.getPerformanceContent())) {
                textContent.append(barPerformance.getPerformanceContent()).append(" ");
            }
            if (StringUtils.isNotEmpty(barPerformance.getPerformanceNotice())) {
                textContent.append(barPerformance.getPerformanceNotice());
            }
            
            // 配图前端审；auditContent 第二参固定 null
            AiContentModerationUtil.AuditResult auditResult = aiContentModerationUtil.auditContent(
                    textContent.toString().trim(), null);
            
            // 根据AI审核结果更新审核状态和拒绝原因
            BarPerformance auditUpdate = new BarPerformance();
            auditUpdate.setId(performanceId);
            if (auditResult != null && auditResult.isPassed()) {
                // 审核通过
                auditUpdate.setReviewStatus(1);
                auditUpdate.setRejectReason(null);
                log.info("演出内容AI审核通过，演出ID：{}", performanceId);
            } else {
                // 审核失败
                String reason = (auditResult != null && StringUtils.isNotEmpty(auditResult.getFailureReason()))
                        ? auditResult.getFailureReason()
                        : "审核未通过";
                log.warn("演出内容AI审核失败，演出ID：{}，原因：{}", performanceId, reason);
                auditUpdate.setReviewStatus(2);
                auditUpdate.setRejectReason(reason);
            }
            barPerformanceMapper.updateById(auditUpdate);
            
            log.info("AI内容审核完成，演出ID：{}", performanceId);
            
        } catch (Exception e) {
            // AI审核异常不影响数据保存，记录日志即可
            log.error("异步调用AI审核接口异常，演出ID：{}", performanceId, e);
        }
    }
}

