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.headless.api.pojo.DataSetDetail;
import com.purui.prims.headless.api.pojo.DataSetModelConfig;
import com.purui.prims.headless.api.pojo.QueryConfig;
import com.purui.prims.headless.api.pojo.SchemaItem;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class DataSetResp extends SchemaItem {

    @ApiModelProperty(value = "主题域ID")
    private Long domainId;

    @ApiModelProperty(value = "数据集详情")
    private DataSetDetail dataSetDetail;

    @ApiModelProperty(value = "数据集别名")
    private String alias;

    @ApiModelProperty(value = "过滤SQL")
    private String filterSql;

    @ApiModelProperty(value = "数据集查询配置")
    private QueryConfig queryConfig = new QueryConfig();

    @ApiModelProperty(value = "所有维度")
    private List<DimensionResp> dimensionList;

    @ApiModelProperty(value = "所有指标")
    private List<MetricResp> metricList;

    @ApiModelProperty(value = "模型关联关系")
    private List<ModelRela> modelRelas;

    public List<Long> metricIds() {
        return getDataSetModelConfigs().stream().map(DataSetModelConfig::getMetrics)
                .flatMap(Collection::stream).collect(Collectors.toList());
    }

    public List<Long> dimensionIds() {
        return getDataSetModelConfigs().stream().map(DataSetModelConfig::getDimensions)
                .flatMap(Collection::stream).collect(Collectors.toList());
    }

    public List<Long> getAllModels() {
        return getDataSetModelConfigs().stream().map(DataSetModelConfig::getId)
                .collect(Collectors.toList());
    }

    public List<Long> getAllIncludeAllModels() {
        return getDataSetModelConfigs().stream().filter(DataSetModelConfig::getIncludesAll)
                .map(DataSetModelConfig::getId)
                .collect(Collectors.toList());
    }

    private List<DataSetModelConfig> getDataSetModelConfigs() {
        if (dataSetDetail == null || CollectionUtils.isEmpty(dataSetDetail.getDataSetModelConfigs())) {
            return Lists.newArrayList();
        }
        return dataSetDetail.getDataSetModelConfigs();
    }

}
