package com.caliverse.admin.dynamodb.repository.Impl;

import com.caliverse.admin.domain.entity.HISTORYTYPE;
import com.caliverse.admin.dynamodb.domain.doc.LandAuctionRefundBidPriceDoc;
import com.caliverse.admin.dynamodb.entity.DynamodbOperationResult;
import com.caliverse.admin.dynamodb.repository.BaseDynamoDBRepository;
import com.caliverse.admin.dynamodb.repository.LandAuctionRefundBidPriceRepository;
import com.caliverse.admin.dynamodb.service.DynamoDBOperations;
import com.caliverse.admin.global.common.code.CommonCode;
import com.caliverse.admin.global.common.code.ErrorCode;
import com.caliverse.admin.global.common.constants.DynamoDBConstants;
import com.caliverse.admin.global.common.exception.RestApiException;
import com.caliverse.admin.global.common.utils.CommonUtils;
import com.caliverse.admin.history.service.DynamodbHistoryLogService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import software.amazon.awssdk.enhanced.dynamodb.Key;

import java.util.List;

@Component
@Slf4j
public class LandAuctionRefundBidPriceRepositoryImpl extends BaseDynamoDBRepository<LandAuctionRefundBidPriceDoc> implements LandAuctionRefundBidPriceRepository {

    public LandAuctionRefundBidPriceRepositoryImpl(DynamoDBOperations operations, DynamodbHistoryLogService historyLogService, ObjectMapper objectMapper) {
        super(operations, LandAuctionRefundBidPriceDoc.class, historyLogService, objectMapper);
    }

    @Override
    public DynamodbOperationResult initLandAuctionRefundBidPrice(String guid, Integer landId, Integer auctionSeq) {

        try{
            Key key = Key.builder()
                    .partitionValue(DynamoDBConstants.PK_KEY_LAND_AUCTION_REFUND_BID_PRICE + guid)
                    .sortValue(String.format("%s#%s", landId, auctionSeq))
                    .build();

            LandAuctionRefundBidPriceDoc doc = findById(key);

            if(doc != null) {

                delete(key);

                log.info("LandAuctionRefundBidPriceDoc Delete Success: {}", objectMapper.writeValueAsString(doc));

                dynamodbHistoryLogService.deleteHistoryLog(
                        HISTORYTYPE.LAND_AUCTION_INITIALIZE,
                        HISTORYTYPE.LAND_AUCTION_INITIALIZE.name(),
                        doc
                );
                return new DynamodbOperationResult(true, "delete success", doc);
            }
            return new DynamodbOperationResult(true, "null", doc);
        }catch (Exception e){
            log.error("Init LandAuctionRefundBidPrice Error: {}", e.getMessage());
            return new DynamodbOperationResult(false, e.getMessage(), null);
        }
    }
}
