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

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

import cn.com.poc.agent_application.constant.AgentApplicationDialoguesRecordConstants;
import cn.com.poc.agent_application.entity.AgentResultEntity;
import cn.com.poc.agent_application.entity.BizAgentApplicationDialoguesRecordEntity;
import cn.com.poc.agent_application.service.BizAgentApplicationDialoguesRecordService;
import cn.com.poc.ai_dialogues.entity.BizAiDialoguesEntity;
import cn.com.poc.ai_dialogues.service.BizAiDialoguesService;
import cn.com.poc.annual_eport.agent.AnnualReportAgent;
import cn.com.poc.annual_eport.service.AnnualReportService;
import cn.com.poc.common.constant.CommonConstant;
import cn.com.yict.framemax.core.exception.BusinessException;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @author alex.yao
 * @date 2026/2/5
 */
@Component
public class AnnualReportServiceImpl implements AnnualReportService {

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

    @Resource
    private AnnualReportAgent annualReportAgent;

    @Resource
    private BizAiDialoguesService bizAiDialoguesService;

    @Resource
    private BizAgentApplicationDialoguesRecordService bizAgentApplicationDialoguesRecordService;

    @Override
    public void call(String input, List<String> fileUrls, String dialoguesId, Map<String, Object> data)
            throws Exception {
        AgentResultEntity agentResultEntity = new AgentResultEntity();
        long inputTimeStamp = System.currentTimeMillis();
        BizAiDialoguesEntity bizAiDialoguesEntity = new BizAiDialoguesEntity();
        bizAiDialoguesEntity.setDialoguesId(dialoguesId);
        bizAiDialoguesEntity.setMemberId(null);
        bizAiDialoguesEntity.setIsDeleted(CommonConstant.IsDeleted.N);
        List<BizAiDialoguesEntity> bizAiDialoguesEntities =
                bizAiDialoguesService.findByExample(bizAiDialoguesEntity, null);

        if (CollectionUtils.isEmpty(bizAiDialoguesEntities)) {
            throw new BusinessException("对话ID不存在");
        }

        BizAiDialoguesEntity aiDialoguesEntity = bizAiDialoguesEntities.get(0);
        //更新标题
        if (org.apache.commons.lang3.StringUtils.isBlank(aiDialoguesEntity.getTitle())) { // 标题
            aiDialoguesEntity.setTitle(input.substring(0, Math.min(input.length(), 150)));
            bizAiDialoguesService.update(aiDialoguesEntity);
        }
        try {
            agentResultEntity = annualReportAgent.agent(dialoguesId, fileUrls, input, data);
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new BusinessException("对话异常");
        } finally {
            //保存对话记录
            //用户输入记录
            BizAgentApplicationDialoguesRecordEntity inputRecord = new BizAgentApplicationDialoguesRecordEntity();
            inputRecord.setAgentId(null);
            inputRecord.setMemberId(null);
            inputRecord.setContent(input);
            inputRecord.setDialogsId(dialoguesId);
            inputRecord.setRole(AgentApplicationDialoguesRecordConstants.ROLE.USER);
            inputRecord.setTimestamp(inputTimeStamp);
            bizAgentApplicationDialoguesRecordService.save(inputRecord);
            //AI输出记录
            BizAgentApplicationDialoguesRecordEntity outputRecord = new BizAgentApplicationDialoguesRecordEntity();
            outputRecord.setRole(AgentApplicationDialoguesRecordConstants.ROLE.ASSISTANT);
            outputRecord.setAgentId(null);
            outputRecord.setDialogsId(dialoguesId);
            outputRecord.setMemberId(null);
            outputRecord.setTimestamp(System.currentTimeMillis());
            outputRecord.setContent(agentResultEntity.getMessage());
            outputRecord.setReasoningContent(agentResultEntity.getReasoningContent());
            bizAgentApplicationDialoguesRecordService.save(outputRecord);
        }
    }
}
