package incheon.ags.ias.tmprUserAuthrt.web;

import incheon.ags.ias.sys.service.SysService;
import incheon.ags.ias.tmprUserAuthrt.service.impl.TmprUserAuthrtServiceImpl;
import incheon.ags.ias.tmprUserAuthrt.vo.TmprUserAuthrtSearchVO;
import incheon.com.security.annotation.RequireRole;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.egovframe.rte.ptl.mvc.tags.ui.pagination.PaginationInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestParam;

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

@Controller
@RequiredArgsConstructor
@Slf4j
@RequireRole(system = "AGS", roles = "ROLE_SUPER_ADMIN", description = "통합관리자 역할 접근 제어")
public class TmprUserAuthrtController {
    private final TmprUserAuthrtServiceImpl tmprUserAuthrtService;
    private final SysService sysService;

    @GetMapping("/ags/ias/tmprUserAuthrt/tmprUserAuthrtList.do")
    public String tmprUserAuthrtList(
            @RequestParam(defaultValue = "1") int page,
            @ModelAttribute TmprUserAuthrtSearchVO searchVO,
            ModelMap model) throws Exception {

        // 1. 총 개수 조회
        int totalCount = tmprUserAuthrtService.selectTmprUserAuthrtListTotCnt(searchVO);

        // 2. 페이징 설정
        searchVO.setPageIndex(page);
        PaginationInfo paginationInfo = new PaginationInfo();
        paginationInfo.setCurrentPageNo(page);
        paginationInfo.setRecordCountPerPage(searchVO.getRecordCountPerPage());
        paginationInfo.setPageSize(searchVO.getPageSize());
        paginationInfo.setTotalRecordCount(totalCount);

        // 3. 페이징 파라미터 설정
        searchVO.setFirstIndex(paginationInfo.getFirstRecordIndex());
        searchVO.setLastIndex(paginationInfo.getLastRecordIndex());
        searchVO.setRecordCountPerPage(paginationInfo.getRecordCountPerPage());

        // 4. 임시 사용자 권한 리스트 조회
        List<Map<String, Object>> tmprUserAuthrtList = tmprUserAuthrtService.selectTmprUserAuthrtList(searchVO);
        model.addAttribute("tmprUserAuthrtList", tmprUserAuthrtList);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("paginationInfo", paginationInfo);
        model.addAttribute("searchVO", searchVO);

        return "ags/ias/tmprUserAuthrt/tmprUserAuthrtList";
    }

    @GetMapping("/ags/ias/tmprUserAuthrt/tmprUserAuthrtDetail.do")
    public String tmprUserAuthrtDetail(
            @RequestParam String userId,
            @RequestParam String roleCd,
            @RequestParam String sysCd,
            ModelMap model) throws Exception {

        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("roleCd", roleCd);
        params.put("sysCd", sysCd);

        // 임시 사용자 권한 정보 조회
        Map<String, Object> tmprUserAuthrtInfo = tmprUserAuthrtService.selectTmprUserAuthrtDetail(params);
        model.addAttribute("tmprUserAuthrtInfo", tmprUserAuthrtInfo);

        // 해당 사용자의 메뉴-권한 매핑 조회
        List<Map<String, Object>> menuAuthrtList = tmprUserAuthrtService.selectTmprUserMenuAuthrtList(params);
        model.addAttribute("menuAuthrtList", menuAuthrtList);

        // 해당 사용자의 시스템-권한 매핑 조회
        List<Map<String, Object>> sysAuthrtList = tmprUserAuthrtService.selectTmprUserSysAuthrtList(params);
        model.addAttribute("sysAuthrtList", sysAuthrtList);

        List<Map<String, Object>> menuList = tmprUserAuthrtService.selectMenuListBySysCd(tmprUserAuthrtInfo.get("sysCd").toString());
        model.addAttribute("menuList", menuList);

        List<Map<String, Object>> globalSysAuthrtList = tmprUserAuthrtService.selectSysAuthrtStngList(tmprUserAuthrtInfo.get("sysCd").toString());
        model.addAttribute("globalSysAuthrtList", globalSysAuthrtList);

        return "ags/ias/tmprUserAuthrt/tmprUserAuthrtDetail";
    }

    @GetMapping("/ags/ias/tmprUserAuthrt/tmprUserAuthrtModify.do")
    public String tmprUserAuthrtModify(
            @RequestParam String userId,
            @RequestParam String roleCd,
            @RequestParam String sysCd,
            ModelMap model) throws Exception {

        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("roleCd", roleCd);
        params.put("sysCd", sysCd);

        // 임시 사용자 권한 정보 조회
        Map<String, Object> tmprUserAuthrtInfo = tmprUserAuthrtService.selectTmprUserAuthrtDetail(params);
        model.addAttribute("tmprUserAuthrtInfo", tmprUserAuthrtInfo);

        // 기존 메뉴-권한 매핑 조회
        List<Map<String, Object>> menuAuthrtList = tmprUserAuthrtService.selectTmprUserMenuAuthrtList(params);
        model.addAttribute("menuAuthrtList", menuAuthrtList);

        // 기존 시스템-권한 매핑 조회
        List<Map<String, Object>> sysAuthrtList = tmprUserAuthrtService.selectTmprUserSysAuthrtList(params);
        model.addAttribute("sysAuthrtList", sysAuthrtList);

        List<Map<String, Object>> menuList = tmprUserAuthrtService.selectMenuListBySysCd(tmprUserAuthrtInfo.get("sysCd").toString());
        model.addAttribute("menuList", menuList);

        List<Map<String, Object>> globalSysAuthrtList = tmprUserAuthrtService.selectSysAuthrtStngList(tmprUserAuthrtInfo.get("sysCd").toString());
        model.addAttribute("globalSysAuthrtList", globalSysAuthrtList);

        List<Map<String, Object>> globalMenuAuthrtList = tmprUserAuthrtService.selectMenuAuthrtStngList(tmprUserAuthrtInfo.get("sysCd").toString());
        model.addAttribute("globalMenuAuthrtList", globalMenuAuthrtList);

        return "ags/ias/tmprUserAuthrt/tmprUserAuthrtModify";
    }

    @GetMapping("/ags/ias/tmprUserAuthrt/tmprUserAuthrtRegist.do")
    public String tmprUserAuthrtRegist(ModelMap model) throws Exception {

        // 시스템 목록 조회
        List<Map<String, Object>> sysList = sysService.selectAllSysList();
        model.addAttribute("sysList", sysList);

        return "ags/ias/tmprUserAuthrt/tmprUserAuthrtRegist";
    }
}