package com.caliverse.admin.scheduler.polling.service;

import com.caliverse.admin.domain.entity.DataInit;
import com.caliverse.admin.domain.entity.EInitDataType;
import com.caliverse.admin.domain.entity.LandAuction;
import com.caliverse.admin.domain.entity.LandOwnerChange;
import com.caliverse.admin.domain.entity.log.LogAction;
import com.caliverse.admin.domain.request.LandRequest;
import com.caliverse.admin.domain.service.DataService;
import com.caliverse.admin.domain.service.LandService;
import com.caliverse.admin.domain.service.UserGameSessionService;
import com.caliverse.admin.dynamodb.service.DynamodbDataService;
import com.caliverse.admin.dynamodb.service.DynamodbLandAuctionService;
import com.caliverse.admin.dynamodb.service.DynamodbLandService;
import com.caliverse.admin.dynamodb.service.DynamodbService;
import com.caliverse.admin.global.common.annotation.BusinessProcess;
import com.caliverse.admin.global.component.manager.BusinessProcessIdManager;
import com.caliverse.admin.mongodb.service.BusinessLogService;
import com.caliverse.admin.scheduler.CommonScheduler;
import com.caliverse.admin.scheduler.entity.SchedulerType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

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

@Slf4j
@Component
public class DataInitializeScheduler extends CommonScheduler {

    private final DataService dataService;
    private final DynamodbDataService dynamodbDataService;
    private final LandService landService;
    private final DynamodbLandAuctionService dynamodbLandAuctionService;
    @Value("${spring.profiles.active}")
    private String activeProfile;

    public DataInitializeScheduler(DataService dataService,
                                   DynamodbDataService dynamodbDataService,
                                   LandService landService,
                                   DynamodbLandAuctionService dynamodbLandAuctionService,
                                   BusinessLogService businessLogService,
                                   BusinessProcessIdManager processIdManager) {
        super(businessLogService, processIdManager);
        this.dataService = dataService;
        this.dynamodbDataService = dynamodbDataService;
        this.landService = landService;
        this.dynamodbLandAuctionService = dynamodbLandAuctionService;
    }

    @Override
    protected void executeInternal() {
        executeWithProcessId(LogAction.SCHEDULE_DATA_INIT, this::executeLogic);
    }

    private void executeLogic() {
        List<DataInit> scheduleList = dataService.getScheduleDataInitList();

        scheduleList.forEach(dataInit -> {
            EInitDataType dataType = dataInit.getDataType();
            dataService.updateStatus(dataInit.getId(), DataInit.DATA_INIT_STATUS.RUNNING);
            switch (dataType) {
                case LandAuction -> initLandAuction(dataInit.getCreateDt());
                case LandOwned -> initLandOwner(dataInit.getCreateDt());
                case LandDesc -> initLandDesc();
            }
            dataService.updateStatus(dataInit.getId(), DataInit.DATA_INIT_STATUS.FINISH);
        });
    }

    private void initLandAuction(LocalDateTime createDt){
        List<LandAuction> landAuctions = landService.getAllLandAuctionList();
        landAuctions.forEach(landAuction -> {
            dataService.initLandAuction(landAuction);
            dynamodbLandAuctionService.initLandAuction(landAuction.getLandId(), landAuction.getAuctionSeq(), landAuction.getBidderGuid());
        });
        //치트로 있는 데이터 삭제
        dynamodbDataService.InitDataLandAuction();
    }

    private void initLandDesc(){
        dynamodbDataService.InitDataLandDesc();
    }

    private void initLandOwner(LocalDateTime createDt){
        List<LandOwnerChange> landOwnerChanges = landService.getAllLandOwnerChangesList();
        landOwnerChanges.forEach(dataService::initLandOwned);
        dynamodbDataService.InitDataLandOwned();
    }

    @Override
    public SchedulerType getSchedulerType() {
        return SchedulerType.DATA_INITIALIZE;
    }
}
