package org.jk.rag.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jk.common.mybatis.core.page.PageQuery;
import org.jk.common.satoken.utils.LoginHelper;
import org.jk.rag.domain.SliceInfoHistory;
import org.jk.rag.domain.dto.ReviseSliceDTO;
import org.jk.rag.domain.entity.JkMediaReplacement;
import org.jk.rag.domain.entity.JkSliceInfo;
import org.jk.rag.domain.entity.SliceInfo;
import org.jk.rag.domain.vo.JkReviseDocumentVo;
import org.jk.rag.domain.vo.JkReviseVo;
import org.jk.rag.domain.vo.ReviseHistoryVo;
import org.jk.rag.mapper.JkMediaReplacementMapper;
import org.jk.rag.mapper.JkReviseMapper;
import org.jk.rag.mapper.JkSliceInfoMapper;
import org.jk.rag.service.IJkReviseService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 用户 业务层处理
 *
 * @author jk
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class JkReviseServiceImpl implements IJkReviseService {

    private final JkReviseMapper reviseMapper;

    private final JkMediaReplacementMapper jkMediaReplacementMapper;

    private final JkSliceInfoMapper sliceInfoMapper;

    @Override
    public Page<JkReviseVo> selectRevisePageList(String knowledgeName, PageQuery pageQuery) {
        Long createBy = LoginHelper.getUserId();
        Page<JkReviseVo> page = reviseMapper.selectRevisePageList(knowledgeName,createBy,pageQuery.build());
        for (JkReviseVo reviseVo : page.getRecords()) {
            List<String> appNames = reviseMapper.selectAppName(reviseVo.getKnowledgeId());
            reviseVo.setAppNames(appNames);
        }
        return page;
    }

    @Override
    public List<JkReviseDocumentVo> selectReviseList(String documentName,String knowledgeId) {
        Long createBy = LoginHelper.getUserId();
        List<JkReviseDocumentVo> documentVos = reviseMapper.selectReviseDocumentList(documentName,createBy,knowledgeId);
        return documentVos;
    }

    @Override
    public List<SliceInfo> selectReviseSliceList(String sliceText, String documentId) {
        List<SliceInfo> sliceInfos  = reviseMapper.selectReviseSliceInfoList(documentId,sliceText);
        for (SliceInfo sliceInfo : sliceInfos) {
            List<JkMediaReplacement> mediaReplacements = jkMediaReplacementMapper.selectMediaList(JkMediaReplacement.builder().documentId(sliceInfo.getDocumentId()).build());
            List<JkMediaReplacement> filterList = mediaReplacements.stream()
                .filter(entity -> {
                    // 避免 originText 为 null 导致空指针
                    String originText = entity.getOriginText();
                    return originText != null && sliceInfo.getSliceText().contains(originText);
                })
                .collect(Collectors.toList());
            sliceInfo.setMediaList(filterList);
        }
        return sliceInfos;
    }

    @Override
    public void updateReviseSlice(ReviseSliceDTO reviseSliceDTO) {
        JkSliceInfo sliceInfo = sliceInfoMapper.selectBySliceId(reviseSliceDTO.getSliceId());
        if(reviseSliceDTO.getRevisionStatus().equals("0")) {
            reviseSliceDTO.setRefSliceId(null);
        }
        String userId = LoginHelper.getUserId().toString();
        reviseMapper.updateSliceRevisionStatus(reviseSliceDTO.getRevisionStatus(), reviseSliceDTO.getSliceId(), reviseSliceDTO.getRefSliceId(), reviseSliceDTO.getRevisionSliceText(),userId);
        SliceInfoHistory sliceInfoHistory = new SliceInfoHistory();
        sliceInfoHistory.setRefSliceId(sliceInfo.getSliceId());
        sliceInfoHistory.setRevisionStatus(reviseSliceDTO.getRevisionStatus());
        sliceInfoHistory.setSliceId(reviseSliceDTO.getSliceId());
        sliceInfoHistory.setDocumentId(sliceInfo.getDocumentId());
        sliceInfoHistory.setKnowledgeId(sliceInfo.getKnowledgeId());
        sliceInfoHistory.setRefSliceId(reviseSliceDTO.getRefSliceId());
        sliceInfoHistory.setRevisionSliceText(reviseSliceDTO.getRevisionSliceText());
        reviseMapper.insert(sliceInfoHistory);
    }

    @Override
    public Page<ReviseHistoryVo> selectReviseHistoryList(String knowledgeId,String knowledgeName,String documentName,String refDocumentName,PageQuery pageQuery) {
        return reviseMapper.selectReviseHistoryList(knowledgeId,knowledgeName,documentName,refDocumentName,pageQuery.build());
    }
}
