package com.caliverse.admin.domain.service;

import java.util.List;
import java.util.Map;

import com.caliverse.admin.domain.adminlog.AdminItemDeleteLog;
import com.caliverse.admin.domain.adminlog.IAdminLog;
import com.caliverse.admin.domain.dao.admin.HistoryMapper;
import com.caliverse.admin.domain.entity.ItemList;
import com.caliverse.admin.domain.entity.SEARCHTYPE;
import com.caliverse.admin.domain.response.ItemDeleteResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.caliverse.admin.domain.request.ItemsRequest;
import com.caliverse.admin.domain.response.ItemsResponse;
import com.caliverse.admin.global.common.code.CommonCode;
import com.caliverse.admin.global.common.code.SuccessCode;

import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class ItemsService {
    private static final Logger logger = LoggerFactory.getLogger(ItemsService.class);

    private final DynamoDBService dynamoDBService;
    private final UserGameSessionService userGameSessionService;
    private final DynamoDBQueryServiceBase dynamoDBQueryServiceBase;
    private final HistoryMapper historyMapper;



    // 아이템 정보 조회 GUID,item ID, item name
    public ItemsResponse findItems(Map<String,String> requestParam){
        String searchType = requestParam.get("search_type").toString();
        String search_key = requestParam.get("search_key").toString().trim();
        String guid = search_key;

        if(searchType.equals(SEARCHTYPE.NAME.name())){
            guid = dynamoDBService.getNickNameByGuid(search_key);
        }

        List<ItemList> itemList = dynamoDBService.getItems(guid);

        return ItemsResponse.builder()
                .resultData(ItemsResponse.ResultData.builder()
                        .list(itemList)
                        .build())
                .status(CommonCode.SUCCESS.getHttpStatus())
                .result(CommonCode.SUCCESS.getResult())
                .build();
    }


    @Transactional(transactionManager = "transactionManager")
    public ItemDeleteResponse postItemDelete(ItemsRequest itemDeleteRequest){
        var userGuid = itemDeleteRequest.getUserGuid();
        var itemGuid = itemDeleteRequest.getItemGuid();
        var itemCount = itemDeleteRequest.getItemCount();

        //UserKick
        userGameSessionService.kickUserSession(userGuid);
        //ItemDelete
        dynamoDBQueryServiceBase.deleteUserItem(userGuid, itemGuid);

        //로그 기록
        IAdminLog adminLog = new AdminItemDeleteLog(userGuid, itemGuid, itemCount);
        adminLog.saveLogToDB();


        return ItemDeleteResponse.builder()
                .status(CommonCode.SUCCESS.getHttpStatus())
                .result(CommonCode.SUCCESS.getResult())
                .resultData(ItemDeleteResponse.ResultData.builder()
                        .message(SuccessCode.DYNAMODB_ITEM_DELETE_SUCCESS.getMessage())
                        .deletedItemGuid(itemGuid)
                        .build())
                .build();

    }

}
