package com.madeu.controller.api.doctalk;

import com.madeu.init.ManagerDraftAction;
import com.madeu.service.api.doctalk.DocTalkService;
import com.madeu.service.web.webloghistory.WebLogHistoryService;
import com.madeu.util.HttpUtil;
import com.madeu.util.RequestLogUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;


@Slf4j
@Controller
public class DocTalkController extends ManagerDraftAction{

	@Autowired
	private DocTalkService docTalkService;

	@Autowired
	private WebLogHistoryService webLogHistoryService;

	/**
	 * <b>예약 생성</b>
	 * 네이버 예약 발생시, 닥톡에서 API 호출
	 * 닥톡에서 네이버 예약 요청 파라미터를 전달해줌
	 *
	 *
	 * 페이지 처리 없음 
	 * request: 
	 * response : msgCode, msgDesc		
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/add-booking",method=RequestMethod.POST,produces="application/json;charset=utf8")
	@ResponseBody
	public HashMap<String, Object> insertReserveNaver(HttpServletRequest request, HttpServletResponse response) {
			
		log.debug("DocTalkController insertReserveNaver START");

		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();

		StringBuffer errorMsg = new StringBuffer();

		try{
			map = docTalkService.insertReserveNaver(paramMap);
		}catch(Exception e){
			e.printStackTrace();
			errorMsg.append(e);
		}finally {

			try {
				HashMap<String, Object> visitLogParamMap = RequestLogUtil.getVisitLogParameterMap(request);
				HashMap<String, Object> insertMap = new HashMap<String, Object>();

				insertMap.put("url", "/add-booking");
				insertMap.put("func", "insertReserveNaver");
				insertMap.put("funcName", "예약 생성");
				insertMap.put("service", "docTalkService");
				insertMap.put("serviceName", "DocTalk");
				insertMap.put("requestValue", String.valueOf(paramMap));
				insertMap.put("responseValue", String.valueOf(map));
				insertMap.put("tId", map.get("tId"));
				if((String.valueOf(errorMsg)).equals("") || (String.valueOf(errorMsg) == null) || String.valueOf(errorMsg).length() == 0){
					insertMap.put("resultCode", "SUCCESS");
				}else{
					insertMap.put("resultCode", "ERROR");
				}
				insertMap.put("resultMsg", String.valueOf(errorMsg));

				webLogHistoryService.insertLogHistory(insertMap, visitLogParamMap);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	
		log.debug("DocTalkController insertReserveNaver END");
		
		return map;
	}
}
