package com.caliverse.admin.dynamodb.service;

import com.caliverse.admin.domain.entity.BattleEvent;
import com.caliverse.admin.domain.request.BattleEventRequest;
import com.caliverse.admin.dynamodb.domain.atrrib.BattleEventAttrib;
import com.caliverse.admin.dynamodb.domain.atrrib.SystemMetaMailAttrib;
import com.caliverse.admin.dynamodb.domain.doc.BattleEventDoc;
import com.caliverse.admin.dynamodb.domain.doc.SystemMetaMailDoc;
import com.caliverse.admin.dynamodb.entity.EOncePeriodRangeType;
import com.caliverse.admin.dynamodb.repository.BattleEventRepository;
import com.caliverse.admin.global.common.annotation.DynamoDBTransaction;
import com.caliverse.admin.global.common.constants.DynamoDBConstants;
import com.caliverse.admin.global.common.utils.CommonUtils;
import com.caliverse.admin.global.common.utils.DateUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import software.amazon.awssdk.enhanced.dynamodb.Key;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
@Slf4j
public class DynamodbBattleEventService {
    private final BattleEventRepository battleEventRepository;

    @DynamoDBTransaction
    public void insertBattleEvent(BattleEventRequest battleEventRequest) {
        battleEventRepository.insert(battleEventRequest);
    }

    @DynamoDBTransaction
    public void updateBattleEvent(BattleEventRequest battleEventRequest) {
        battleEventRepository.update(battleEventRequest);
    }

    public int getEventId(){
        return battleEventRepository.findEventId();
    }

    @DynamoDBTransaction
    public void updateStopBattleEvent(BattleEvent battleEvent) {
        battleEventRepository.updateStop(battleEvent);
    }

    @DynamoDBTransaction
    public void oldBattleEventClear(){
        Key key = Key.builder()
                .partitionValue(DynamoDBConstants.PK_KEY_BATTLE_EVENT)
                .build();
        List<BattleEventDoc> eventList = battleEventRepository.findAll(key);
        eventList.forEach(event -> {
            try {
                BattleEventAttrib attrib = CommonUtils.stringByObject(event.getAttribValue(), BattleEventAttrib.class);
                EOncePeriodRangeType oncePeriodRangeType = attrib.getOncePeriodType();
                LocalDateTime startTime = DateUtils.stringToDateTime(attrib.getStartDay());
                LocalDateTime expiredTime = LocalDateTime.now().minusMonths(1);
                if (Objects.requireNonNull(oncePeriodRangeType) == EOncePeriodRangeType.NONE) {
                    if (startTime.isBefore(expiredTime)) {
                        battleEventRepository.delete(event.getSK());
                    }
                } else {
                    LocalDateTime endDate = DateUtils.stringToDateTime(attrib.getEndDate());
                    if (endDate.isBefore(expiredTime)) {
                        battleEventRepository.delete(event.getSK());
                    }
                }

            }catch (Exception e){
                log.error("oldSystemMetaMail Clear Fail: {}",e.getMessage());
            }
        });
    }

}
