package incheon.ags.dss.under.web;

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

import org.egovframe.rte.ptl.mvc.tags.ui.pagination.PaginationInfo;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;

import incheon.ags.dss.under.service.UrbUdgdFcltyService;
import incheon.ags.dss.under.vo.UrbUdgdFcltyDtlVO;
import incheon.ags.dss.under.vo.UrbUdgdFcltyMstVO;
import incheon.com.cmm.api.DefaultApiResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Tag(name = "지하시설물 분석", description = "지하시설물 위험도/노후도 분석 실행 및 결과 조회 API")
@RestController 
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/api/v1/dss/urb/under/udgdFclty")
public class UrbUdgdFcltyApiController {

    private final UrbUdgdFcltyService service;

    /** 분석 실행 (POST) */
    @Operation(summary = "지하시설물 분석 실행", description = "입력된 파라미터(MstVO)를 기반으로 지하시설물 분석을 수행하고, 분석 관리 번호를 반환합니다.")
    @PostMapping("/run.do")
    public ResponseEntity<DefaultApiResponse> runAnalysis(@RequestBody UrbUdgdFcltyMstVO vo) throws Exception {
        int anlsMngNo = service.runUndergroundAnalysis(vo);
        
        Map<String, Object> result = new HashMap<>();
        result.put("anlsMngNo", anlsMngNo);
        
        return ResponseEntity.ok(DefaultApiResponse.success(result, "지하시설물 분석이 완료되었습니다."));
    }

    /** 분석 결과 상세 조회 (GET) */
    @Operation(summary = "지하시설물 분석 결과 조회", description = "분석 관리 번호 등 조건에 맞는 지하시설물 분석 상세 결과를 조회합니다.")
    @GetMapping("/result.do")
    public ResponseEntity<DefaultApiResponse> getResult(
    		@ModelAttribute UrbUdgdFcltyDtlVO searchVO, // 검색 조건 (anlsMngNo 등)
            @RequestParam(defaultValue = "1") int pageIndex,
            @RequestParam(defaultValue = "20") int recordCountPerPage
    		) throws Exception {
    	
    	// 1. PaginationInfo 생성
        PaginationInfo paginationInfo = new PaginationInfo();
        paginationInfo.setCurrentPageNo(pageIndex);
        paginationInfo.setRecordCountPerPage(recordCountPerPage);
        paginationInfo.setPageSize(10); // 페이지 리스트 사이즈 (예: 1~10페이지)

        // 2. VO에 페이징 정보 세팅
        searchVO.setPageIndex(pageIndex);
        searchVO.setRecordCountPerPage(recordCountPerPage);
        searchVO.setFirstIndex(paginationInfo.getFirstRecordIndex());
    	
        List<UrbUdgdFcltyDtlVO> list = service.selectUndergroundResultList(searchVO);
        int totalCount = service.selectUndergroundResultCount(searchVO); // Count 메서드 추가 필요

        paginationInfo.setTotalRecordCount(totalCount);
        
        // 4. 결과 반환
        Map<String, Object> result = new HashMap<>();
        result.put("list", list);
        result.put("paging", paginationInfo); // 프론트에서 페이징 렌더링에 사용
        
        return ResponseEntity.ok(DefaultApiResponse.success(result, "조회되었습니다."));
    }
}