package com.purui.prims.headless.api.pojo.response;

import com.google.common.collect.Lists;
import com.purui.prims.common.pojo.ModelRela;
import com.purui.prims.common.pojo.enums.QueryType;
import com.purui.prims.headless.api.pojo.enums.SchemaType;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.purui.prims.common.pojo.Constants.UNDERLINE;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class SemanticSchemaResp {

    private Long dataSetId;
    private List<Long> modelIds;
    private SchemaType schemaType;
    private List<MetricSchemaResp> metrics = Lists.newArrayList();
    private List<DimSchemaResp> dimensions = Lists.newArrayList();
    // 保留所有指标，用于后续查询或计算
    private List<MetricResp> allMetrics = Lists.newArrayList();
    private List<ModelRela> modelRelas = Lists.newArrayList();
    private List<ModelResp> modelResps = Lists.newArrayList();
    private DataSetResp dataSetResp;
    private DatabaseResp databaseResp;
    private QueryType queryType;

    public String getSchemaKey() {
        if (dataSetId == null) {
            return String.format("%s_%s", schemaType, StringUtils.join(modelIds, UNDERLINE));
        }
        return String.format("%s_%s", schemaType, dataSetId);

    }

    public MetricSchemaResp getMetric(String bizName) {
        return metrics.stream().filter(metric -> bizName.equalsIgnoreCase(metric.getBizName()))
                .findFirst().orElse(null);
    }

    public MetricSchemaResp getMetric(Long id) {
        return metrics.stream().filter(metric -> id.equals(metric.getId()))
                .findFirst().orElse(null);
    }

    public DimSchemaResp getDimension(String bizName) {
        return dimensions.stream().filter(dimension -> bizName.equalsIgnoreCase(dimension.getBizName()))
                .findFirst().orElse(null);
    }

    public DimSchemaResp getDimension(Long id) {
        return dimensions.stream().filter(dimension -> id.equals(dimension.getId()))
                .findFirst().orElse(null);
    }

    public Set<String> getNameFromBizNames(Set<String> bizNames) {
        Set<String> names = new HashSet<>();
        for (String bizName : bizNames) {
            DimSchemaResp dimSchemaResp = getDimension(bizName);
            if (dimSchemaResp != null) {
                names.add(dimSchemaResp.getName());
                continue;
            }
            MetricSchemaResp metricSchemaResp = getMetric(bizName);
            if (metricSchemaResp != null) {
                names.add(metricSchemaResp.getName());
            }
        }
        return names;
    }
}