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

import com.caliverse.admin.domain.entity.HISTORYTYPE;
import com.caliverse.admin.dynamodb.domain.doc.LandAuctionActivityDoc;
import com.caliverse.admin.dynamodb.domain.doc.LandAuctionRecordDoc;
import com.caliverse.admin.dynamodb.domain.doc.LandAuctionRegistryDoc;
import com.caliverse.admin.dynamodb.entity.DynamodbOperationResult;
import com.caliverse.admin.dynamodb.repository.BaseDynamoDBRepository;
import com.caliverse.admin.dynamodb.repository.LandAuctionRecordRepository;
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 LandAuctionRecordRepositoryImpl extends BaseDynamoDBRepository<LandAuctionRecordDoc> implements LandAuctionRecordRepository {
    public LandAuctionRecordRepositoryImpl(DynamoDBOperations operations, DynamodbHistoryLogService dynamodbHistoryLogService, ObjectMapper objectMapper) {
        super(operations, LandAuctionRecordDoc.class, dynamodbHistoryLogService, objectMapper);
    }

    @Override
    public DynamodbOperationResult initLandAuctionRecord(Integer landId) {

        try{
            Key key = Key.builder()
                    .partitionValue(DynamoDBConstants.PK_KEY_LAND_AUCTION_RECORD)
                    .sortValue(String.valueOf(landId))
                    .build();
            LandAuctionRecordDoc doc = findById(key);

            if(doc != null) {

                delete(key);

                log.info("LandAuctionRecordDoc 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 LandAuctionRecord Error: {}", e.getMessage());
            return new DynamodbOperationResult(false, e.getMessage(), null);
        }
    }
}
