package incheon.ags.dss.facility.service.impl;

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

import org.springframework.stereotype.Service;

import incheon.ags.dss.facility.mapper.FacHistoryMapper;
import incheon.ags.dss.facility.service.FacHistoryService;
import incheon.ags.dss.facility.vo.SafetyInspectionVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Service
@RequiredArgsConstructor
@Slf4j
public class FacHistoryServiceImpl implements FacHistoryService {

    // private final RestTemplate restTemplate;
    private final FacHistoryMapper facHistoryMapper;

    @Override
    public List<String> selectSafetyInspectionYearList(SafetyInspectionVO searchVO) throws Exception {
        String pnu = searchVO.getSearchKeyword();
        log.info("Requesting Safety Year List (Dummy Data)... PNU: {}", pnu);
        // TODO: 외부 API 호출 (예: /api/safety/years?pnu=...)
        return List.of("2024", "2022"); // (더미 연도)
    }

    @Override
    public SafetyInspectionVO selectSafetyInspectionDetail(SafetyInspectionVO searchVO) throws Exception {
        String pnu = searchVO.getSearchKeyword();
        String year = searchVO.getYear();
        log.info("Requesting Safety Detail (Dummy Data)... PNU: {}, Year: {}", pnu, year);

       
        // TODO: 외부 API 호출 (예: /api/safety/detail?pnu=...&year=...)
        
        if ("2024".equals(year)) {
            SafetyInspectionVO vo = new SafetyInspectionVO();
            vo.setBldgName("민천광역시청 본관 (2024년)");
            vo.setAddress("인천광역시 남동구 정각로 29");
            vo.setPurpose("업무시설(관청사)");
            vo.setApprovalYear("2018년");
            vo.setInspectionOrder("제2차 정기안전진단");
            vo.setInspectionPeriod("2024.04.01 ~ 2024.06.30");
            
            // (더미) 하위 탭 데이터
            vo.setResultSummary(
                SafetyInspectionVO.ResultSummaryVO.builder()
                    .safetyGrade("B (양호)")
                    .summaryText("전반적으로 양호하나, 3층 외벽 일부 미세 균열 관찰됨.")
                    .build()
            );
            vo.setPartDefects(List.of(
                SafetyInspectionVO.PartDefectVO.builder().partName("외벽").defectType("누수, 균열").status("C (보통)").build()
            ));
            
            return vo;
        }
        return null; // 해당 연도 데이터 없음
    }

    @Override
    public List<Map<String, Object>> selectBlcmHistTargetListByPnu(String pnu) throws Exception {
        return facHistoryMapper.selectBlcmHistTargetListByPnu(pnu);
    }

    @Override
    public List<Map<String, Object>> selectBlcmHistTargetListByCoord(double longitude, double latitude) throws Exception {
        return facHistoryMapper.selectBlcmHistTargetListByCoord(longitude, latitude);
    }
}