package com.caliverse.admin.Indicators.Indicatorsservice.base;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.aggregation.ProjectionOperation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;

import com.caliverse.admin.global.common.constants.AdminConstants;


@Service
public abstract class IndicatorsLogLoadServiceBase implements IndicatorsLogLoadService {

    protected final MongoTemplate mongoTemplate;

    public IndicatorsLogLoadServiceBase(
            @Qualifier("mongoIndicatorTemplate") MongoTemplate mongoTemplate
    ) {
        this.mongoTemplate = mongoTemplate;
    }

    protected Criteria makeCriteria(String startDate, String endDate, String dateFieldName) {
        return new Criteria()
                .andOperator(
                        Criteria.where(dateFieldName).gte(startDate),
                        Criteria.where(dateFieldName).lt(endDate)
                );
    }

    public Criteria makeCriteria(String startDate, String endDate)
    {
        return makeCriteria(startDate, endDate, AdminConstants.MONGO_DB_KEY_LOGTIME);
    }
    
    // 24.12.13 현재 사용안함
    private AggregationOperation getDefaultProjectOperationName(){
        ProjectionOperation projectOperation = Aggregation.project()
            .and(AdminConstants.MONGO_DB_KEY_LOGDAY).as(AdminConstants.MONGO_DB_KEY_LOGDAY)
            .and(AdminConstants.INDICATORS_KEY_DAU_BY_LANG).as(AdminConstants.INDICATORS_KEY_DAU_BY_LANG)
        ;
        return projectOperation;
    }

    // 24.12.13 현재 사용안함
    protected List<AggregationOperation> setDefaultOperation(Criteria criteria){

        List<AggregationOperation> operations = new ArrayList<>();
        
        operations.add(Aggregation.match(criteria));
        operations.add(getDefaultProjectOperationName());

        return operations;
    }


    

    

}

