package com.arms.egovframework.javaservice.esframework.factory.creator;

import com.arms.egovframework.javaservice.esframework.esquery.EsQuery;
import com.arms.egovframework.javaservice.esframework.factory.builder.AggregationQueryBuilder;
import com.arms.egovframework.javaservice.esframework.model.dto.esquery.MainGroupDTO;
import lombok.Getter;
import org.opensearch.search.aggregations.AggregationBuilders;
import org.opensearch.search.aggregations.metrics.SumAggregationBuilder;

import java.util.Optional;

@Getter
public class AggregationQuerySumCreator extends AggregationQueryBuilder<SumAggregationBuilder> {

    private AggregationQuerySumCreator(MainGroupDTO mainGroupDTO, EsQuery esQuery){
        super(
            mainGroupDTO
            ,esQuery
            ,AggregationBuilders
                .sum(Optional.ofNullable(mainGroupDTO.getMainFieldAlias()).orElseGet(() -> "group_by_" + mainGroupDTO.getMainField()))
                .field(mainGroupDTO.getMainField())
        );
    }

    public static AggregationQuerySumCreator of(EsQuery esQuery){
        return new AggregationQuerySumCreator(new MainGroupDTO(),esQuery);
    }

    public static AggregationQuerySumCreator of(MainGroupDTO mainGroupDTO, EsQuery esQuery){
        return new AggregationQuerySumCreator(mainGroupDTO,esQuery);
    }

}
