package cn.com.poc.message.service.impl;

import javax.annotation.Resource;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

import cn.com.poc.ai_software_copyright.agent.SoftwareCopyRightAgent;
import cn.com.poc.ai_software_copyright.contant.AiSoftWareCopyRightEnum;
import cn.com.poc.ai_software_copyright.entity.BizSoftwareCopyrightEntity;
import cn.com.poc.ai_software_copyright.service.BizSoftwareCopyrightService;
import cn.com.poc.common.utils.JsonUtils;
import cn.com.poc.message.entity.SoftwareCopyRightMessage;
import cn.com.poc.message.service.SoftwareCopyRightConsumerService;
import cn.com.poc.message.service.SoftwareCopyRightProduceService;
import cn.com.poc.message.topic.SoftwareCopyRightTopic;
import cn.com.yict.framemax.tumbleweed.client.annotation.Consumer;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.stereotype.Service;

/**
 * @author alex.yao
 * @date 2025/12/26
 */
@Service
public class SoftwareCopyRightConsumerServiceImpl implements SoftwareCopyRightConsumerService {

    @Resource
    private BizSoftwareCopyrightService bizSoftwareCopyrightService;

    @Resource
    private SoftwareCopyRightProduceService produceService;

    @Resource
    private SoftwareCopyRightAgent agent;

    /**
     * 提交生成文档任务
     *
     * @param message
     * @return
     */
    @Override
    @Consumer(topic = SoftwareCopyRightTopic.SUBMIT_SOFTWARE_COPYRIGHT_TASK, scale = 2)
    public void submitTask(SoftwareCopyRightMessage message) {
        message.setStatus(AiSoftWareCopyRightEnum.GENERATED_STATUS.Running);
        produceService.updateStatus(message);
        //todo 实现提交生成文档任务
        String generatedDocType = message.getGeneratedDocType();
        List<String> docTypes = JsonUtils.deSerialize(generatedDocType, new TypeReference<List<String>>() {
        }.getType());

        // 创建一个CompletableFuture数组来存储所有任务
        List<CompletableFuture<Object>> futures = docTypes.stream()
                .map(docType ->
                        CompletableFuture.supplyAsync(() ->
                                agent.generatedDocByType(message, AiSoftWareCopyRightEnum.DOC_TYPE.valueOf(docType)))
                )
                .collect(Collectors.toList());

        // 等待所有任务完成
        CompletableFuture<Void> allFutures = CompletableFuture.allOf(
                futures.toArray(new CompletableFuture[0])
        );
        allFutures.thenRun(() -> {
            //todo 处理完成逻辑

        }).exceptionally(throwable -> {

            return null;
        });
    }

    @Override
    @Consumer(topic = SoftwareCopyRightTopic.UPDATE_TASK_STATUS, retry = true, scale = 2)
    public void updateStatus(SoftwareCopyRightMessage message) {
        BizSoftwareCopyrightEntity bizSoftwareCopyrightEntity = bizSoftwareCopyrightService.get(message.getId());
        if (bizSoftwareCopyrightEntity != null) {
            bizSoftwareCopyrightEntity.setGeneratedStatus(message.getStatus().name());
            bizSoftwareCopyrightService.update(bizSoftwareCopyrightEntity);
        }
    }
}
