package com.caliverse.admin.dynamodb.service;

import com.caliverse.admin.domain.datacomponent.MetaDataHandler;
import com.caliverse.admin.domain.entity.metadata.MetaLandData;
import com.caliverse.admin.domain.service.LandService;
import com.caliverse.admin.dynamodb.domain.atrrib.LandAttrib;
import com.caliverse.admin.dynamodb.domain.atrrib.LandAuctionRegistryAttrib;
import com.caliverse.admin.dynamodb.domain.doc.LandAuctionRefundBidPriceDoc;
import com.caliverse.admin.dynamodb.domain.doc.LandAuctionRegistryDoc;
import com.caliverse.admin.dynamodb.repository.*;
import com.caliverse.admin.global.common.constants.DynamoDBConstants;
import com.caliverse.admin.global.component.transaction.TransactionIdManager;
import com.caliverse.admin.history.service.DataInitializeHistoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import software.amazon.awssdk.enhanced.dynamodb.Key;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class DynamodbDataService {
    private final LandAuctionRegistryRepository landAuctionRegistryRepository;
    private final LandAuctionActivityRepository landAuctionActivityRepository;
    private final LandAuctionRecordRepository landAuctionRecordRepository;
    private final LandAuctionHighestBidUserRepository landAuctionHighestBidUserRepository;
    private final LandAuctionRefundBidPriceRepository landAuctionRefundBidPriceRepository;
    private final LandRepository landRepository;
    private final BuildingRepository buildingRepository;
    private final OwnedBuildingRepository ownedBuildingRepository;
    private final OwnedLandRepository ownedLandRepository;
    private final MetaDataHandler metaDataHandler;
    private final TransactionIdManager transactionIdManager;
    private final DataInitializeHistoryService dataInitializeHistoryService;
    private final DynamodbLandService dynamodbLandService;
    private final LandService landService;
    private final DynamodbLandAuctionService dynamodbLandAuctionService;

    public void InitDataLandAuction(){
        Key key = Key.builder()
                .partitionValue(DynamoDBConstants.PK_KEY_LAND_AUCTION_REGISTRY)
                .build();
        List<LandAuctionRegistryDoc> registryList = landAuctionRegistryRepository.findAll(key);
        registryList.forEach(data -> {
            LandAuctionRegistryAttrib attrib = data.getAttribValue();
            dynamodbLandAuctionService.initLandAuction(attrib.getLandMetaId(), attrib.getAuctionNumber(), "");
        });
    }

    public void InitDataLandOwned(){
        List<MetaLandData> landData = metaDataHandler.getMetaLandListData();
        landData.forEach(land -> {
            LandAttrib landAttrib = landRepository.findLandAttrib(land.getLandId());
            if(landAttrib != null){
                String owner_guid = landAttrib.getOwnerUserGuid();
                if(!owner_guid.isEmpty()){
                    dynamodbLandService.initLandOwner(land, owner_guid);
                }
            }
        });
    }

    public void InitDataLandDesc(){
        List<MetaLandData> landData = metaDataHandler.getMetaLandListData();
        landData.forEach(land -> {
            LandAttrib landAttrib = landRepository.findLandAttrib(land.getLandId());
            if(landAttrib != null){
                dynamodbLandService.initLandDesc(land);
            }
        });
    }


}
