package incheon.ags.ias.authrt.web;

import incheon.ags.ias.authrt.service.AuthrtService;
import incheon.ags.ias.authrt.vo.AuthrtSearchVO;
import incheon.ags.ias.authrt.vo.AuthrtVO;
import incheon.ags.ias.sys.service.SysService;
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.*;

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

@Controller
@RequiredArgsConstructor
@Slf4j
@RequireRole(system = "AGS", roles = "ROLE_SUPER_ADMIN", description = "통합관리자 역할 접근 제어")
public class AuthrtController {

    private final AuthrtService authrtService;
    private final SysService sysService;

    @GetMapping("/ags/ias/authrt/authrtList.do")
    public String authrtList(
            @RequestParam(defaultValue = "1") int page,
            @ModelAttribute AuthrtSearchVO vo,
            ModelMap model) throws Exception {

        vo.setPageIndex(page);

        PaginationInfo paginationInfo = new PaginationInfo();
        paginationInfo.setCurrentPageNo(page);
        paginationInfo.setRecordCountPerPage(vo.getRecordCountPerPage());
        paginationInfo.setPageSize(vo.getPageSize());

        vo.setFirstIndex(paginationInfo.getFirstRecordIndex());
        vo.setLastIndex(paginationInfo.getLastRecordIndex());
        vo.setRecordCountPerPage(paginationInfo.getRecordCountPerPage());

        int totalCount = authrtService.selectAuthrtListCnt(vo);
        model.addAttribute("totalCount", totalCount);
        paginationInfo.setTotalRecordCount(totalCount);

        List<Map<String, Object>> authrtList = authrtService.selectAuthrtList(vo);
        model.addAttribute("authrtList", authrtList);
        model.addAttribute("paginationInfo", paginationInfo);
        
        // 검색 조건을 모델에 추가
        model.addAttribute("searchVO", vo);

        // 시스템 목록 조회
        List<Map<String, Object>> sysList = sysService.selectAllSysList();
        model.addAttribute("sysList", sysList);
        
        // 권한 타입 목록 조회
        List<Map<String, Object>> authrtTypeList = authrtService.selectAuthrtTypeList();
        model.addAttribute("authrtTypeList", authrtTypeList);

        return "/ags/ias/authrt/authrtList";
    }

    @GetMapping("/ags/ias/authrt/authrtDetail.do")
    public String authrtDetail(
            @RequestParam(value = "authrtCd", required = false) String authrtCd,
            @RequestParam(value = "sysCd", required = false) String sysCd,
            ModelMap model) throws Exception {

        AuthrtVO vo = new AuthrtVO();
        vo.setAuthrtCd(authrtCd);
        vo.setSysCd(sysCd);
        AuthrtVO authrtDetail = authrtService.selectAuthrtDetail(vo);
        model.addAttribute("authrtDetail", authrtDetail);

        return "ags/ias/authrt/authrtDetail";
    }

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

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

        return "ags/ias/authrt/authrtRegist";
    }

    @GetMapping("/ags/ias/authrt/authrtModify.do")
    public String authrtModify(
            @RequestParam(value = "authrtCd", required = false) String authrtCd,
            @RequestParam(value = "sysCd", required = false) String sysCd,
            ModelMap model) throws Exception {

        AuthrtVO vo = new AuthrtVO();
        vo.setAuthrtCd(authrtCd);
        vo.setSysCd(sysCd);
        AuthrtVO authrtDetail = authrtService.selectAuthrtDetail(vo);
        model.addAttribute("authrtDetail", authrtDetail);

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

        return "ags/ias/authrt/authrtModify";
    }
}
