package com.caliverse.admin.mongodb.repository;

import com.caliverse.admin.domain.entity.log.LogAction;
import com.caliverse.admin.domain.entity.log.LogCategory;
import com.caliverse.admin.domain.entity.log.LogStatus;
import com.caliverse.admin.mongodb.domain.BusinessLog;
import org.springframework.data.mongodb.repository.Aggregation;
import org.springframework.data.mongodb.repository.MongoRepository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

public interface BusinessLogRepository extends MongoRepository<BusinessLog, String> {
    List<BusinessLog> findByCategoryAndLogTimeBetween(
            LogCategory category, LocalDateTime logTime, LocalDateTime logTime2);

    List<BusinessLog> findByActionAndLogTimeBetween(
            LogAction action, LocalDateTime logTime, LocalDateTime logTime2);

    List<BusinessLog> findByStatusAndLogTimeBetween(
            LogStatus status, LocalDateTime logTime, LocalDateTime logTime2);

    List<BusinessLog> findByProcIdOrderByLogTimeAsc(String tranId);

    List<BusinessLog> findByCategoryAndActionAndLogTimeBetween(
            LogCategory category, LogAction action, LocalDateTime logTime, LocalDateTime logTime2);

    // 집계 쿼리
    @Aggregation(pipeline = {
            "{ $match: { 'LogTime': { $gte: ?0, $lte: ?1 } } }",
            "{ $group: { _id: { category: '$category', action: '$action' }, count: { $sum: 1 } } }",
            "{ $sort: { count: -1 } }"
    })
    List<Map<String, Object>> getCategoryActionStats(LocalDateTime start, LocalDateTime end);
}
