package incheon.ags.dss.facility.web;

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

import org.springframework.http.ResponseEntity;
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.RestController;

import incheon.com.cmm.ComDefaultVO;
import incheon.com.cmm.api.DefaultApiResponse;
import incheon.ags.dss.facility.service.RealEstateService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 외부 연동: 부동산 정보(13-19p) JSON 데이터를 반환하는 API 컨트롤러 (R-Only)
 */
@RestController 
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/api/v1/dss/facility/realEstate")
public class RealEstateApiController {

    private final RealEstateService realEstateService;

    /**
     * 13-14p: 토지(임야)대장 상세 (기본+이동+등급)
     */
    @GetMapping("/land.do")
    public ResponseEntity<DefaultApiResponse> getLandLedger(
            @ModelAttribute ComDefaultVO searchVO) throws Exception { 
        
        log.info("API: getLandLedger (PNU: {})", searchVO.getSearchKeyword());
        
        Map<String, Object> detail = realEstateService.selectLandLedgerDetail(searchVO);
        List<Map<String, Object>> historyList = realEstateService.selectLandLedgerHistoryList(searchVO);
        List<Map<String, Object>> gradeList = realEstateService.selectLandGradeHistoryList(searchVO);

        log.info("API: detailData ({})", detail);
        
        Map<String, Object> result = new HashMap<>();
        result.put("detail", detail);
        result.put("historyList", historyList);
        result.put("gradeList", gradeList);
        
        return ResponseEntity.ok(DefaultApiResponse.success(result, "토지대장이 조회되었습니다."));
    }

    /**
     * 15p: 토지이용계획 (목록+도면)
     */
    @GetMapping("/plan.do")
    public ResponseEntity<DefaultApiResponse> getLandUsePlan(
            @ModelAttribute ComDefaultVO searchVO) throws Exception { 
        
        log.info("API: getLandUsePlan (PNU: {})", searchVO.getSearchKeyword());
        
        List<Map<String, Object>> planList = realEstateService.selectLandUsePlanList(searchVO);
        Map<String, Object> drawing = realEstateService.selectLandUsePlanDrawing(searchVO);

        Map<String, Object> result = new HashMap<>();
        result.put("planList", planList);
        result.put("drawing", drawing);
        
        return ResponseEntity.ok(DefaultApiResponse.success(result, "토지이용계획이 조회되었습니다."));
    }

    /**
     * 16-19p: 건축물대장 (기본+층별+전유부+기타)
     */
    @GetMapping("/building.do")
    public ResponseEntity<DefaultApiResponse> getBuildingLedger(
            @ModelAttribute ComDefaultVO searchVO) throws Exception { 
        
        log.info("API: getBuildingLedger (PNU: {})", searchVO.getSearchKeyword());
        
        Map<String, Object> basicInfo = realEstateService.selectBuildingBasicInfo(searchVO);
        List<Map<String, Object>> floorList = realEstateService.selectBuildingFloorList(searchVO);
        List<Map<String, Object>> occupantList = realEstateService.selectBuildingOccupantList(searchVO);
        Map<String, Object> etcInfo = realEstateService.selectBuildingEtcInfo(searchVO);

        Map<String, Object> result = new HashMap<>();
        result.put("basicInfo", basicInfo);
        result.put("floorList", floorList);
        result.put("occupantList", occupantList);
        result.put("etcInfo", etcInfo);
        
        return ResponseEntity.ok(DefaultApiResponse.success(result, "건축물대장이 조회되었습니다."));
    }
}