package com.bsmlab.dfx.dfxconsole.app.agent;

import com.bsmlab.dfx.agent.config.AgentConfigDto;
import com.bsmlab.dfx.agent.listener.dto.ReceiveMessageDto;
import com.bsmlab.dfx.dfxconsole.app.agent.service.*;
import com.bsmlab.dfx.dfxconsole.framework.dto.SearchParameterDto;
import com.bsmlab.dfx.dfxconsole.framework.support.ResponseUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
@RequiredArgsConstructor
@Slf4j
public class DfxAgentInfoController {
    private final DfxAgentInfoService dfxAgentInfoService;
    private final DfxAgentMessageHistoryService dfxAgentMessageHistoryService;

    @PostMapping("/app-api/agent/getAgentInfoDtoList")
    public ResponseEntity<List<DfxAgentInfoDto>> agentConfigDtoList() {
        List<DfxAgentInfoDto> dfxAgentInfoDtoList = dfxAgentInfoService.getDfxAgentInfoDtoList();
        return ResponseEntity.ok().body(dfxAgentInfoDtoList);
    }

    @PostMapping("/app-api/agent/getAgentInfoDto")
    public ResponseEntity<DfxAgentInfoDto> getAgentInfoDto(@RequestBody Map<String, String> paramMap) {
        String agentId = paramMap.get("agentId");
        DfxAgentInfoDto dfxAgentInfoDto = dfxAgentInfoService.getDfxAgentInfoDto(agentId);
        return ResponseEntity.ok().body(dfxAgentInfoDto);
    }

    @PostMapping("/app-api/agent/getAgentConfigDto")
    public ResponseEntity<?> getAgentConfigDto(@RequestBody Map<String, String> paramMap) {
        String agentId = paramMap.get("agentId");
        AgentConfigDto agentConfigDto = null;
        try {
            agentConfigDto = dfxAgentInfoService.getDfxAgentConfigDto(agentId);
            return ResponseEntity.ok().body(agentConfigDto);
        } catch (JsonProcessingException e) {
            return ResponseEntity.internalServerError().body(ResponseUtils.toExceptionResponseDto(e));
        }
    }

    @PostMapping("/app-api/agent/getAgentMessageDtoListTotalCount")
    public ResponseEntity<Integer> getAgentMessageDtoListTotalCount() {
        int totalCount = dfxAgentMessageHistoryService.selectDfxAgentMessageDtoListTotalCount();
        return ResponseEntity.ok().body(totalCount);
    }

    @PostMapping("/app-api/agent/getAgentMessageDtoList")
    public ResponseEntity<List<DfxAgentMessageDto>> getAgentMessageDtoList(@RequestBody SearchParameterDto searchParameterDto) {
        List<DfxAgentMessageDto> dfxAgentMessageDtoList = dfxAgentMessageHistoryService.selectDfxAgentMessageDtoList(searchParameterDto);
        return ResponseEntity.ok().body(dfxAgentMessageDtoList);
    }

    @PostMapping("/app-api/agent/getAgentMessageData")
    public ResponseEntity<?> getAgentMessageData(DfxAgentMessageDto dfxAgentMessageDto) {
        try {
            List<Map<String, Object>> dataMapList = this.dfxAgentMessageHistoryService.selectDfxAgentMessageData(dfxAgentMessageDto);
            return ResponseEntity.ok().body(dataMapList);
        } catch (JsonProcessingException e) {
            return ResponseEntity.internalServerError().body(ResponseUtils.toExceptionResponseDto(e));
        }
    }

    @PostMapping("/app-api/agent/getAgentMessageHistoryDtoList")
    public ResponseEntity<List<DfxAgentMessageHistoryDto>> getAgentMessageHistoryDtoList() {
        List<DfxAgentMessageHistoryDto> dfxAgentMessageHistoryDtoList = dfxAgentMessageHistoryService.selectDfxAgentMessageHistoryDtoList();
        return ResponseEntity.ok().body(dfxAgentMessageHistoryDtoList);
    }
}
