package com.jasamedika.medifirst2000.controller;

import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.vo.MapProdukPaketToProdukVO;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.MapProdukPaketToProdukService;
import com.jasamedika.medifirst2000.util.rest.RestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.springframework.http.HttpStatus.*;
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 Salman
 * @version 1.0.0
 * @since 7 Aug 2023
 */
@RestController
@RequestMapping("/paket-tindakan")
public class MapProdukPaketController extends LocaleController<MapProdukPaketToProdukVO> {
	@Autowired
	private MapProdukPaketToProdukService mapProdukPaketToProdukService;

	@RequestMapping(method = POST, consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Object> save(@RequestBody @Valid MapProdukPaketToProdukVO vo) {
		try {
			mapProdukPaketToProdukService.save(vo);
			Map<String, String> message = new HashMap<>();
			message.put(CREATED.getReasonPhrase(), "Berhasil tersimpan");
			return RestUtil.getJsonResponse(vo, CREATED, message);
		} catch (ServiceVOException e) {
			Map<String, String> error = new HashMap<>();
			error.put("bad_request", e.getMessage());
			return RestUtil.getJsonResponse(null, HttpStatus.BAD_REQUEST, error);
		}
	}

	@RequestMapping(method = GET)
	public ResponseEntity<Object> get() {
		List<MapProdukPaketToProdukVO> data = mapProdukPaketToProdukService.get();
		Map<String, String> message = new HashMap<>();
		message.put(OK.getReasonPhrase(), "Berhasil");
		return RestUtil.getJsonResponse(data, OK, message);
	}

	@RequestMapping(value = "/{paketId}", method = GET)
	public ResponseEntity<Object> get(@PathVariable Integer paketId) {
		List<MapProdukPaketToProdukVO> data = mapProdukPaketToProdukService.get(paketId);
		Map<String, String> message = new HashMap<>();
		message.put(OK.getReasonPhrase(), "Berhasil");
		return RestUtil.getJsonResponse(data, OK, message);
	}

	@RequestMapping(value = "/{noRec}", method = GET)
	public ResponseEntity<Object> get(@PathVariable String noRec) {
		MapProdukPaketToProdukVO data = mapProdukPaketToProdukService.get(noRec);
		Map<String, String> message = new HashMap<>();
		message.put(OK.getReasonPhrase(), "Berhasil");
		return RestUtil.getJsonResponse(data, OK, message);
	}

	@RequestMapping(value = "/{noRec}", method = POST, consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Object> delete(@PathVariable String noRec) {
		boolean status = mapProdukPaketToProdukService.delete(noRec);
		Map<String, String> message = new HashMap<>();
		message.put(OK.getReasonPhrase(), "Berhasil");
		return RestUtil.getJsonResponse(status, OK, message);
	}
}
