package com.dbnt.kcscbackend.admin.contents.survey;

import com.dbnt.kcscbackend.admin.contents.survey.entity.TnSurvey;
import com.dbnt.kcscbackend.admin.contents.survey.entity.TnSurveyQt;
import com.dbnt.kcscbackend.admin.contents.survey.service.AdminSurveyService;
import com.dbnt.kcscbackend.auth.entity.LoginVO;
import com.dbnt.kcscbackend.config.common.ResponseCode;
import com.dbnt.kcscbackend.config.common.ResultVO;

import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/survey")
public class AdminSurveyController {

  private final AdminSurveyService adminSurveyService;

  @RequestMapping(method = RequestMethod.GET, value = "/list")
  public ResultVO getSurveyList() throws Exception{

    ResultVO resultVO = new ResultVO();
    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("surveyList", adminSurveyService.selectSurveyList());
    resultVO.setResult(resultMap);
    return resultVO;
  }

  @RequestMapping(method = RequestMethod.PUT, value = "/info")
  public ResultVO surveyEdit(@RequestBody @Valid TnSurvey survey, Errors errors, @AuthenticationPrincipal LoginVO user) throws Exception{
    ResultVO resultVO = new ResultVO();
    if(errors.hasErrors()){
      StringBuilder msg = new StringBuilder();
      for(FieldError error: errors.getFieldErrors()){
        msg.append(error.getDefaultMessage());
        msg.append("\n");
      }
      resultVO.setResultCode(ResponseCode.INPUT_CHECK_ERROR.getCode());
      resultVO.setResultMessage(msg.toString());
    }else {
      adminSurveyService.insertSurvey(survey, user.getId());
      resultVO.setResultCode(ResponseCode.SUCCESS.getCode());
      resultVO.setResultMessage("저장 되었습니다.");
    }
    return resultVO;
  }

  @RequestMapping(method = RequestMethod.DELETE, value = "/info")
  public ResultVO surveyDelete(@RequestBody TnSurvey survey, @AuthenticationPrincipal LoginVO user) throws Exception{
    ResultVO resultVO = new ResultVO();
    adminSurveyService.deleteSurvey(survey.getSvySeq());
    resultVO.setResultCode(ResponseCode.SUCCESS.getCode());
    resultVO.setResultMessage("저장 되었습니다.");
    return resultVO;
  }

  @RequestMapping(method = RequestMethod.PUT, value = "/info-use-yn")
  public ResultVO surveyUseYn(@RequestBody TnSurvey survey, @AuthenticationPrincipal LoginVO user) throws Exception{
    ResultVO resultVO = new ResultVO();
    adminSurveyService.updateSurveyUseYn(survey.getSvySeq(), user.getId());
    resultVO.setResultCode(ResponseCode.SUCCESS.getCode());
    resultVO.setResultMessage("저장 되었습니다.");
    return resultVO;
  }

  @RequestMapping(method = RequestMethod.GET, value = "/info-qt")
  public ResultVO surveyQt(TnSurvey survey) throws Exception{

    ResultVO resultVO = new ResultVO();
    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("qtList", adminSurveyService.selectSurveyQt(survey));
    resultVO.setResult(resultMap);
    return resultVO;
  }

  @RequestMapping(method = RequestMethod.PUT, value = "/info-qt")
  public ResultVO surveyQtEdit(@RequestBody List<TnSurveyQt> qtList, @AuthenticationPrincipal LoginVO user) throws Exception{
    ResultVO resultVO = new ResultVO();

    adminSurveyService.insertSurveyQt(qtList, user.getId());
    resultVO.setResultCode(ResponseCode.SUCCESS.getCode());
    resultVO.setResultMessage("저장 되었습니다.");

    return resultVO;
  }
}
