import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import cn.reghao.jutil.tool.id.SnowFlake;
import cn.reghao.tnb.content.api.constant.PostScope;
import cn.reghao.tnb.search.app.SearchApplication;
import cn.reghao.tnb.search.app.es.*;
import cn.reghao.tnb.search.app.model.po.VideoText;
import co.elastic.clients.elasticsearch._types.mapping.Property;
import co.elastic.clients.elasticsearch.indices.AnalyzeRequest;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.test.context.ActiveProfiles;

import java.io.IOException;
import java.util.*;

/**
 * @author reghao
 * @date 2025-03-18 15:51:21
 */
@Slf4j
@ActiveProfiles("dev")
@SpringBootTest(classes = SearchApplication.class)
public class ElasticTest {
    void setLogLevel() {
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        Logger rootLogger = loggerContext.getLogger("ROOT");
        rootLogger.setLevel(Level.INFO);
    }

    SnowFlake idGenerator = new SnowFlake(1, 1);
    @Autowired
    ElasticService elasticService;
    @Autowired
    IndexService indexService;
    @Autowired
    MappingService mappingService;
    @Autowired
    DocumentService documentService;

    @Test
    public void indexTest() throws IOException {
        String indexName = "video_text";

        indexService.deleteIndex(indexName);
        Map<String, Property> propertyMap = mappingService.getVideoTextPropertyMap();
        indexService.createIndex(indexName, propertyMap);
    }

    @Autowired
    VideoTextDocument videoTextDocument;
    @Test
    public void documentTest() {
        videoTextDocument.deleteAllDocument();
    }

    @Autowired
    SearchService searchService;
    @Test
    public void storeTest() throws Exception {
        int pn = 1;
        int ps = 12;
        PageRequest pageRequest = PageRequest.of(pn-1, ps);
        String videoId = "ao1n8ggYOg";

        String index = VideoText.class.getSimpleName().toLowerCase(Locale.ROOT);
        String kw = "隔壁";
        /*Page<VideoSummary> page = hibernateQuery.queryWithHighlight(kw, pn, ps);
        Page<VideoSummary> page1 = luceneQuery.queryWithHighlight("", kw, pn, ps);*/
        //Page<VideoSummary> page2 = esQuery.queryWithHighlight(index, kw, pn, ps, VideoSummary.class);
        System.out.println();

        //Map<String, Property> propertyMap = mappingService.getVideoTextPropertyMap();
//        indexService.deleteIndex(index);
//        indexService.createIndex(index, propertyMap);
        //indexService.getIndex(index);
        //indexService.deleteIndex(index);
        //indexService.getIndex(index);
        //indexService.updateMapping(index);
        //searchService.searchAll(index);
        //searchService.count(index);
    }

    @Autowired
    VideoTextQuery videoTextQuery;
    @Test
    public void searchTest() {
        int pageNumber = 1;
        String keyword = "隔壁";

        List<Integer> scopes = List.of(PostScope.PUBLIC.getCode(), PostScope.PROTECT.getCode());
        Page<VideoText> page = videoTextQuery.queryWithHighlight(keyword, scopes, pageNumber);
        long total = page.getTotalElements();
        List<VideoText> list = page.getContent();
        List<VideoText> list1 = videoTextQuery.queryByPage(pageNumber);
        System.out.println();
    }

    @Test
    public void analyzerTest() throws IOException {
        String text = "中华人民共和国国歌";
        String analyzer = "standard";
        analyzer = "ik_max_word";
        //analyzer = "ik_smart";
        AnalyzeRequest analyzeRequest = new AnalyzeRequest.Builder()
                .text(text)
                .analyzer(analyzer)
                .build();
        /*ElasticsearchClient esClient = elasticService.getElasticsearchClient();
        AnalyzeResponse analyzeResponse = esClient.indices().analyze(analyzeRequest);
        List<AnalyzeToken> tokens = analyzeResponse.tokens();*/
    }
}
