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

import com.eactive.eai.rms.common.base.BaseAnnotationController;
import com.eactive.eai.rms.common.combo.ComboService;
import com.eactive.eai.rms.common.combo.ComboVo;
import com.eactive.eai.rms.common.login.SessionManager;
import com.eactive.eai.rms.common.vo.GridResponse;
import com.eactive.eai.rms.data.entity.onl.apim.portalinquiry.PortalInquirySearch;
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 javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequiredArgsConstructor
public class PortalInquiryManController extends BaseAnnotationController {

	private final PortalInquiryManService portalInquiryManService;
	private final ComboService comboService;

	@GetMapping(value = "/onl/apim/portalinquiry/portalInquiryMan.view")
	public void view() {
		// view
	}

	@GetMapping(value = "/onl/apim/portalinquiry/portalInquiryMan.view", params = "cmd=DETAIL")
	public String detailView() {
		return "/onl/apim/portalinquiry/portalInquiryManDetail";
	}


	@PostMapping(value = "/onl/apim/portalinquiry/portalInquiryMan.json", params = "cmd=LIST_INIT_COMBO")
	public ResponseEntity<Map<String, Object>> initCombo() {
		List<ComboVo> portalOrgList = comboService.getFromPortalOrgName();
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("orgRows", portalOrgList);
		return ResponseEntity.ok(resultMap);
	}

	@PostMapping(value = "/onl/apim/portalinquiry/portalInquiryMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<PortalInquiryUI>> selectList(@SortDefault(value = "createdDate", direction = Sort.Direction.DESC) Pageable pageable,
																	PortalInquirySearch portalInquirySearch) {
		Page<PortalInquiryUI> page = portalInquiryManService.selectList(pageable, portalInquirySearch);
		return ResponseEntity.ok(new GridResponse<>(page));
	}

	@PostMapping(value = "/onl/apim/portalinquiry/portalInquiryMan.json", params = "cmd=DETAIL")
	public ResponseEntity<PortalInquiryUI> selectDetail(String id) {
		PortalInquiryUI PortalNoticeUI = portalInquiryManService.selectDetail(id);
		return ResponseEntity.ok(PortalNoticeUI);
	}


	@PostMapping(value = "/onl/apim/portalinquiry/portalInquiryMan.json", params = "cmd=INSERT")
	public ResponseEntity<String> insert(HttpServletRequest request, PortalInquiryUI portalInquiryUI) {
		String userid = SessionManager.getUserId(request);
		portalInquiryManService.insertAndUpdate(portalInquiryUI, userid);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/onl/apim/portalinquiry/portalInquiryMan.json", params = "cmd=DELETE_BULK")
	public ResponseEntity<String> deleteBulk(String ids) {
		portalInquiryManService.deleteBulk(ids);
		return ResponseEntity.ok().build();
	}
	
	@PostMapping(value = "/onl/apim/portalinquiry/portalInquiryMan.json", params = "cmd=LIST_COMMENT")
	public ResponseEntity<List<PortalInquiryCommentUI>> selectCommentList(String id) {
		return ResponseEntity.ok(portalInquiryManService.selectCommentList(id));
	}

	@PostMapping(value = "/onl/apim/portalinquiry/portalInquiryMan.json", params = "cmd=INSERT_COMMENT")
	public ResponseEntity<String> insertComment(HttpServletRequest request, PortalInquiryCommentUI portalInquiryCommentUI) {
		String userid = SessionManager.getUserId(request);
		portalInquiryManService.insertComment(portalInquiryCommentUI, userid);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/onl/apim/portalinquiry/portalInquiryMan.json", params = "cmd=DELETE_COMMENT")
	public ResponseEntity<Void> deleteComment(String id) {
		portalInquiryManService.deleteComment(id);
		return ResponseEntity.ok().build();
	}

	// 공개범위 변경 — 감사 로그는 AuditLogInterceptor 가 자동 기록(auditpoints.dat 등록, auditReason 파라미터)
	@PostMapping(value = "/onl/apim/portalinquiry/portalInquiryMan.json", params = "cmd=VISIBILITY")
	public ResponseEntity<String> updateVisibility(String targetType, String id, String visibility, String auditReason) {
		portalInquiryManService.updateVisibility(targetType, id, visibility, auditReason);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/onl/apim/portalinquiry/portalInquiryMan.json", params = "cmd=INCREMENT_VIEW")
	public ResponseEntity<Void> incrementView(String id) {
		portalInquiryManService.incrementViewCount(id);
		return ResponseEntity.ok().build();
	}

}
