package com.icomsys.main_vm.biz.common.system.controller;

import com.google.gson.Gson;
import com.icomsys.main_vm.biz.advice.excep.CustomNotFoundException;
import com.icomsys.main_vm.biz.common.login.res.OprmngCodeRes;
import com.icomsys.main_vm.biz.common.login.res.UserVo;
import com.icomsys.main_vm.biz.common.login.service.LoginService;
import com.icomsys.main_vm.biz.common.system.service.SystemGroupAuthService;
import com.icomsys.main_vm.biz.common.system.vo.*;
import com.icomsys.main_vm.common.code.enumresource.RoleResource;
import com.querydsl.core.QueryResults;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oracle.jdbc.proxy.annotation.Post;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Controller
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/admin/common/system")
public class SystemGroupAuthController {

    private final SystemGroupAuthService groupAuthService;

    /**
     * @Name 권한그룹의 페이지랜딩 컨트롤러
     * @Description 권한그룹의 페이지를 랜딩함
     * @Author JangWonSeok
     * @CreateDate 2023. 03. 09.
     */
    @GetMapping("/groupAuth/manage.do")
    public String GroupAuthView() {
        log.info("group Auth View controller");
        return "/adm/common/system/groupAuth";
    }

    /**
     * @Name 리스트페이지의 API
     * @Description 권한그룹 리스트의 API
     * @Author JangWonSeok
     * @CreateDate 2023. 03. 09.
     */
    @PostMapping("/groupAuth/list")
    @ResponseBody
    public DslDataTableRes<SystemServiceAuthGroupRes> GroupAuthList(@RequestBody SystemServiceAuthGroupReq dto) {
        log.info("list dto = {}", new Gson().toJson(dto));
        QueryResults<SystemServiceAuthGroupRes> result = groupAuthService.GroupAuthList(dto);
        return new DslDataTableRes<SystemServiceAuthGroupRes>(result);
    }

    /**
     * @Name 권한그룹의 상세내역 조회페이지 랜더링
     * @Description 권한그룹 상세보기 JSP를 랜더링함.
     * @Author JangWonSeok
     * @CreateDate 2023. 03. 09.
     */
    @GetMapping("/groupAuth/view")
    public String GroupAuthView(@RequestParam(name = "custCode", required = true) String custCode) {
        log.info("view Controller - {}", custCode);
        return "/adm/common/system/groupAuthView";
    }

    /**
     * @Name 권한그룹 권한데이터
     * @Description 권한그룹의 데이터를 트리구조로 뿌려주는 데이터를 조회함
     * @Author JangWonSeok
     * @CreateDate 2023. 03. 09.
     */
    @GetMapping("/groupAuth/newTree")
    @ResponseBody
    public SystemServiceGroupTreeRes GroupAuthNewTree(@RequestParam(name = "serviceGroup", required = true)String serviceGroup) {
        return groupAuthService.GroupAuthNewTree(serviceGroup);
    }

    /**
     * @Name 권한그룹을 생성 &저장하는 API
     * @Description
     * @Author JangWonSeok
     * @CreateDate 2023. 03. 09.
     */
    @PostMapping("/groupAuth/insert")
    @ResponseBody
    public void GroupAuthInsert(@RequestBody SystemServiceGroupInsertReq dto) {
        groupAuthService.GroupAuthUpdateInsert(dto);
    }

    /**
     * @Name 권한그룹을 업데이트페이지 랜더링
     * @Description 권한그룹 업데이트 JSP페이지 랜더링
     * @Author JangWonSeok
     * @CreateDate 2023. 03. 09.
     */
    @GetMapping("/groupAuth/update")
    public String GroupAuthUpdate() {
        log.info("GroupAuthUpdate Controller");
        return "/adm/common/system/groupAuthUpdate";
    }

    /**
     * @Name 권한그룹을 업데이트페이지의 데이터조회
     * @Description 권한그룹 업데이트 페이지의 데이터를 조회하는 API
     * @Author JangWonSeok
     * @CreateDate 2023. 03. 09.
     */
    @GetMapping("/groupAuth/update/select")
    @ResponseBody
    public SystemServiceAuthGroupUpdateRes GroupAuthUpdateData(@RequestParam(name = "authGroupSeq") Long authGroupSeq) throws CustomNotFoundException {
        log.info("GroupAuthUpdateData Controller" + authGroupSeq);
        return groupAuthService.GroupAuthUpdateData(authGroupSeq);
    }

    /**
     * @Name 권한그룹에 소속시킬 유저를 검색하는 API
     * @Description 권한그룹에 소속시킬 유저를 검색하는 API
     * @Author JangWonSeok
     * @CreateDate 2023. 03. 09.
     */
    @PostMapping("/groupAuth/userSearch")
    @ResponseBody
    public List<SystemServiceAuthGroupUserSearchRes> GroupAuthUpdateDataUserSearch(@RequestParam(name = "authGroupSeq") Long authGroupSeq, @RequestBody SystemServiceAuthGroupUserSearchReq dto) throws CustomNotFoundException {
        return groupAuthService.GroupAuthUpdateDataUserSearch(authGroupSeq, dto);
    }

    /**
     * @Name 권한그룹에 을 업데이트 하는 API
     * @Description 권한그룹을 업데이트하는 API
     * @Author JangWonSeok
     * @CreateDate 2023. 03. 09.
     */
    @PostMapping("/groupAuth/modify/update")
    @ResponseBody
    public void GroupAuthModityUpdate(@RequestBody SystemServiceAuthGroupModifyReq dto) throws CustomNotFoundException {
        log.info("GroupAuthModityUpdate Controller" + dto);
        groupAuthService.GroupAuthModityUpdate(dto);
    }

    @GetMapping("/groupAuth/insert/opr")
    @ResponseBody
    public List<OprmngCodeRes> GroupInsertOpr() {
        return groupAuthService.GroupInsertOpr();
    }

}
