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

import java.util.Optional;

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 com.arms.egovframework.javaservice.esframework.model.dto.esquery.SingleValueGroupDTO;
import lombok.Getter;

import org.opensearch.search.aggregations.BucketOrder;
import org.opensearch.search.aggregations.bucket.histogram.DateHistogramInterval;
import org.opensearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder;

@Getter
public class DateAggregationQueryCreator extends AggregationQueryBuilder<DateHistogramAggregationBuilder> {

    private DateAggregationQueryCreator(MainGroupDTO mainGroupDTO, EsQuery esQuery, DateHistogramInterval dateHistogramInterval){
        super(
            mainGroupDTO
            ,esQuery
            ,new DateHistogramAggregationBuilder(Optional.ofNullable(mainGroupDTO.getMainFieldAlias()).orElseGet(() ->"date_group_by_" + mainGroupDTO.getMainField()))
                .field(mainGroupDTO.getMainField())
                .calendarInterval(dateHistogramInterval)
                .minDocCount(0)
                .order(BucketOrder.count(mainGroupDTO.isAscending()))
        );
    }

    private DateAggregationQueryCreator(SingleValueGroupDTO singleValueGroupDTO, EsQuery esQuery, DateHistogramInterval dateHistogramInterval){
        super(
            singleValueGroupDTO
            ,esQuery
            ,new DateHistogramAggregationBuilder(Optional.ofNullable(singleValueGroupDTO.getMainFieldAlias()).orElseGet(() ->"date_group_by_" + singleValueGroupDTO.getMainField()))
                .field(singleValueGroupDTO.getMainField())
                .calendarInterval(dateHistogramInterval)
                .minDocCount(0)
                .order(BucketOrder.count(singleValueGroupDTO.isAscending()))
        );
    }

    public static DateAggregationQueryCreator week(EsQuery esQuery){
        return new DateAggregationQueryCreator(new MainGroupDTO() {},esQuery,DateHistogramInterval.WEEK);
    }

    public static DateAggregationQueryCreator week(MainGroupDTO mainGroupDTO, EsQuery esQuery){
        return new DateAggregationQueryCreator(mainGroupDTO,esQuery,DateHistogramInterval.WEEK);
    }

    public static DateAggregationQueryCreator day(EsQuery esQuery){
        return new DateAggregationQueryCreator(new MainGroupDTO() {},esQuery,DateHistogramInterval.DAY);
    }

    public static DateAggregationQueryCreator day(MainGroupDTO mainGroupDTO, EsQuery esQuery){
        return new DateAggregationQueryCreator(mainGroupDTO,esQuery,DateHistogramInterval.DAY);
    }

    public static DateAggregationQueryCreator day(SingleValueGroupDTO singleValueGroupDTO, EsQuery esQuery){
        return new DateAggregationQueryCreator(singleValueGroupDTO,esQuery,DateHistogramInterval.DAY);
    }

    public static DateAggregationQueryCreator month(EsQuery esQuery){
        return new DateAggregationQueryCreator(new MainGroupDTO() {},esQuery,DateHistogramInterval.MONTH);
    }

    public static DateAggregationQueryCreator month(MainGroupDTO mainGroupDTO, EsQuery esQuery){
        return new DateAggregationQueryCreator(mainGroupDTO,esQuery,DateHistogramInterval.MONTH);
    }

}
