package cn.reghao.tnb.search.app.service;

import cn.reghao.jutil.jdk.db.PageList;
import cn.reghao.tnb.search.app.db.repository.WenshuDocRepository;
import cn.reghao.tnb.search.app.es.QueryService;
import cn.reghao.tnb.search.app.lucene.LuceneSearch;
import cn.reghao.tnb.search.app.model.po.Wenshu;
import cn.reghao.tnb.search.app.model.po.WenshuDoc;
import cn.reghao.tnb.search.app.model.po.WenshuLucene;
import cn.reghao.tnb.search.app.model.vo.ElasticQuery;
import cn.reghao.tnb.search.app.model.vo.WenshuResult;
import org.apache.lucene.document.Document;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2025-08-21 15:22:31
 */
@Service
public class WenshuSearchService {
    private int pageSize = 10;
    private String indexName = "wenshu_lucene";
    private LuceneSearch luceneSearch;
    private WenshuDocRepository wenshuDocRepository;
    private final QueryService<Wenshu> queryService;

    public WenshuSearchService(LuceneSearch luceneSearch, WenshuDocRepository wenshuDocRepository,
                               QueryService<Wenshu> queryService) {
        this.luceneSearch = luceneSearch;
        this.wenshuDocRepository = wenshuDocRepository;
        this.queryService = queryService;
    }

    public PageList<WenshuResult> search(int pageNumber, String queryString) {
        ElasticQuery elasticQuery = new ElasticQuery.Builder()
                .pageSize(pageSize)
                .pageNumber(pageNumber)
                .indexName(indexName)
                .queryFiledNames(List.of("caseName", "fullText"))
                .highlightFiledNames(List.of("caseName", "fullText"))
                .queryString(queryString)
                .build();

        PageList<WenshuLucene> pageList = luceneSearch.queryWithHighlight(elasticQuery, WenshuLucene.class);
        long total = pageList.getTotalSize();
        List<WenshuResult> wenshuResultList = new ArrayList<>();
        for (WenshuLucene wenshuLucene : pageList.getList()) {
            String wenshuId = wenshuLucene.getId();
            WenshuDoc wenshuDoc = wenshuDocRepository.findByWenshuId(Long.parseLong(wenshuId));
            Wenshu wenshu = new Wenshu(wenshuDoc, wenshuLucene);
            WenshuResult wenshuResult = new WenshuResult(wenshu);
            wenshuResultList.add(wenshuResult);
        }
        return PageList.pageList(pageNumber, pageSize, (int) total, wenshuResultList);
    }

    public PageList<WenshuResult> search1(int pageNumber, String queryString) {
        ElasticQuery elasticQuery = new ElasticQuery.Builder()
                .pageSize(pageSize)
                .pageNumber(pageNumber)
                .indexName(indexName)
                .queryFiledNames(List.of("caseName", "fullText"))
                .highlightFiledNames(List.of("caseName", "fullText"))
                .queryString(queryString)
                .build();
        Page<Wenshu> page = queryService.queryWithHighlight(elasticQuery, Wenshu.class);
        int pageSize = page.getSize();
        int total = (int) page.getTotalElements();
        List<WenshuResult> list = page.getContent().stream().map(WenshuResult::new).collect(Collectors.toList());
        return PageList.pageList(pageNumber, pageSize, total, list);
    }

    public Wenshu queryById(String wenshuId, String queryString) {
        String fieldName = "fullText";
        long wenshuIdLong = Long.parseLong(wenshuId);
        Document document = luceneSearch.findDocumentById(indexName, wenshuId);
        String fullText = document.get(fieldName);

        String highlightText = "";
        try {
            highlightText = luceneSearch.getHighlightText(fieldName, fullText, queryString);
        } catch (Exception e) {
            e.printStackTrace();
        }

        WenshuDoc wenshuDoc = wenshuDocRepository.findByWenshuId(wenshuIdLong);
        Wenshu wenshu = new Wenshu(wenshuDoc);
        if (!highlightText.isBlank()) {
            wenshu.setFullText(highlightText);
        } else {
            wenshu.setFullText(fullText);
        }
        return wenshu;
    }
}
