package com.jasamedika.medifirst2000.controller;

import com.jasamedika.medifirst2000.constants.MessageResource;
import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.LogbookRemunService;
import com.jasamedika.medifirst2000.vo.LogbookKinerjaVO;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import static com.jasamedika.medifirst2000.constants.Constants.MessageInfo.ERROR_MESSAGE;
import static com.jasamedika.medifirst2000.core.web.WebConstants.HttpHeaderInfo.LABEL_SUCCESS;
import static com.jasamedika.medifirst2000.util.rest.RestUtil.getJsonHttptatus;
import static com.jasamedika.medifirst2000.util.rest.RestUtil.getJsonResponse;
import static org.slf4j.LoggerFactory.getLogger;
import static org.springframework.http.HttpStatus.ACCEPTED;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * @author Salman
 * @version 1.0.0
 * @since 02/01/2025
 */
@RestController
@RequestMapping("/remunerasi/logbook")
public class LogbookRemunController extends LocaleController<LogbookKinerjaVO> {

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

	@Autowired
	private LogbookRemunService logbookRemunService;

	@RequestMapping(value = "/rekap", method = POST, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<String> patchingRecapLogbook(HttpServletRequest request) {
		try {
			logbookRemunService.generateRecap();
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse("The recap has been successfully generated", ACCEPTED, mapHeaderMessage);
		} catch (ServiceVOException | JpaSystemException e) {
			LOGGER.error("Got Exception {} when generateRecap", e.getMessage());
			mapHeaderMessage.put(ERROR_MESSAGE, e.getMessage());
			return getJsonHttptatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		}
	}

}
