package com.caliverse.admin.dynamodb.service;

import com.caliverse.admin.domain.entity.BlackList;
import com.caliverse.admin.dynamodb.domain.atrrib.NicknameAttrib;
import com.caliverse.admin.dynamodb.domain.atrrib.UserBaseAttrib;
import com.caliverse.admin.dynamodb.repository.*;
import com.caliverse.admin.global.common.annotation.DynamoDBTransaction;
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 com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


@Slf4j
@Service
@RequiredArgsConstructor
public class DynamodbUserService {
    private final AccountBaseRepository accountBaseRepository;
    private final UserBaseRepository userBaseRepository;
    private final UserNicknameRegistryRepository registryRepository;
    private final NicknameRepository nicknameRepository;

    private final ObjectMapper mapper = new ObjectMapper();

    public boolean isBlockUser(String guid){
        String account_id = getGuidByAccountId(guid);
        return accountBaseRepository.isBlockUser(account_id);
    }

    @DynamoDBTransaction
    public void updateBlockUser(BlackList.STATUSTYPE status, BlackList blackList){
        String guid = blackList.getGuid();
        String account_id = getGuidByAccountId(guid);

        if(status.equals(BlackList.STATUSTYPE.INPROGRESS)){
            accountBaseRepository.updateBlockUserStart(account_id, blackList);
        }
        else{
            accountBaseRepository.updateBlockUserEnd(account_id);
        }
    }

    public String getGuidByName(String guid){
        NicknameAttrib nicknameAttrib = nicknameRepository.findUser(guid);
        if(nicknameAttrib == null){
            log.error("getGuidByName Nickname Null guid: {}", guid);
            return "";
        }

        return nicknameAttrib.getNickname();
    }

    public String getGuidByAccountId(String guid){
        UserBaseAttrib userBaseAttrib = userBaseRepository.findUser(guid);
        if(userBaseAttrib == null){
            log.error("getGuidByAccountId UserBase Null guid: {}", guid);
            return "";
        }

        return userBaseAttrib.getAccountId();
    }

}
