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.metadata.MetaLandData;
import com.caliverse.admin.domain.entity.metadata.MetaSystemMailData;
import com.caliverse.admin.domain.request.LandRequest;
import com.caliverse.admin.dynamodb.domain.atrrib.MoneyAttrib;
import com.caliverse.admin.dynamodb.domain.atrrib.UserNicknameRegistryAttrib;
import com.caliverse.admin.dynamodb.entity.MailItem;
import com.caliverse.admin.dynamodb.repository.MailRepository;
import com.caliverse.admin.dynamodb.repository.MoneyRepository;
import com.caliverse.admin.dynamodb.repository.SystemMetaMailRepository;
import com.caliverse.admin.dynamodb.repository.UserNicknameRegistryRepository;
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.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 java.time.LocalDateTime;
import java.util.ArrayList;
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 IndicatorsMoneyService moneyService;

    private final ObjectMapper mapper = new ObjectMapper();
    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 insertSystemMail(Event event){
        systemMetaMailRepository.insert(event);
    }

    @DynamoDBTransaction
    public void insertLandChangesMail(LandRequest landRequest){
        MetaSystemMailData systemMailData = metaDataHandler.getMetaSystemMailListData().stream()
                .filter(data -> data.getKey().equals(CommonConstants.SYSTEM_MAIL_LAND_TRANS_KEY)).findFirst().orElse(null);
        MetaLandData landData = metaDataHandler.getMetaLandData(landRequest.getLandId());

        List<String> arguments = new ArrayList<>();
        arguments.add(landRequest.getUserName());
        arguments.add(landData.getLandName());

        String guid = landRequest.getUserGuid();
        String nickname = landRequest.getUserName();
        List<MailItem> items = new ArrayList<>();
        items.add(MailItem.builder()
                .itemId(landData.getLinkedItem())
                .count(1.0)
                .productId(0)
                .isRepeatProduct(false)
                .build()
        );

        mailRepository.insertRecvSystemMail(guid, nickname, items, systemMailData, arguments);
    }
}
