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

import com.caliverse.admin.Indicators.Indicatordomain.IndicatorsLog;
import com.caliverse.admin.Indicators.entity.MetaverseServerInfo;
import com.caliverse.admin.Indicators.indicatorrepository.IndicatorMetaverServerRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class IndicatorsMetaverseServerService {

    @Autowired private IndicatorMetaverServerRepository metaverServerRepository;

    public void collectMetaverseServerCount(String startTime, String endTime, int count){
        String logTimeStr = startTime.substring(0, 10);

        MetaverseServerInfo metaverseServerLog = new MetaverseServerInfo(logTimeStr, count);
        log.info("collectMetaverseServerCount MetaverseServer Log Save logDay: {}, ServerCount: {}", logTimeStr, metaverseServerLog.getServerCount());
        saveStatLogData(metaverseServerLog);
    }

    public void saveStatLogData(IndicatorsLog indicatorsLog) {

        if (indicatorsLog instanceof MetaverseServerInfo metaverseServerInfo) {
            try {
                metaverServerRepository.save(metaverseServerInfo);
            } catch (Exception e) {
                log.error("Error Repository Write MetaverseServerInfo: {}, Message: {}", metaverseServerInfo, e.getMessage());
            }
        } else {
            log.error("Not instanceof MetaverseServerInfo");
        }
    }
}
