package com.jasamedika.medifirst2000.controller;

import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.dto.superapp.request.EmailDto;
import com.jasamedika.medifirst2000.dto.superapp.response.AuthorizationDto;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.MapPegawaiJabatanToUnitKerjaService;
import com.jasamedika.medifirst2000.service.PelayananPasienService;
import com.jasamedika.medifirst2000.vo.LogbookKinerjaVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

import static org.springframework.http.HttpStatus.CREATED;
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.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * @author salmanoe
 * @version 1.0.0
 * @since 04/06/2024
 */
@RestController
@RequestMapping("/remun")
public class RemunerasiController extends LocaleController<LogbookKinerjaVO> {

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

	@Autowired
	private PelayananPasienService pelayananPasienService;

	@Autowired
	private MapPegawaiJabatanToUnitKerjaService mapPegawaiJabatanToUnitKerjaService;

	@RequestMapping(value = "/logbook-tarif/{idPegawai}/{bulan}", method = GET)
	public ResponseEntity<Object> recap(HttpServletRequest request, @PathVariable("idPegawai") Integer idPegawai,
			@PathVariable("bulan") String bulan) {
		LOGGER.info("Superapp requesting logbook tarif data id pegawai {} bulan {}", idPegawai, bulan);

		List<Map<String, Object>> result = pelayananPasienService.rekapLogbook(idPegawai, bulan);
		return new ResponseEntity<>(result, HttpStatus.OK);
	}

	@RequestMapping(value = "/authorization", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
	public ResponseEntity<Object> authorize(HttpServletRequest request, @RequestBody EmailDto dto) {
		try {
			AuthorizationDto result = mapPegawaiJabatanToUnitKerjaService.get(dto);
			return new ResponseEntity<>(result, CREATED);
		} catch (ServiceVOException | JpaSystemException e) {
			LOGGER.error("Got exception {} when get authorization for superapp", e.getMessage());

			return new ResponseEntity<>(null, INTERNAL_SERVER_ERROR);
		}
	}

}
