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.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * PC端使用说明书生成策略实现
 */
@Component
public class PcOperatingManualGenerationStrategy extends DocGenerationStrategy {

    private static final Logger logger = LoggerFactory.getLogger(PcOperatingManualGenerationStrategy.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 = "pcOperatingManual" + DateUtils.getCurrTime();

        CommonThreadPoolExecutor.addTask(() -> {
            try {
                softwareCopyRightUtils.executeBlockingChat(fileUrls, q, data, user, "pc-operating-manual");
            } catch (Exception e) {
                logger.error("生成电脑端使用说明书失败: {}", e.getMessage(), e);
            }
        });
    }
}