package com.samsung.embo.kpi.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.samsung.authentication.Account;
import com.samsung.embo.formula.entity.Formula;
import com.samsung.embo.kpi.ParameterService;
import com.samsung.user.entity.User;

import io.swagger.v3.oas.annotations.Operation;

@RestController
@RequestMapping("/embo/parameter/")
public class ParemeterController {

	private final ParameterService parameterService;

	public ParemeterController(ParameterService parameterService) {
		this.parameterService = parameterService;
	}

	@Operation(summary = "parameterInfo 조회")
	@PostMapping(value = "main.do", params = "method=search")
	public Map<String, Object> search(@RequestBody Map<String, Object> params) throws Exception {

		User user = Account.currentUser();
		params.put("userId", user.getUserId());
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("result",parameterService.selectParameter(params));
		return result;
	}


	@Operation(summary = "parameterInfo 상세조회")
	@PostMapping(value = "main.do", params = "method=getParameter")
	public Map<String, Object> getParameter(@RequestBody Map<String, Object> params) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();

		result.put("result", parameterService.selectParameter(params));

		return result;
	}


	@Operation(summary = "parameterInfo 등록")
	@PostMapping(value = "main.do", params = "method=regist")
	public String regist(@RequestBody Map<String, Object> params) throws Exception {
		User user = Account.currentUser();
		params.put("userId", user.getUserId());
		return parameterService.insertParameter(params);
	}

	@Operation(summary = "parameterInfo 수정")
	@PostMapping(value = "main.do", params = "method=update")
	public String update(@RequestBody Map<String, Object> params) throws Exception {
		User user = Account.currentUser();
		params.put("userId", user.getUserId());
		parameterService.updateParameter(params);
		return "success";
	}

	@Operation(summary = "parameterInfo 삭제")
	@PostMapping(value = "main.do", params = "method=delete")
	public String delete(@RequestBody Map<String, Object> params) throws Exception {
		User user = Account.currentUser();
		params.put("userId", user.getUserId());
		int ret = parameterService.deleteParameter(params);
		return "success";
	}

}
