package com.caliverse.admin.domain.service;

import com.caliverse.admin.global.common.code.CommonCode;
import com.caliverse.admin.global.common.code.ErrorCode;
import com.caliverse.admin.global.common.exception.RestApiException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest;
import software.amazon.awssdk.services.dynamodb.model.DeleteItemResponse;


/*
* 추후에 일반화 처리 필요
* */
@Service
public class DynamoDBQueryServiceBase {

    @Value("${amazon.dynamodb.metaTable}")
    private String metaTable;

    private final DynamoDbClient dynamoDbClient;
    private final DynamoDBAttributeCreateService dynamoDBAttributeCreateService;

    public DynamoDBQueryServiceBase(DynamoDbClient dynamoDbClient
                                    , DynamoDBAttributeCreateService dynamoDBAttributeCreateService
    ) {
        this.dynamoDBAttributeCreateService = dynamoDBAttributeCreateService;
        this.dynamoDbClient = dynamoDbClient;
    }


    public void deleteUserItem(String userGuid, String itemGuid) {


        var itemAttributes = dynamoDBAttributeCreateService.makeItemAttributeKey(userGuid, itemGuid);

        DeleteItemRequest request = DeleteItemRequest.builder()
                .tableName(metaTable)
                .key(itemAttributes)
                .build();

        DeleteItemResponse response = dynamoDbClient.deleteItem(request);

        if (!response.sdkHttpResponse().isSuccessful()) {
            throw new RestApiException(CommonCode.ERROR.getHttpStatus(), ErrorCode.DYNAMODB_ITEM_DELETE_FAIL.getMessage() );
        }

    }

}
