package incheon.sgp.rst.web;

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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import incheon.sgp.rst.service.RstLcadstService;
import incheon.sgp.rst.vo.RstAdmResultVO;
import lombok.RequiredArgsConstructor;

/**
 * @ClassName : RstLcadstController.java
 * @Description : 지목별/소유별 지적통계 컨트롤러
 *
 * @author : 이주훈
 * @since : 2025. 11. 06
 * @version : 1.0
 *
 */
@Controller
@RequestMapping("/sgp/rst")
@RequiredArgsConstructor
public class RstLcadstController {
	
	private final RstLcadstService rstLcadstService;
	
    // 지목별 통계 조회
    @GetMapping("/jimok/search.do")
    @ResponseBody
    public Map<String, Object> search(
        @RequestParam(required = false) String crtrYr,
        @RequestParam(required = false) String sggCd,
        @RequestParam(required = false) String emdCd,
        @RequestParam(required = false) String liCd
    ) throws Exception {
        if (crtrYr == null || crtrYr.isEmpty()) crtrYr = "2025";

        Map<String, Object> result = new HashMap<>();
        result.put("summary", rstLcadstService.selectLandSummary(sggCd, emdCd, liCd, crtrYr));
        result.put("groupList", rstLcadstService.selectLandGroupStats(sggCd, emdCd, liCd, crtrYr));
        return result;
    }
    
    // 소유별 통계 조회 
    @GetMapping("/ownst/search.do")
    @ResponseBody
    public Map<String, Object> searchOwnst(
        @RequestParam(required = false) String crtrYr,
        @RequestParam(required = false) String sggCd,
        @RequestParam(required = false) String emdCd,
        @RequestParam(required = false) String liCd
    ) throws Exception {
        if (crtrYr == null || crtrYr.isEmpty()) crtrYr = "2025";

        Map<String, Object> result = new HashMap<>();
        result.put("summary", rstLcadstService.selectOwnSummary(sggCd, emdCd, liCd, crtrYr));
        result.put("groupList", rstLcadstService.selectOwnGroupStats(sggCd, emdCd, liCd, crtrYr));
        return result;
    }
    
    // 행정구역 조회 (공통)
    @GetMapping("/admList.do")
    @ResponseBody
    public List<RstAdmResultVO> admList(
        @RequestParam String type,
        @RequestParam(required = false) String parentCd
    ) throws Exception {

        switch (type) {
            case "ctp":
                return rstLcadstService.selectCtpList();
            case "sig":
                return rstLcadstService.selectSigList(parentCd);
            case "emd":
                return rstLcadstService.selectEmdList(parentCd);
            case "li":
            	return rstLcadstService.selectLiList(parentCd);
            default:
                return List.of();
        }
    }
    
    // 행정구역 위치 이동을 위한 좌표값(x,y) 조회 
    @GetMapping("/admCoord.do")
    @ResponseBody
    public Map<String, Object> getAdmCoord(@RequestParam String admCd) throws Exception {
        return rstLcadstService.getAdmCenter(admCd); 
    } 
    
    // 기준연도 목록 조회
    @GetMapping("/lcadst/crtrYrList.do")
    @ResponseBody
    public List<String> getCrtrYrList() throws Exception {
        return rstLcadstService.selectCrtrYrList();
    }
    
}
    
