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

import cn.reghao.jutil.jdk.web.db.PageList;
import cn.reghao.tnb.search.esapp.db.repository.WenshuDocRepository;
import cn.reghao.tnb.search.esapp.es.QueryService;
import cn.reghao.tnb.search.esapp.model.po.Wenshu;
import cn.reghao.tnb.search.esapp.model.vo.ElasticQuery;
import cn.reghao.tnb.search.esapp.model.vo.WenshuResult;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

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 WenshuDocRepository wenshuDocRepository;
    private final QueryService<Wenshu> queryService;

    public WenshuSearchService(WenshuDocRepository wenshuDocRepository, QueryService<Wenshu> queryService) {
        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();
        return PageList.empty();
    }

    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) {
        return null;
    }
}
