package com.caliverse.admin.logs.logservice.indicators;

import com.caliverse.admin.Indicators.Indicatordomain.IndicatorsLog;
import com.caliverse.admin.Indicators.entity.DauLogInfo;
import com.caliverse.admin.Indicators.indicatorrepository.IndicatorDauRepository;
import com.caliverse.admin.logs.Indicatordomain.DauMongoLog;
import com.caliverse.admin.logs.logservice.businesslogservice.BusinessLogDauService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Slf4j
@Service
public class IndicatorsDauService{

    @Autowired private BusinessLogDauService businessLogDauService;
    @Autowired private IndicatorDauRepository dauRepository;

    public void collectDailyActiveUser(String startTime, String endTime){
        String logTimeStr = startTime.substring(0, 10);
        List<DauMongoLog> indicatorLog = businessLogDauService.loadBusinessLogData(startTime, endTime, DauMongoLog.class);

        if (indicatorLog == null || indicatorLog.isEmpty()) {
            DauLogInfo dauLog = new DauLogInfo(logTimeStr, 0);
            log.info("collectDailyActiveUser DAU Log Save logDay: {}, Dau: {}", logTimeStr, dauLog.getDau());
            saveStatLogData(dauLog);
            return;
        }

        for(DauMongoLog mongo_log : indicatorLog){
            String logDay = mongo_log.getLogDay();

            DauLogInfo dauLog = new DauLogInfo(logDay, mongo_log.getAccountIdListCount());
            log.info("collectDailyActiveUser DAU Log Save logDay: {}, Dau: {}", logDay, dauLog.getDau());
            saveStatLogData(dauLog);
        }
    }

    public void saveStatLogData(IndicatorsLog indicatorsLog) {

        if (indicatorsLog instanceof DauLogInfo) {
            DauLogInfo dau_log_info = (DauLogInfo) indicatorsLog;
            try {
                dauRepository.save(dau_log_info);
            } catch (Exception e) {
                log.error("Error Repository Write DAUInfo: {}, Message: {}", dau_log_info, e.getMessage());
            }
        } else {
            log.error("Not instanceof DauLogInfo");
        }
    }

    /**
     * 특정 날짜의 DAU 정보를 조회합니다.
     * @param date 조회할 날짜 (형식: "YYYY-MM-DD")
     * @return 해당 날짜의 DAU 정보, 없을 경우 null
     */
//    public DauLogInfo getDauLogData(LocalDate date) {
//        try {
//            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
//            String logDay = date.format(formatter);
//            List<DauLogInfo> dauLogInfo = dauRepository.findByLogDay(logDay);
//            if (dauLogInfo == null) {
//                log.info("No DAU data found for date: {}", logDay);
//                return null;
//            }
//            return dauLogInfo.isEmpty() ? null : dauLogInfo.get(0);
//        } catch (Exception e) {
//            log.error("Error while fetching DAU for date: {}", date, e);
//            throw e;
//        }
//    }
}
