package incheon.ags.uis.dept.web;

import incheon.ags.uis.dept.service.DeptMappingService;
import incheon.ags.uis.dept.vo.DeptMappingVO;
import incheon.com.security.vo.LoginVO;
import org.egovframe.rte.fdl.security.userdetails.util.EgovUserDetailsHelper;
import org.egovframe.rte.psl.dataaccess.util.EgovMap;
import org.egovframe.rte.ptl.mvc.tags.ui.pagination.PaginationInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/ags/uis/dept")
public class DeptMappingController {

    private final DeptMappingService deptMappingService;

    public DeptMappingController(DeptMappingService deptMappingService) {
        this.deptMappingService = deptMappingService;
    }

    /**
     * 부서 매핑 목록
     *  - GET /ags/uis/dept/deptList.do
     */
    @GetMapping("/deptList.do")
    public String deptList(@ModelAttribute("searchVO") DeptMappingVO searchVO,
                           @RequestParam(value = "mngt", required = false) String mngt,
                           @RequestParam(value = "searchKeyword", required = false) String searchKeyword,
                           @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                           Model model) {

        // 검색조건 매핑
        searchVO.setMngt(mngt);
        searchVO.setKeyword(searchKeyword);

        // 페이지 번호 설정
        searchVO.setPageIndex(page);

        // eGov 페이징 세팅
        PaginationInfo paginationInfo = new PaginationInfo();
        paginationInfo.setCurrentPageNo(searchVO.getPageIndex());
        paginationInfo.setRecordCountPerPage(searchVO.getRecordCountPerPage()); // 기본 10
        paginationInfo.setPageSize(searchVO.getPageSize());                     // 기본 10

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

        // 총 건수
        int totalCount = deptMappingService.selectDeptMappingListCount(searchVO);
        paginationInfo.setTotalRecordCount(totalCount);

        // 목록 조회
        List<DeptMappingVO> deptMappingList = deptMappingService.selectDeptMappingList(searchVO);

        // 관리기관 목록 조회
        List<EgovMap> mngtList = deptMappingService.selectMngtList();

        // 모델
        model.addAttribute("deptMappingList", deptMappingList);
        model.addAttribute("paginationInfo", paginationInfo);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("mngtList", mngtList);

        // 검색값 다시 내려주기
        model.addAttribute("mngt", mngt);
        model.addAttribute("searchKeyword", searchKeyword);

        return "ags/uis/dept/deptMappingList";
    }

    /**
     * 부서 매핑 상세
     *  - GET /ags/uis/dept/deptDetail.do
     */
    @GetMapping("/deptDetail.do")
    public String deptDetail(@RequestParam("gid") Long gid,
                             @RequestParam("mngt") String mngt,
                             @RequestParam("deptCode") String deptCode,
                             Model model) {
        DeptMappingVO searchVO = new DeptMappingVO();
        searchVO.setGid(gid);
        searchVO.setMngt(mngt);
        searchVO.setDeptCode(deptCode);

        DeptMappingVO detail = deptMappingService.selectDeptMappingDetail(searchVO);
        model.addAttribute("detail", detail);
        return "ags/uis/dept/deptMappingDetail";
    }

    /**
     * 부서 매핑 등록 페이지
     *  - GET /ags/uis/dept/deptRegist.do
     */
    @GetMapping("/deptRegist.do")
    public String deptRegist(Model model) {
        // 관리기관 목록 조회
        List<EgovMap> mngtList = deptMappingService.selectMngtList();
        model.addAttribute("mngtList", mngtList);
        return "ags/uis/dept/deptMappingRegist";
    }

    /**
     * 부서 매핑 등록 처리
     *  - POST /ags/uis/dept/deptRegist.do
     */
    @org.springframework.web.bind.annotation.PostMapping("/deptRegist.do")
    public String deptRegistProcess(@ModelAttribute DeptMappingVO deptMappingVO) {
        // 현재 로그인한 사용자 정보 가져오기
        LoginVO loginVO = (LoginVO) EgovUserDetailsHelper.getAuthenticatedUser();
        String userId = loginVO.getUserId();

        // 사용자 정보 설정
        deptMappingVO.setFrstRegisterId(userId);
        deptMappingVO.setLastUpdusrId(userId);

        deptMappingService.insertDeptMapping(deptMappingVO);
        return "redirect:/ags/uis/dept/deptList.do";
    }

    /**
     * 부서 매핑 수정 페이지
     *  - GET /ags/uis/dept/deptModify.do
     */
    @GetMapping("/deptModify.do")
    public String deptModify(@RequestParam("gid") Long gid,
                             @RequestParam("mngt") String mngt,
                             @RequestParam("deptCode") String deptCode,
                             Model model) {
        DeptMappingVO searchVO = new DeptMappingVO();
        searchVO.setGid(gid);
        searchVO.setMngt(mngt);
        searchVO.setDeptCode(deptCode);

        DeptMappingVO detail = deptMappingService.selectDeptMappingDetail(searchVO);
        model.addAttribute("detail", detail);
        return "ags/uis/dept/deptMappingModify";
    }

    /**
     * 부서 매핑 수정 처리
     *  - POST /ags/uis/dept/deptModify.do
     */
    @org.springframework.web.bind.annotation.PostMapping("/deptModify.do")
    public String deptModifyProcess(@ModelAttribute DeptMappingVO deptMappingVO) {
        // 현재 로그인한 사용자 정보 가져오기
        LoginVO loginVO = (LoginVO) EgovUserDetailsHelper.getAuthenticatedUser();
        String userId = loginVO.getUserId();

        // 수정자 정보 설정 (MyBatis 인터셉터가 자동으로 주입하지만 명시적으로 설정)
        deptMappingVO.setLastUpdusrId(userId);

        deptMappingService.updateDeptMapping(deptMappingVO);
        return "redirect:/ags/uis/dept/deptList.do";
    }
}
