package incheon.cmm.g2f.sample.service.impl;

import org.egovframe.rte.fdl.cmmn.EgovAbstractServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import incheon.cmm.g2f.sample.mapper.G2FSampleMapper;
import incheon.cmm.g2f.sample.service.G2FSampleService;
import incheon.cmm.g2f.sample.vo.AddressSearchRequestVO;
import incheon.cmm.g2f.sample.vo.AddressSearchResponseVO;
import incheon.cmm.g2f.sample.vo.CoorSearchRequestVO;
import incheon.cmm.g2f.sample.vo.CoorSearchResponseVO;
import incheon.cmm.g2f.sample.vo.G2FSampleVO;
import incheon.cmm.g2f.sample.vo.PoiSearchRequestVO;
import incheon.cmm.g2f.sample.vo.PoiSearchResponseVO;
import incheon.cmm.g2f.poi.mapper.G2FPoiMapper;
import incheon.cmm.g2f.util.searchApiUtil;
import incheon.com.config.annotation.MainDB;
import lombok.extern.slf4j.Slf4j;  // ✅ 추가

import java.util.List;
@MainDB
@Slf4j  
@Service
public class G2FSampleServiceImpl extends EgovAbstractServiceImpl implements G2FSampleService {

    private final G2FSampleMapper mapper;
    private final searchApiUtil searchApiUtil;  

    public G2FSampleServiceImpl(G2FSampleMapper mapper, searchApiUtil searchApiUtil) {
        this.mapper = mapper;
        this.searchApiUtil = searchApiUtil;
    }

    @Override
    public G2FSampleVO getById(String nfId) {
        return mapper.findById(nfId);
    }

    @Override
    public List<G2FSampleVO> getList() {
        return mapper.findAll();
    }

    @Override
    public List<G2FSampleVO> getList(String searchKeyword, String searchType, int page, int size) {
        int offset = (page - 1) * size;
        return mapper.findAllWithPaging(searchKeyword, searchType, size, offset);
    }

    @Override
    public int getTotalCount(String searchKeyword, String searchType) {
        return mapper.count(searchKeyword, searchType);
    }

    @Override
    @Transactional
    public void create(G2FSampleVO poi) {
        mapper.insert(poi);
    }

    @Override
    @Transactional
    public void update(G2FSampleVO poi) {
        mapper.update(poi);
    }

    @Override
    @Transactional
    public void delete(String nfId) {
        mapper.delete(nfId);
    }
    
    @Override
    public AddressSearchResponseVO searchAddress(AddressSearchRequestVO request) throws Exception {
        log.debug("주소 검색 서비스 실행: {}", request);
        
        // 인천광역시가 포함되지 않은 경우 자동으로 추가
        String keyword = request.getKeyword().trim();
        if (!keyword.contains("인천광역시")) {
            keyword = "인천광역시 " + keyword;
        }
        request.setKeyword(keyword);
        
        // SearchApiUtil을 통해 외부 API 호출
        AddressSearchResponseVO response = searchApiUtil.searchAddress(request);
        
        log.debug("주소 검색 결과: 총 {}건", response.getResults().getCommon().getTotalCount());
        
        return response;
    }

    @Override
    public PoiSearchResponseVO searchPoi(PoiSearchRequestVO request) throws Exception {
        log.debug("POI 검색 서비스 실행: {}", request);
        
        if (request.getKeyword() == null || request.getKeyword().trim().isEmpty()) {
            throw new IllegalArgumentException("검색어를 입력해주세요.");
        }
        
        // SearchApiUtil을 통해 외부 API 호출
        PoiSearchResponseVO response = searchApiUtil.searchPoi(request);
        
        log.debug("POI 검색 결과: 총 {}건", response.getTotalHits());
        
        return response;
    }

    @Override
    public CoorSearchResponseVO searchPoiByLocation(CoorSearchRequestVO request) throws Exception {
        log.debug("좌표 기반 POI 검색 서비스 실행: {}", request);
        
        // 좌표 유효성 검증
        try {
            Double.parseDouble(request.getLat());
            Double.parseDouble(request.getLng());
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("유효한 좌표를 입력해주세요.");
        }
        
        // SearchApiUtil을 통해 외부 API 호출
        CoorSearchResponseVO response = searchApiUtil.searchPoiByLocation(request);
        
        log.debug("좌표 기반 POI 검색 결과: {}건", 
                response.getList() != null ? response.getList().size() : 0);
        
        return response;
    }
}