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.ModuleService;
import com.samsung.user.entity.User;

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

@RestController
@RequestMapping("/embo/module/")
public class ModuleController {

	private final ModuleService moduleService;

	public ModuleController(ModuleService moduleService) {
		this.moduleService = moduleService;
	}

	@Operation(summary = "moduleInfo 조회")
	@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",moduleService.selectModule(params));
		return result;
	}


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

		result.put("result", moduleService.selectModule(params));

		return result;
	}


	@Operation(summary = "moduleInfo 등록")
	@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 moduleService.insertModule(params);
	}

	@Operation(summary = "moduleInfo 수정")
	@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());
		moduleService.updateModule(params);
		return "success";
	}

	@Operation(summary = "moduleInfo 삭제")
	@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 = moduleService.deleteModule(params);
		return "success";
	}

}
