package incheon.ags.cms.monitoring.web;

import incheon.ags.cms.monitoring.service.MonitoringService;
import incheon.com.cmm.ResponseCode;
import incheon.com.cmm.api.DefaultApiResponse;
import incheon.com.security.vo.LoginVO;
import lombok.RequiredArgsConstructor;
import org.egovframe.rte.psl.dataaccess.util.EgovMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/api/v1/cms/monitoring")
@RequiredArgsConstructor
public class MonitoringApiController {

    private final MonitoringService monitoringService;

    private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringApiController.class);

    /**
     * 청소차량 최신 신호 조회
     *
     * @param searchKeyword 차량 정보
     * @return 신호 목록
     * @throws Exception
     */
    @GetMapping("/getRealtimeVehicleList")
    @ResponseBody
    public ResponseEntity<DefaultApiResponse<Map<String, Object>>> getRealtimeVehicleList(@RequestParam String searchKeyword) throws Exception {

        Map<String, Object> response = new HashMap<>();

        // 최신신호 목록
        List<EgovMap> signalList = monitoringService.selectRealtimeVehicleList(searchKeyword);

        response.put("success", true);
        response.put("message", "차량 신호 조회가 완료되었습니다.");
        response.put("count", signalList.size());
        response.put("results", signalList);
        LOGGER.info(response.get("message").toString());
        return ResponseEntity.ok(DefaultApiResponse.success(response));
    }

    /**
     * 청소차량 관제규칙 위반 차량 목록 조회
     *
     * @return 관제규칙 위반 차량 목록
     * @throws Exception
     */
    @GetMapping("/getViolationList")
    @ResponseBody
    public ResponseEntity<DefaultApiResponse<Map<String, Object>>> getViolationList() throws Exception {

        Map<String, Object> response = new HashMap<>();

        // 최신신호 목록
        List<EgovMap> violationList = monitoringService.selectViolationList();

        response.put("success", true);
        response.put("message", "관제규칙 위반 차량 목록 조회가 완료되었습니다.");
        response.put("count", violationList.size());
        response.put("results", violationList);
        LOGGER.info(response.get("message").toString());
        return ResponseEntity.ok(DefaultApiResponse.success(response));
    }

    /**
     * 청소차량 관제규칙위반 통보여부 수정
     * @param vltnHstryIds
     * @return
     * @throws Exception
     */
    @PutMapping("/updateViolationNotice")
    @ResponseBody
    public ResponseEntity<DefaultApiResponse<Map<String, Object>>> updateViolationNotice(
            @RequestBody List<Integer> vltnHstryIds, Authentication authentication) throws Exception {

        Map<String, Object> response = new HashMap<>();

        try {
            LoginVO loginVO = (LoginVO) authentication.getPrincipal();
            String loginUserId = loginVO.getUserId();

            Map<String, Object> paramMap = new EgovMap();
            paramMap.put("vltnHstryIdList", vltnHstryIds);
            paramMap.put("lastMdfcnId", loginUserId);

            // 관제규칙 위반차량 통보 완료로 변경
            int updateCount = monitoringService.updateViolationNotice(paramMap);

            response.put("success", true);
            response.put("message", "관제규칙 위반차량 통보가 완료되었습니다.");
            response.put("count", updateCount);
            LOGGER.info(response.get("message").toString());
        } catch (IllegalArgumentException e) {
            LOGGER.error(e.getMessage());
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(DefaultApiResponse.error(ResponseCode.INPUT_ERROR.getCode(), e.getMessage(), e.toString()));
        }

        return ResponseEntity.ok(DefaultApiResponse.success(response));
    }

}
