package com.caliverse.admin.dynamodb.service;

import com.caliverse.admin.Indicators.entity.MoneyLogInfo;
import com.caliverse.admin.domain.datacomponent.MetaDataHandler;
import com.caliverse.admin.domain.entity.WorldEvent;
import com.caliverse.admin.domain.request.RankRequest;
import com.caliverse.admin.domain.request.WorldEventRequest;
import com.caliverse.admin.dynamodb.domain.atrrib.MoneyAttrib;
import com.caliverse.admin.dynamodb.domain.atrrib.UserNicknameRegistryAttrib;
import com.caliverse.admin.dynamodb.repository.*;
import com.caliverse.admin.global.common.annotation.DynamoDBTransaction;
import com.caliverse.admin.global.common.constants.AdminConstants;
import com.caliverse.admin.logs.Indicatordomain.StartEndTime;
import com.caliverse.admin.logs.logservice.LogServiceHelper;
import com.caliverse.admin.logs.logservice.indicators.IndicatorsMoneyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class DynamodbService {
    private final DynamoDBOperations DynamoDBOperations;
    private final UserNicknameRegistryRepository userNicknameRegistryRepository;
    private final MoneyRepository moneyRepository;
    private final SystemMetaMailRepository systemMetaMailRepository;
    private final MailRepository mailRepository;
    private final MailJsonRepository mailJsonRepository;
    private final WorldEventScheduleRepository worldEventScheduleRepository;

    private final IndicatorsMoneyService moneyService;

    private final MetaDataHandler metaDataHandler;

    public void saveUserMoney(){
        try {
            List<UserNicknameRegistryAttrib> userList = userNicknameRegistryRepository.findAllAttrib();
            StartEndTime startEndTime = LogServiceHelper.getCurrentLogSearchEndTime(AdminConstants.STAT_DAY_NUM);
            String logTime = startEndTime.getStartTime().substring(0, 10);

            userList.forEach(user -> {
                log.info(String.valueOf(user));
                String guid = user.getUserGuid();
                MoneyAttrib money = moneyRepository.findAttrib(guid);
                MoneyLogInfo moneyInfo = new MoneyLogInfo(logTime, guid, user.getNickname(),money.getGold(), money.getSapphire(), money.getCalium(), money.getRuby());
                moneyService.saveStatLogData(moneyInfo);
            });
        }catch(Exception e){
            log.error(e.getMessage());
        }
    }

    @DynamoDBTransaction
    public void insertWorldEventSchedule(WorldEventRequest worldEventRequest) {
        worldEventScheduleRepository.insert(worldEventRequest);
    }

    @DynamoDBTransaction
    public void updateWorldEventSchedule(WorldEventRequest worldEventRequest) {
        worldEventScheduleRepository.update(worldEventRequest);
    }

    @DynamoDBTransaction
    public void deleteWorldEventSchedule(WorldEvent worldEvent) {
        worldEventScheduleRepository.delete(worldEvent.getId().toString());
    }
}
