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

import com.caliverse.admin.dynamodb.domain.atrrib.AccountBaseAttrib;
import com.caliverse.admin.dynamodb.domain.atrrib.UserNicknameRegistryAttrib;
import com.caliverse.admin.dynamodb.domain.doc.UserNicknameRegistryDoc;
import com.caliverse.admin.dynamodb.repository.BaseDynamoDBRepository;
import com.caliverse.admin.dynamodb.repository.UserNicknameRegistryRepository;
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.history.service.DynamodbHistoryLogService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
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.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

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

    @Override
    public UserNicknameRegistryAttrib findUser(String nickname) {
        Key key = Key.builder()
                .partitionValue(DynamoDBConstants.PK_KEY_USER_NICKNAME_REGISTRY)
                .build();

        UserNicknameRegistryDoc doc = findById(key);
        try {
            return objectMapper.readValue(doc.getAttribValue(), UserNicknameRegistryAttrib.class);
        } catch (JsonProcessingException e) {
            throw new RestApiException(CommonCode.ERROR.getHttpStatus(), ErrorCode.DYNAMODB_CONNECTION_ERROR.getMessage());
        }
    }

    @Override
    public List<String> findAllNicknameByGuid() {
        Key key = Key.builder()
                .partitionValue(DynamoDBConstants.PK_KEY_USER_NICKNAME_REGISTRY)
                .build();

        List<UserNicknameRegistryDoc> doc = findAll(key);

        return doc.stream()
                .map(UserNicknameRegistryDoc::getAttribValue)
                .map(attribJson -> {
                    try {
                        JsonNode jsonNode = objectMapper.readTree(attribJson);
                        UserNicknameRegistryAttrib attrib = objectMapper.convertValue(jsonNode, UserNicknameRegistryAttrib.class);
                        return attrib.getUserGuid();
                    } catch (JsonProcessingException e) {
                        log.error("JSON parsing failed: {}", e.getMessage());
                        return null;
                    }
                })
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }

    @Override
    public List<UserNicknameRegistryAttrib> findAllAttrib() {
        Key key = Key.builder()
                .partitionValue(DynamoDBConstants.PK_KEY_USER_NICKNAME_REGISTRY)
                .build();

        List<UserNicknameRegistryDoc> doc = findAll(key);

        return doc.stream()
                .map(UserNicknameRegistryDoc::getAttribValue)
                .map(attribJson -> {
                    try {
                        JsonNode jsonNode = objectMapper.readTree(attribJson);
                        return objectMapper.convertValue(jsonNode, UserNicknameRegistryAttrib.class);
                    } catch (JsonProcessingException e) {
                        log.error("JSON parsing failed: {}", e.getMessage());
                        return null;
                    }
                })
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }
}
