package com.caliverse.admin.domain.service;

import com.caliverse.admin.domain.datacomponent.MetaDataHandler;
import com.caliverse.admin.domain.entity.BattleEvent;
import com.caliverse.admin.domain.entity.metadata.*;
import com.caliverse.admin.domain.request.BattleEventRequest;
import com.caliverse.admin.domain.response.BattleEventResponse;
import com.caliverse.admin.dynamodb.service.DynamodbBattleEventService;
import com.caliverse.admin.global.common.code.CommonCode;
import com.caliverse.admin.global.common.utils.CommonUtils;
import com.caliverse.admin.mongodb.service.MysqlHistoryLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

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

@Service
@RequiredArgsConstructor
@Slf4j
public class GameSettingService {

    private final DynamodbBattleEventService dynamodbBattleEventService;

    private final MetaDataHandler metaDataHandler;
    private final MysqlHistoryLogService mysqlHistoryLogService;

    // 게임 셋팅 설정 조회
    public BattleEventResponse getGameSettingList(@RequestParam Map<String, String> requestParam){

        requestParam = CommonUtils.pageSetting(requestParam);

        List<MetaGameModeMatchData> list = metaDataHandler.getMetaGameModeMatchListData();

        return BattleEventResponse.builder()
                .status(CommonCode.SUCCESS.getHttpStatus())
                .result(CommonCode.SUCCESS.getResult())
                .resultData(BattleEventResponse.ResultData.builder()
//                        .battleEventList(list)
                        .pageNo(requestParam.get("page_no")!=null?
                                Integer.parseInt(requestParam.get("page_no")):1)
                        .build()
                )
                .build();
    }

    // 게임 셋팅 설정 상세조회
    public BattleEventResponse getGameSettingDetail(Long id){

        BattleEvent battleEvent = null;

        return BattleEventResponse.builder()
                .status(CommonCode.SUCCESS.getHttpStatus())
                .result(CommonCode.SUCCESS.getResult())
                .resultData(BattleEventResponse.ResultData.builder()
                        .battleEvent(battleEvent)
                        .build())
                .build();
    }

    // 게임 셋팅 설정 저장/수정
    @Transactional(transactionManager = "transactionManager")
    public BattleEventResponse updateGameSetting(Long id, BattleEventRequest battleEventRequest) {
        battleEventRequest.setId(id);
        battleEventRequest.setUpdateBy(CommonUtils.getAdmin().getId());
        battleEventRequest.setUpdateDt(LocalDateTime.now());

        dynamodbBattleEventService.updateBattleEvent(battleEventRequest);

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

}
