package incheon.ags.ias.srvy.srvyResult.web;

import incheon.ags.ias.srvy.srvyResult.service.SrvyResultService;
import incheon.ags.ias.srvy.srvyResult.vo.SrvyResultDetailVO;
import incheon.ags.ias.srvy.srvyResult.vo.SrvyResultSearchVO;
import incheon.ags.ias.srvy.srvyResult.vo.SrvyResultVO;
import incheon.com.security.annotation.RequireRole;
import org.egovframe.rte.ptl.mvc.tags.ui.pagination.PaginationInfo;
import org.springframework.beans.factory.annotation.Autowired;
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.List;

/**
 * 설문 결과 Controller
 */
@Controller
@RequireRole(system = "AGS", roles = "ROLE_SUPER_ADMIN", description = "통합관리자 역할 접근 제어")
public class SrvyResultController {

    @Autowired
    private SrvyResultService srvyResultService;

    /**
     * 설문 결과 목록 조회
     */
    @GetMapping("/ags/ias/srvyRslt/srvyResultList.do")
    public String srvyResultList(
            @RequestParam(defaultValue = "1") int page,
            @ModelAttribute("searchVO") SrvyResultSearchVO searchVO, ModelMap model) throws Exception {

        searchVO.setPageIndex(page);

        // 페이징 정보 설정
        PaginationInfo paginationInfo = new PaginationInfo();
        paginationInfo.setCurrentPageNo(page);
        paginationInfo.setRecordCountPerPage(searchVO.getPageUnit());
        paginationInfo.setPageSize(searchVO.getPageSize());

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

        // 목록 조회와 카운트를 병렬로 처리 (성능 최적화)
        List<SrvyResultVO> resultList = srvyResultService.selectSrvyResultList(searchVO);
        int totalRecordCount = srvyResultService.selectSrvyResultListCnt(searchVO);
        
        paginationInfo.setTotalRecordCount(totalRecordCount);
        model.addAttribute("srvyResultList", resultList);
        model.addAttribute("paginationInfo", paginationInfo);

        return "/ags/ias/srvy/srvyResult/srvyResultList";
    }

    /**
     * 설문 결과 상세 조회
     */
    @GetMapping("/ags/ias/srvyRslt/srvyResultDetail.do")
    public String srvyResultDetail(@RequestParam(value = "srvySn", required = false) Integer srvySn, ModelMap model) throws Exception {
        
        if (srvySn == null) {
            model.addAttribute("errorMessage", "설문 번호가 필요합니다.");
            return "/ags/ias/srvy/srvyResult/srvyResultDetail";
        }
        
        // 설문 결과 상세 조회
        SrvyResultDetailVO detailVO = srvyResultService.selectSrvyResultDetail(srvySn);
        
        model.addAttribute("srvyResultDetail", detailVO);

        return "/ags/ias/srvy/srvyResult/srvyResultDetail";
    }

    /**
     * 답변 전체보기 팝업
     */
    @GetMapping("/ags/ias/srvyRslt/answerPopup.do")
    public String answerPopup(@RequestParam("answers") String answers,
                             @RequestParam("title") String title,
                             @RequestParam("questionNumber") Integer questionNumber,
                             ModelMap model) throws Exception {
        
        model.addAttribute("answers", answers);
        model.addAttribute("title", title);
        model.addAttribute("questionNumber", questionNumber);
        
        return "/ags/ias/srvy/srvyResult/answerPopup";
    }
}
