package com.eactive.eai.rms.ext.djb.webhook.controller;

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

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.SortDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import com.eactive.eai.rms.common.base.BaseAnnotationController;
import com.eactive.eai.rms.common.combo.ComboService;
import com.eactive.eai.rms.common.vo.GridResponse;
import com.eactive.eai.rms.ext.djb.webhook.dto.WebhookSendLogUI;
import com.eactive.eai.rms.ext.djb.webhook.dto.WebhookSendLogUISearch;
import com.eactive.eai.rms.ext.djb.webhook.service.WebhookSendLogManService;

import lombok.RequiredArgsConstructor;

/**
 * 웹훅 발송 로그(PTL_WEBHOOK_SEND_LOG) 조회 화면 컨트롤러.
 *
 * <p>조회 전용 (등록/수정/삭제 없음)</p>
 */
@Controller
@RequiredArgsConstructor
public class WebhookSendLogManController extends BaseAnnotationController {

	private final WebhookSendLogManService webhookSendLogManService;
	private final ComboService comboService;

	@GetMapping(value = "/onl/apim/webhook/webhookSendLogMan.view")
	public void view() {
		// 목록 view
	}

	@GetMapping(value = "/onl/apim/webhook/webhookSendLogMan.view", params = "cmd=DETAIL")
	public String detailView() {
		return "/onl/apim/webhook/webhookSendLogManDetail";
	}

	@PostMapping(value = "/onl/apim/webhook/webhookSendLogMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<WebhookSendLogUI>> selectList(
			@SortDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable,
			WebhookSendLogUISearch search) {
		Page<WebhookSendLogUI> page = webhookSendLogManService.selectList(pageable, search);
		return ResponseEntity.ok(new GridResponse<>(page));
	}

	@PostMapping(value = "/onl/apim/webhook/webhookSendLogMan.json", params = "cmd=LIST_INIT_COMBO")
	public ResponseEntity<Map<String, Object>> initCombo() {
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("eventTypeList", comboService.getMonitoringCodeSortedBySeq(WebhookSendLogManService.CODE_GROUP_EVENT_TYPE));
		return ResponseEntity.ok(resultMap);
	}

	@PostMapping(value = "/onl/apim/webhook/webhookSendLogMan.json", params = "cmd=DETAIL")
	public ResponseEntity<WebhookSendLogUI> selectDetail(Long id) {
		return ResponseEntity.ok(webhookSendLogManService.selectDetail(id));
	}
}
