package com.pavis.admin.aigc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pavis.admin.aigc.mapper.DocChunkMapper;
import com.pavis.admin.aigc.mapper.DocMapper;
import com.pavis.admin.aigc.model.entity.DocChunkDO;
import com.pavis.admin.aigc.model.entity.DocDO;
import lombok.RequiredArgsConstructor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import top.continew.starter.extension.crud.service.BaseServiceImpl;
import com.pavis.admin.aigc.mapper.KnowledgeMapper;
import com.pavis.admin.aigc.model.entity.KnowledgeDO;
import com.pavis.admin.aigc.model.query.KnowledgeQuery;
import com.pavis.admin.aigc.model.req.KnowledgeReq;
import com.pavis.admin.aigc.model.resp.KnowledgeDetailResp;
import com.pavis.admin.aigc.model.resp.KnowledgeResp;
import com.pavis.admin.aigc.service.KnowledgeService;

import java.util.List;

/**
 * AIGC知识库业务实现
 *
 * @author semi
 * @since 2025/05/26 17:28
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class KnowledgeServiceImpl extends BaseServiceImpl<KnowledgeMapper, KnowledgeDO, KnowledgeResp, KnowledgeDetailResp, KnowledgeQuery, KnowledgeReq> implements KnowledgeService {

    private final DocMapper aigcDocsMapper;
    private final DocChunkMapper aigcDocsSliceMapper;

    @Override
    public List<KnowledgeResp> selectKnowledge() {
        return baseMapper.selectKnowledge();
    }

    @Override
    @Transactional
    public void addDocs(DocDO data) {
        aigcDocsMapper.insert(data);
    }

    // @Override
    // @Transactional
    // public void addDocsSlice(DocChunkDO data) {
    //     aigcDocsSliceMapper.insert(data);
    // }

    // @Override
    // @Transactional
    // public void updateDocs(DocDO data) {
    //     aigcDocsMapper.updateById(data);
    // }

    // @Override
    // @Transactional
    // public void addDocs(AigcDocs data) {
    //     data.setCreateTime(new Date());
    //     aigcDocsMapper.insert(data);
    // }

    // @Override
    // @Transactional
    // public void updateDocs(AigcDocs data) {
    //     aigcDocsMapper.updateById(data);
    // }

    // @Override
    // @Transactional
    // public void addDocsSlice(AigcDocsSlice data) {
    //     data.setCreateTime(new Date())
    //             .setWordNum(data.getContent().length())
    //             .setStatus(true)
    //     ;
    //     aigcDocsSliceMapper.insert(data);
    // }

    // @Override
    // @Transactional
    // public void updateDocsSlice(AigcDocsSlice data) {
    //     aigcDocsSliceMapper.updateById(data);
    // }

    @Override
    public List<String> listSliceVectorIdsOfDoc(String docsId) {
        LambdaQueryWrapper<DocChunkDO> selectWrapper = Wrappers.<DocChunkDO>lambdaQuery()
                .select(DocChunkDO::getEmbedStoreId)
                .eq(DocChunkDO::getDocId, docsId);
        List<String> vectorIds = aigcDocsSliceMapper.selectList(selectWrapper)
                .stream()
                .map(DocChunkDO::getEmbedStoreId)
                .toList();
        log.debug("slices of doc: [{}], count: [{}]", docsId, vectorIds.size());
        return vectorIds;
    }

    // @Override
    // public List<AigcDocs> getDocsByKb(String knowledgeId) {
    //     return aigcDocsMapper.selectList(Wrappers.<AigcDocs>lambdaQuery()
    //             .eq(AigcDocs::getKnowledgeId, knowledgeId));
    // }

    // @Override
    // @Transactional
    // public void removeKnowledge(String knowledgeId) {
    //     baseMapper.deleteById(knowledgeId);
    //     // del docs & docsSlice
    //     List<String> docsIds = getDocsByKb(knowledgeId).stream().map(AigcDocs::getId).toList();
    //     docsIds.forEach(this::removeSlicesOfDoc);
    // }
    //
    @Override
    @Transactional
    public void removeSlicesOfDoc(String docsId) {
        LambdaQueryWrapper<DocChunkDO> deleteWrapper = Wrappers.<DocChunkDO>lambdaQuery()
                .eq(DocChunkDO::getDocId, docsId);
        int count = aigcDocsSliceMapper.delete(deleteWrapper);
        log.debug("remove all slices of doc: [{}], count: [{}]", docsId, count);
    }
}