package com.purui.prims.headless.chat.query.rule.detail;

import com.purui.prims.common.pojo.Order;
import com.purui.prims.headless.api.pojo.DataSetSchema;
import com.purui.prims.headless.api.pojo.SchemaElement;
import com.purui.prims.headless.api.pojo.SemanticParseInfo;
import com.purui.prims.headless.chat.ChatContext;
import com.purui.prims.headless.chat.ChatQueryContext;

import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;

public abstract class DetailListQuery extends DetailSemanticQuery {

    @Override
    public void fillParseInfo(ChatQueryContext queryContext, ChatContext chatContext) {
        super.fillParseInfo(queryContext, chatContext);
        this.addEntityDetailAndOrderByMetric(queryContext, parseInfo);
    }

    private void addEntityDetailAndOrderByMetric(ChatQueryContext queryContext, SemanticParseInfo parseInfo) {
        Long dataSetId = parseInfo.getDataSetId();
        if (Objects.nonNull(dataSetId) && dataSetId > 0L) {
            DataSetSchema dataSetSchema = queryContext.getSemanticSchema().getDataSetSchemaMap().get(dataSetId);
            if (dataSetSchema != null && Objects.nonNull(dataSetSchema.getEntity())) {
                Set<SchemaElement> dimensions = new LinkedHashSet<>();
                Set<SchemaElement> metrics = new LinkedHashSet<>();
                Set<Order> orders = new LinkedHashSet<>();
                // TODO 增加默认排序和默认维度
                parseInfo.setDimensions(dimensions);
                parseInfo.setMetrics(metrics);
                parseInfo.setOrders(orders);
            }
        }
    }

}
