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.Event;
import com.caliverse.admin.domain.entity.SEARCHTYPE;
import com.caliverse.admin.domain.entity.metadata.MetaLandData;
import com.caliverse.admin.domain.entity.metadata.MetaSystemMailData;
import com.caliverse.admin.domain.request.LandRequest;
import com.caliverse.admin.dynamodb.domain.atrrib.MailItemAttrib;
import com.caliverse.admin.dynamodb.domain.atrrib.MailJsonAttrib;
import com.caliverse.admin.dynamodb.domain.atrrib.MoneyAttrib;
import com.caliverse.admin.dynamodb.domain.atrrib.UserNicknameRegistryAttrib;
import com.caliverse.admin.dynamodb.domain.doc.MailDoc;
import com.caliverse.admin.dynamodb.domain.doc.MailJsonDoc;
import com.caliverse.admin.dynamodb.dto.PageResult;
import com.caliverse.admin.dynamodb.entity.KeyParam;
import com.caliverse.admin.dynamodb.entity.MailItem;
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.global.common.constants.CommonConstants;
import com.caliverse.admin.global.common.constants.DynamoDBConstants;
import com.caliverse.admin.global.common.utils.CommonUtils;
import com.caliverse.admin.logs.Indicatordomain.StartEndTime;
import com.caliverse.admin.logs.logservice.LogServiceHelper;
import com.caliverse.admin.logs.logservice.indicators.IndicatorsMoneyService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import software.amazon.awssdk.enhanced.dynamodb.Key;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;

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

@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 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());
        }
    }
}
