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

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutionException;
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.contant.AiSoftWareCopyRightRedisKey;
import cn.com.poc.ai_software_copyright.domain.GeneratedDoc;
import cn.com.poc.ai_software_copyright.domian.BaseInfo;
import cn.com.poc.ai_software_copyright.domian.TechnicalContent;
import cn.com.poc.ai_software_copyright.entity.BizSoftwareCopyrightEntity;
import cn.com.poc.ai_software_copyright.service.BizSoftwareCopyrightService;
import cn.com.poc.common.service.RedisService;
import cn.com.poc.common.utils.JsonUtils;
import cn.com.poc.common.utils.StringUtils;
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.core.exception.BusinessException;
import cn.com.yict.framemax.tumbleweed.client.annotation.Consumer;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

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

    private Logger logger = LoggerFactory.getLogger(SoftwareCopyRightConsumerService.class);

    @Resource
    private BizSoftwareCopyrightService bizSoftwareCopyrightService;

    @Resource
    private SoftwareCopyRightProduceService produceService;

    @Resource
    private SoftwareCopyRightAgent agent;

    @Resource
    private RedisService redisService;

    /**
     * 提交生成文档任务
     *
     * @param message
     * @return
     */
    @Override
    @Consumer(topic = SoftwareCopyRightTopic.SUBMIT_SOFTWARE_COPYRIGHT_TASK, scale = 2)
    public void submitTask(SoftwareCopyRightMessage message) {
        logger.info("submit task:{}", JsonUtils.serialize(message));
        message.setStatus(AiSoftWareCopyRightEnum.GENERATED_STATUS.Running);
        produceService.updateStatus(message);

        String generatedDocType = message.getGeneratedDocType();
        List<String> docTypes = JsonUtils.deSerialize(generatedDocType, new TypeReference<List<String>>() {
        }.getType());
        List<String> fileURLS = new ArrayList<>();
        if (StringUtils.isNotBlank(message.getReferenceDocumentFileUrl())) {
            fileURLS = JsonUtils.deSerialize(message.getReferenceDocumentFileUrl(), new TypeReference<List<String>>() {
            }.getType());
        }
        String ui = redisService.get(AiSoftWareCopyRightRedisKey.CALL_BACK + message.getId() + ":ui").toString();
        String demand =
                redisService.get(AiSoftWareCopyRightRedisKey.CALL_BACK + message.getId() + ":demand").toString();
        String structure =
                redisService.get(AiSoftWareCopyRightRedisKey.CALL_BACK + message.getId() + ":structure").toString();
        JSONObject jsonObject = JSONObject.parseObject(structure);
        JSONObject result = jsonObject.getJSONObject("result");
        structure = result.toJSONString();
        GeneratedDoc generatedDoc = new GeneratedDoc();
        generatedDoc.setId(message.getId());
        generatedDoc.setFileURLs(fileURLS);
        generatedDoc.setUi(ui);
        generatedDoc.setDemand(demand);
        generatedDoc.setStructure(structure);
        generatedDoc.setBaseinfo(StringUtils.isNotBlank(message.getBaseInfo()) ?
                JsonUtils.deSerialize(message.getBaseInfo(), BaseInfo.class) : null);
        generatedDoc.setTechnical_content(StringUtils.isNotBlank(message.getTechnicalContent()) ?
                JsonUtils.deSerialize(message.getTechnicalContent(), TechnicalContent.class) : null);
        for (String docType : docTypes) {
            agent.generatedDocByType(generatedDoc, AiSoftWareCopyRightEnum.DOC_TYPE.valueOf(docType));
        }
    }

    @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);
        }
    }
}
