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

import com.caliverse.admin.domain.entity.EFilterOperator;
import com.caliverse.admin.dynamodb.domain.atrrib.QuestAttrib;
import com.caliverse.admin.dynamodb.domain.doc.QuestDoc;
import com.caliverse.admin.dynamodb.dto.PageResult;
import com.caliverse.admin.dynamodb.repository.BaseDynamoDBRepository;
import com.caliverse.admin.dynamodb.repository.QuestRepository;
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.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;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;

import java.time.LocalDateTime;
import java.util.Map;


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

    @Override
    public PageResult<QuestDoc> getQuestListWithPaging(String userGuid, String sortKeyPrefix, String filterAttributeName, EFilterOperator filterOperator, String filterAttributeValue, Map<String, AttributeValue> exclusiveStartKey, boolean sortIndex) {
        String pk = DynamoDBConstants.PK_KEY_QUEST + userGuid;;

        return findByPaging(
                pk,
                sortKeyPrefix,
                filterAttributeName,
                filterOperator,
                filterAttributeValue,
                exclusiveStartKey,
                sortIndex
        );
    }

    @Override
    public void updateQuest(String guid, Integer questId) {
        try {
            Key key = Key.builder()
                    .partitionValue(DynamoDBConstants.PK_KEY_QUEST + guid)
                    .sortValue(questId)
                    .build();

            QuestDoc beforeDoc = findById(key);

            if (beforeDoc != null) {
                QuestDoc afterDoc = deepCopy(beforeDoc, QuestDoc.class);

                QuestAttrib attrib = afterDoc.getAttribValue();

                afterDoc.setAttribValue(attrib);
                afterDoc.setUpdatedDateTime(CommonUtils.convertUTCDate(LocalDateTime.now()));

                update(afterDoc);

                log.info("updateQuest Update Success: {}", CommonUtils.objectByString(afterDoc));

            }
        }catch (Exception e){
            log.error("Update Quest Error: {}", e.getMessage());
            throw new RestApiException(CommonCode.ERROR.getHttpStatus(), ErrorCode.DYNAMODB_ITEM_UPDATE_ERROR.getMessage());
        }
    }

    @Override
    public void deleteItem(String userGuid, Integer questId) {
        try {
            Key key = Key.builder()
                    .partitionValue(DynamoDBConstants.PK_KEY_QUEST + userGuid)
                    .sortValue(questId)
                    .build();

            QuestDoc beforeDoc = findById(key);

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

                log.info("QuestDoc Delete Success: {}", CommonUtils.objectByString(beforeDoc));

            }
        }catch (Exception e){
            log.error("Update QuestRegistry Error: {}", e.getMessage());
            throw new RestApiException(CommonCode.ERROR.getHttpStatus(), ErrorCode.DYNAMODB_ITEM_DELETE_ERROR.getMessage());
        }
    }
}
