package com.dbnt.sims.userInfo;

import com.dbnt.sims.userInfo.model.DashboardConfig;
import com.dbnt.sims.userInfo.model.UserInfo;
import com.dbnt.sims.userInfo.service.UserInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/myInfo")
public class MyInfoController {

  private final UserInfoService userInfoService;

  @GetMapping("/myInfoPage")
  public ModelAndView myInfoPage(@AuthenticationPrincipal UserInfo loginUser){
    ModelAndView mav = new ModelAndView("user/myInfo");
    mav.addObject("userInfo", loginUser);
    mav.addObject("dashboardConfigList", userInfoService.getDashboardConfigList(loginUser.getUserSeq()));
    return mav;
  }

  @GetMapping("/getDashBoardConfig")
  public List<DashboardConfig> getDashBoardConfig(@AuthenticationPrincipal UserInfo loginUser){
    return userInfoService.getDashboardConfigList(loginUser.getUserSeq());
  }

  @PostMapping("/selectedMenuTable")
  @ResponseBody
  public ModelAndView dashboardConfigTable(@RequestBody List<DashboardConfig> selectMenuList){
    ModelAndView mav = new ModelAndView("user/dashboardConfigTable");
    mav.addObject("dashboardConfigList", selectMenuList);
    return mav;
  }

  @PostMapping("saveDashboardConfig")
  @ResponseBody
  public void saveDashboardConfig(@AuthenticationPrincipal UserInfo loginUser, @RequestBody List<DashboardConfig> configList){
    userInfoService.saveDashboardConfigList(loginUser.getUserSeq(), configList);
  }

}
