package com.eactive.eai.rms.onl.apim.messagerequest;

import com.eactive.eai.rms.common.base.BaseAnnotationController;
import com.eactive.eai.rms.common.vo.GridResponse;
import com.eactive.eai.rms.data.entity.onl.apim.messagerequest.MessageRequestUISearch;
import lombok.RequiredArgsConstructor;
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 java.util.HashMap;
import java.util.Map;

/**
 * 발송 내역(PTL_MESSAGE_REQUEST) 조회 화면 컨트롤러.
 *
 * <p>조회 전용 + PENDING→FAILED 상태 전환만 제공한다. (등록/수정/삭제 없음)</p>
 */
@Controller
@RequiredArgsConstructor
public class MessageRequestManController extends BaseAnnotationController {

	private final MessageRequestManService messageRequestManService;

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

	@GetMapping(value = "/onl/apim/messagerequest/messageRequestMan.view", params = "cmd=DETAIL")
	public String detailView() {
		return "/onl/apim/messagerequest/messageRequestManDetail";
	}

	@PostMapping(value = "/onl/apim/messagerequest/messageRequestMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<MessageRequestUI>> selectList(
			@SortDefault(sort = "requestDate", direction = Sort.Direction.DESC) Pageable pageable,
			MessageRequestUISearch search) {
		Page<MessageRequestUI> page = messageRequestManService.selectList(pageable, search);
		return ResponseEntity.ok(new GridResponse<>(page));
	}

	@PostMapping(value = "/onl/apim/messagerequest/messageRequestMan.json", params = "cmd=LIST_INIT_COMBO")
	public ResponseEntity<Map<String, Object>> initCombo() {
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("statusList", messageRequestManService.statusCombo());
		resultMap.put("messageCodeList", messageRequestManService.messageCodeCombo());
		return ResponseEntity.ok(resultMap);
	}

	@PostMapping(value = "/onl/apim/messagerequest/messageRequestMan.json", params = "cmd=DETAIL")
	public ResponseEntity<MessageRequestUI> selectDetail(String id) {
		return ResponseEntity.ok(messageRequestManService.selectDetail(id));
	}

	/** PENDING 건을 FAILED 로 전환 (감사 포인트: UPDATE_STATUS). */
	@PostMapping(value = "/onl/apim/messagerequest/messageRequestMan.json", params = "cmd=UPDATE_STATUS")
	public ResponseEntity<Void> updateStatus(String id) {
		messageRequestManService.updateStatusToFailed(id);
		return ResponseEntity.ok().build();
	}
}
