package com.caliverse.admin.dynamodb.service;

import com.caliverse.admin.domain.entity.LandAuction;
import com.caliverse.admin.domain.request.LandRequest;
import com.caliverse.admin.dynamodb.domain.doc.LandAuctionRecordDoc;
import com.caliverse.admin.dynamodb.repository.LandAuctionRecordRepository;
import com.caliverse.admin.global.common.annotation.DynamoDBTransaction;
import com.caliverse.admin.dynamodb.domain.atrrib.LandAuctionHighestBidUserAttrib;
import com.caliverse.admin.dynamodb.domain.atrrib.LandAuctionRegistryAttrib;
import com.caliverse.admin.dynamodb.repository.LandAuctionActivityRepository;
import com.caliverse.admin.dynamodb.repository.LandAuctionHighestBidUserRepository;
import com.caliverse.admin.dynamodb.repository.LandAuctionRegistryRepository;
import com.caliverse.admin.global.common.constants.DynamoDBConstants;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import software.amazon.awssdk.enhanced.dynamodb.Key;

@Service
@RequiredArgsConstructor
@Slf4j
public class DynamodbLandAuctionService {
    private final LandAuctionRegistryRepository registryRepository;
    private final LandAuctionActivityRepository activityRepository;
    private final LandAuctionHighestBidUserRepository highestBidUserRepository;
    private final LandAuctionRecordRepository landAuctionRecordRepository;

    @DynamoDBTransaction
    public void insertLandAuctionRegistryWithActivity(LandRequest landRequest) {
        registryRepository.insertAuction(landRequest);
        highestBidUserRepository.insertHighestBidUser(landRequest);
//        activityRepository.upsertActivity(landRequest);   //로직변경으로 activity는 운영툴에서 안건드는걸로한다.
    }

    @DynamoDBTransaction
    public void updateLandAuction(LandRequest landRequest) {
        registryRepository.updateAuction(landRequest);
    }

    @DynamoDBTransaction
    public void cancelLandAuction(LandAuction auctionInfo) {
        registryRepository.cancelAuction(auctionInfo.getLandId(), auctionInfo.getAuctionSeq());
    }

    public LandAuctionRegistryAttrib getLandAuctionRegistry(Integer land_id, Integer auction_seq){
        return registryRepository.findRegistryAttrib(land_id, auction_seq);
    }

    public LandAuctionHighestBidUserAttrib getLandAuctionHighestUser(Integer land_id, Integer auction_seq){
        return highestBidUserRepository.findHighestBidUserAttrib(land_id, auction_seq);
    }

    public int getLandAuctionNumber(Integer landId){
//        return activityRepository.findAuctionNumber(landId);
        return registryRepository.findAuctionNumber(landId);
    }

    public int getLandAuctionRecordNumber(Integer landId){
        Key key = Key.builder()
                .partitionValue(DynamoDBConstants.PK_KEY_LAND_AUCTION_RECORD)
                .sortValue(String.valueOf(landId))
                .build();
        LandAuctionRecordDoc doc = landAuctionRecordRepository.findById(key);

        if(doc == null) return 0;

        return doc.getAttribValue().getAuctionNumber();
    }
}
