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

import co.elastic.clients.elasticsearch._types.mapping.*;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * @author reghao
 * @date 2025-03-12 11:11:07
 */
@Service
public class MappingService {
    Property booleanProp = Property.of(builder -> builder.boolean_(BooleanProperty.of(pro -> pro.index(true))));
    Property intProp = Property.of(builder -> builder.integer(IntegerNumberProperty.of(pro -> pro.index(true))));
    Property longProp = Property.of(builder -> builder.long_(LongNumberProperty.of(pro -> pro.index(true).store(true))));
    Property doubleProp = Property.of(builder -> builder.double_(DoubleNumberProperty.of(pro -> pro.index(true).store(true))));
    Property dateProp = Property.of(builder -> builder.date(DateProperty.of(pro -> pro.index(true).store(true))));
    Property keywordProp = Property.of(builder -> builder.keyword(KeywordProperty.of(pro -> pro.index(true).store(true))));
    Property textProp = Property.of(builder -> builder.text(TextProperty.of(pro -> pro.index(true).store(true))));
    Property textPropIk = Property.of(builder -> builder.text(TextProperty.of(pro -> pro.index(true).analyzer("ik_max_word").store(true))));
    Property textKeywordProp = new Property(new TextProperty.Builder().index(true).fields("raw", keywordProp).store(true).build());

    public Map<String, Property> getPropertyMapWithNginxLog(Class<?> clazz) {
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            String name = field.getName();
            Class<?> type = field.getType();
        }

        Map<String, Property> propertyMap = new HashMap<>();
        propertyMap.put("id", keywordProp);
        propertyMap.put("timeIso8601", dateProp);
        propertyMap.put("remoteAddr", keywordProp);
        propertyMap.put("request", textKeywordProp);
        propertyMap.put("status", intProp);
        propertyMap.put("requestMethod", keywordProp);
        propertyMap.put("bodyBytesSent", intProp);
        propertyMap.put("requestTime", doubleProp);
        propertyMap.put("upstreamResponseTime", keywordProp);
        propertyMap.put("upstreamAddr", keywordProp);
        propertyMap.put("host", keywordProp);
        propertyMap.put("url", textKeywordProp);
        propertyMap.put("methodUrl", textKeywordProp);
        propertyMap.put("httpXForwardedFor", textKeywordProp);
        propertyMap.put("httpReferer", textKeywordProp);
        propertyMap.put("httpUserAgent", textKeywordProp);
        return propertyMap;
    }

    public Map<String, Property> getPropertyMapWithWenshu() {
        Map<String, Property> propertyMap = new HashMap<>();
        propertyMap.put("id", keywordProp);
        propertyMap.put("originalUrl", keywordProp);
        propertyMap.put("caseId", textPropIk);
        propertyMap.put("caseName", textPropIk);
        propertyMap.put("court", keywordProp);
        propertyMap.put("region", keywordProp);
        propertyMap.put("caseType", keywordProp);
        propertyMap.put("caseTypeId", intProp);
        propertyMap.put("judgmentDate", keywordProp);
        propertyMap.put("judgeDate", keywordProp);
        propertyMap.put("publicDate", keywordProp);
        propertyMap.put("parties", textPropIk);
        propertyMap.put("cause", textPropIk);
        propertyMap.put("legalBasis", textPropIk);
        propertyMap.put("fullText", textPropIk);
        return propertyMap;
    }

    public Map<String, Property> getPropertyMapWithVideoText(Class<?> clazz) {
        Map<String, Property> propertyMap = new HashMap<>();
        propertyMap.put("id", keywordProp);
        propertyMap.put("title", textPropIk);
        propertyMap.put("description", textPropIk);
        propertyMap.put("scope", intProp);
        propertyMap.put("publishTime", longProp);
        return propertyMap;
    }
}
