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

import com.caliverse.admin.domain.entity.HISTORYTYPE;
import com.caliverse.admin.domain.request.LandRequest;
import com.caliverse.admin.dynamodb.domain.atrrib.LandAuctionHighestBidUserAttrib;
import com.caliverse.admin.dynamodb.domain.atrrib.LandAuctionRegistryAttrib;
import com.caliverse.admin.dynamodb.domain.doc.LandAuctionActivityDoc;
import com.caliverse.admin.dynamodb.domain.doc.LandAuctionHighestBidUserDoc;
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.LandAuctionHighestBidUserRepository;
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.CommonConstants;
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.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class LandAuctionHighestBidUserRepositoryImpl extends BaseDynamoDBRepository<LandAuctionHighestBidUserDoc> implements LandAuctionHighestBidUserRepository {
    public LandAuctionHighestBidUserRepositoryImpl(DynamoDBOperations operations, DynamodbHistoryLogService dynamodbHistoryLogService, ObjectMapper objectMapper) {
        super(operations, LandAuctionHighestBidUserDoc.class, dynamodbHistoryLogService, objectMapper);
    }

    @Override
    public LandAuctionHighestBidUserAttrib findHighestBidUserAttrib(Integer landId, Integer auctionSeq) {
        Key key = Key.builder()
                .partitionValue(DynamoDBConstants.PK_KEY_LAND_AUCTION_HIGHEST_USER)
                .sortValue(String.format("%s#%s", landId, auctionSeq))
                .build();

        LandAuctionHighestBidUserDoc doc = findById(key);
        return doc != null ? doc.getAttribValue() : null;
    }

    @Override
    public void insertHighestBidUser(LandRequest landRequest) {
        try {
            LocalDateTime nowDate = LocalDateTime.now();

            LandAuctionHighestBidUserAttrib attrib = new LandAuctionHighestBidUserAttrib();
            attrib.setLandMetaId(landRequest.getLandId());
            attrib.setAuctionNumber(landRequest.getAuctionSeq());
            attrib.setBidCurrencyType(landRequest.getCurrencyType());
            attrib.setHighestBidPrice((double) 0);
            attrib.setHighestBidUserGuid("");
            attrib.setHighestBidUserNickname("");
            attrib.setNormalHighestBidPrice((double) 0);
            attrib.setNormalHighestBidUserGuid("");
            attrib.setNormalHighestBidUserNickname("");
            attrib.setHighestRankVersionTime(DynamoDBConstants.MIN_DATE);

            String sk = String.format("%s#%s", landRequest.getLandId(), landRequest.getAuctionSeq());

            LandAuctionHighestBidUserDoc registry = new LandAuctionHighestBidUserDoc();
            registry.setPK(DynamoDBConstants.PK_KEY_LAND_AUCTION_HIGHEST_USER);
            registry.setSK(sk);
            registry.setDocType(DynamoDBConstants.DOC_LANDAUCTION_HIGHEST_USER);
            registry.setAttribValue(attrib);
            registry.setCreatedDateTime(CommonUtils.convertUTCDate(nowDate));
            registry.setUpdatedDateTime(CommonUtils.convertUTCDate(nowDate));
            registry.setDeletedDateTime(DynamoDBConstants.MIN_DATE);
            registry.setRestoredDateTime(DynamoDBConstants.MIN_DATE);

            log.info("LandAuctionRegistryDoc Insert Success: {}", objectMapper.writeValueAsString(registry));

            dynamodbHistoryLogService.insertHistoryLog(
                    HISTORYTYPE.LAND_AUCTION_ADD,
                    HISTORYTYPE.LAND_AUCTION_ADD.name(),
                    registry
            );

            save(registry);
        }catch (Exception e){
            throw new RestApiException(CommonCode.ERROR.getHttpStatus(), ErrorCode.DYNAMODB_CONNECTION_ERROR.getMessage());
        }
    }

    @Override
    public DynamodbOperationResult initLandAuctionHighestBidUser(Integer landId, Integer auctionSeq) {

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

            LandAuctionHighestBidUserDoc doc = findById(key);

            if(doc != null) {
                delete(key);

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

}
