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

import com.caliverse.admin.dynamodb.domain.atrrib.UserBaseAttrib;
import com.caliverse.admin.dynamodb.domain.doc.UserBaseDoc;
import com.caliverse.admin.dynamodb.repository.BaseDynamoDBRepository;
import com.caliverse.admin.dynamodb.repository.UserBaseRepository;
import com.caliverse.admin.dynamodb.service.DynamoDBOperations;
import com.caliverse.admin.global.common.constants.DynamoDBConstants;
import com.caliverse.admin.global.common.utils.CommonUtils;
import com.caliverse.admin.mongodb.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;

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

    @Override
    public UserBaseAttrib findUser(String guid) {
        Key key = Key.builder()
                .partitionValue(DynamoDBConstants.PK_KEY_USER_BASE + guid)
                .sortValue(DynamoDBConstants.EMPTY)
                .build();

        UserBaseDoc doc = findById(key);
        if(doc == null) return null;

        return CommonUtils.stringByObject(doc.getAttribValue(), UserBaseAttrib.class);
    }
}
