package cn.com.poc.ai_software_copyright.strategy.impl;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.strategy.DocGenerationStrategy;
import cn.com.poc.ai_software_copyright.utils.SoftwareCopyRightUtils;
import cn.com.poc.common.pool.CommonThreadPoolExecutor;
import cn.com.poc.common.utils.DateUtils;
import cn.com.poc.common.utils.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 源代码生成策略实现
 */
@Component
public class SourceCodeGenerationStrategy extends DocGenerationStrategy {

    private static final Logger logger = LoggerFactory.getLogger(SourceCodeGenerationStrategy.class);

    @Resource
    private SoftwareCopyRightUtils softwareCopyRightUtils;

    @Override
    public void generate(GeneratedDoc doc) {
        String q = "生成源代码";
        List<String> fileUrls = softwareCopyRightUtils.downloadAndUnzipPlugin(doc.getFileURLs());
        Map<String, Object> data = new HashMap<>();
        data.put("ui", doc.getUi());
        data.put("structure", doc.getStructure());
        data.put("demand", doc.getDemand());
        data.put("code", "");
        data.put("baseinfo", JsonUtils.serialize(doc.getBaseinfo()));
        data.put("technical_content", JsonUtils.serialize(doc.getTechnical_content()));
        data.put("callback_url", this.getCallbackURL()); // 获取回调URL
        data.put("task_id", AiSoftWareCopyRightRedisKey.CALL_BACK + doc.getId());
        String user = "sourceCode" + DateUtils.getCurrTime();

        CommonThreadPoolExecutor.addTask(() -> {
            try {
                softwareCopyRightUtils.executeBlockingChat(fileUrls, q, data, user, "source-code");
            } catch (Exception e) {
                logger.error("生成源代码失败: {}", e.getMessage(), e);
            }
        });
    }
}