package incheon.sgp.rst.web;

import java.util.HashMap;
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.RstVoluService;
import lombok.RequiredArgsConstructor;

/**
 * @ClassName : RstVoluController.java
 * @Description : 부동산 시장동향 통계 컨트롤러
 *
 * @author : 이주훈
 * @since : 2025. 10. 27
 * @version : 1.0
 *
 */
@Controller
@RequestMapping("/sgp/rst")
@RequiredArgsConstructor
public class RstVoluController {
	
	private final RstVoluService rstVoluService;

    /**
     * 인천광역시 매매 거래량 조회
     */
    @GetMapping("/volu/getSalesVolume.do")
    @ResponseBody
    public Map<String, Object> getSalesVolume(
            @RequestParam(name = "crtrYm") String crtrYm,
            @RequestParam(name = "sttySggCd", required = false, defaultValue = "000") String sttySggCd
    ) {
        Map<String, Object> result = new HashMap<>();
        try {
            result = rstVoluService.getSalesVolume(crtrYm, sttySggCd);
            result.put("status", "success");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("status", "error");
            result.put("message", e.getMessage());
        }
        return result;
    }
    
    /**
     * 인천광역시 전월세 거래량 조회
     */
    @GetMapping("/volu/getRentalVolume.do")
    @ResponseBody
    public Map<String, Object> getRentalVolume(
            @RequestParam(name = "crtrYm") String crtrYm,
            @RequestParam(name = "sttySggCd", required = false, defaultValue = "000") String sttySggCd
    ) {
        Map<String, Object> result = new HashMap<>();
        try {
            result = rstVoluService.getRentalVolume(crtrYm, sttySggCd);
            result.put("status", "success");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("status", "error");
            result.put("message", e.getMessage());
        }
        return result;
    }
    
    /**
     * 연월 콤보박스 동적 제어
     */
    @GetMapping("/volu/getCrtrYmRange.do")
    @ResponseBody
    public Map<String, Object> getCrtrYmRange() {
        Map<String, Object> result = new HashMap<>();
        try {
            Map<String, Object> range = rstVoluService.getCrtrYmRange();
            String minYear = range.get("minYear").toString();
            String maxYear = range.get("maxYear").toString();
            String maxMonth = range.get("maxMonth").toString();
            System.out.println("minYear : " + minYear
            + " maxYear : " + maxYear
            + " maxMonth : " + maxMonth
            );
            result.put("status", "success");
            result.put("minYear", minYear);
            result.put("maxYear", maxYear);
            result.put("maxMonth", maxMonth);
        } catch (Exception e) {
            result.put("status", "error");
            result.put("message", e.getMessage());
        }
        return result;
    }
    
    /**
     * 지도용 군구별 거래량 조회 (매매/전월세)
     */
    @GetMapping("/volu/getRegionVolumeBoth.do")
    @ResponseBody
    public Map<String, Object> getRegionVolumeBoth(@RequestParam String crtrYm) throws Exception {
        Map<String, Object> data = rstVoluService.selectRegionTradeVolume(crtrYm);

        Map<String, Object> result = new HashMap<>();
        result.put("status", "success");
        result.putAll(data);
        return result;
    }
    
}
    
