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

import cn.reghao.jutil.jdk.db.PageList;
import cn.reghao.jutil.web.WebResult;
import cn.reghao.tnb.search.app.model.po.Wenshu;
import cn.reghao.tnb.search.app.model.vo.WenshuResult;
import cn.reghao.tnb.search.app.service.WenshuSearchService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * 裁判文书搜索接口
 *
 * @author reghao
 * @date 2025-05-02 16:44:46
 */
@RestController
@RequestMapping("/api/search1/wenshu")
public class WenshuSearchController {
    private final WenshuSearchService wenshuSearchService;

    public WenshuSearchController(WenshuSearchService wenshuSearchService) {
        this.wenshuSearchService = wenshuSearchService;
    }

    @GetMapping(value = "/query", produces = MediaType.APPLICATION_JSON_VALUE)
    public String searchWenshu(@RequestParam(value = "pn") int pn,
                               @RequestParam(value = "keyword") String keyword) {
        PageList<WenshuResult> pageList1 = wenshuSearchService.search(pn, keyword);
        /*ElasticQuery elasticQuery = new ElasticQuery.Builder()
                .pageSize(pageSize)
                .pageNumber(pn)
                .indexName(indexName)
                .highlightFieldName("caseName")
                .otherFiledNames(List.of("caseName", "cause", "parties"))
                .queryString(keyword)
                .build();
        Page<Wenshu> page = queryService.queryWithHighlight(elasticQuery, Wenshu.class);
        int pageNumber = page.getNumber();
        int pageSize = page.getSize();
        int total = (int) page.getTotalElements();
        List<WenshuResult> list = page.getContent().stream().map(WenshuResult::new).collect(Collectors.toList());
        PageList<WenshuResult> pageList = PageList.pageList(pageNumber, pageSize, total, list);*/
        return WebResult.success(pageList1);
    }

    @GetMapping(value = "/detail", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getWenshuDetail(@RequestParam("id") String id, @RequestParam("keyword") String keyword) {
        Wenshu wenshu = wenshuSearchService.queryById(id, keyword);
        return WebResult.success(wenshu);
    }
}
