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

import com.caliverse.admin.Indicators.Indicatordomain.IndicatorsLog;
import com.caliverse.admin.Indicators.Indicatorsservice.base.IndicatorsLogLoadServiceBase;
import com.caliverse.admin.global.common.constants.AdminConstants;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Sort;
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.ConditionalOperators;
import org.springframework.data.mongodb.core.aggregation.ProjectionOperation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IndicatorsNruLoadService extends IndicatorsLogLoadServiceBase {
    public IndicatorsNruLoadService(
            @Qualifier("mongoIndicatorTemplate") MongoTemplate mongoTemplate
    ) {
        super(mongoTemplate);
    }

    public <T extends IndicatorsLog> T getDailyIndicatorLog(String date, Class<T> clazz) {
        Criteria criteria = Criteria.where(AdminConstants.MONGO_DB_KEY_LOGDAY).is(date);

        ProjectionOperation projection = Aggregation.project()
                .andInclude(AdminConstants.MONGO_DB_COLLECTION_NRU, AdminConstants.MONGO_DB_KEY_LOGDAY)
                .and(ConditionalOperators.ifNull("$" + AdminConstants.MONGO_DB_COLLECTION_NRU).then(0))
                .as(AdminConstants.MONGO_DB_COLLECTION_NRU);

        List<AggregationOperation> operations = List.of(
                Aggregation.match(criteria),
                projection
        );

        Aggregation aggregation = Aggregation.newAggregation(operations);

        List<T> results = mongoTemplate.aggregate(aggregation, AdminConstants.MONGO_DB_COLLECTION_NRU, clazz)
                .getMappedResults();

        return results.get(0);

//        return mongoTemplate.aggregate(aggregation, AdminConstants.MONGO_DB_COLLECTION_NRU, clazz)
//                .getUniqueMappedResult();  // 단일 결과만 반환
    }

    @Override
    public <T extends IndicatorsLog> List<T> getIndicatorsLogData(String startTime, String endTime, Class<T> clazz) {
        Criteria criteria = makeCriteria(startTime, endTime, AdminConstants.MONGO_DB_KEY_LOGDAY);

        ProjectionOperation projection = Aggregation.project()
                .andInclude(AdminConstants.MONGO_DB_COLLECTION_NRU, AdminConstants.MONGO_DB_KEY_LOGDAY)
                .and(ConditionalOperators.ifNull("$" + AdminConstants.MONGO_DB_COLLECTION_NRU).then(0))
                .as(AdminConstants.MONGO_DB_COLLECTION_NRU);

        List<AggregationOperation> operations = List.of(
                Aggregation.match(criteria),
                projection,
                Aggregation.sort(Sort.Direction.ASC, AdminConstants.MONGO_DB_KEY_LOGDAY)
        );

        Aggregation aggregation = Aggregation.newAggregation(operations);

        return mongoTemplate.aggregate(aggregation, AdminConstants.MONGO_DB_COLLECTION_NRU, clazz).getMappedResults();
    }
}
