package incheon.uis.uer.service.impl;

import incheon.uis.uer.mapper.UerShpMapper;
import incheon.uis.uer.vo.TaskLayerVO;
import incheon.uis.uer.service.UerShpService;
import incheon.uis.ums.shp.ShapeDownloadUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.zip.ZipOutputStream;

@Service
public class UerShpServiceImpl implements UerShpService {
    private final UerShpMapper uerShpMapper;

    /**
     * GeoServer WFS Endpoint
     * (application.yml / properties 에 정의)
     * ex) wfs.base-url=http://10.100.232.241/MapPrimeServer/map/wfs
     */
    @Value("${gis.server.url}")
    private String wfsBaseUrl;

    public UerShpServiceImpl(UerShpMapper uerShpMapper) {
        this.uerShpMapper = uerShpMapper;
    }

    @Override
    public List<TaskLayerVO> getTaskLayerList() {
        // group_cd 고정이므로 그냥 전체 조회
        return uerShpMapper.selectTaskLayerList();
    }

    @Override
    public void addLayerShpToZip(ZipOutputStream zos,
                                 String typeName,
                                 String cqlFilter,
                                 String entryLabel) {

        try {
            ShapeDownloadUtil.addLayerFromWfs(
                    zos,
                    wfsBaseUrl,
                    typeName,
                    entryLabel,   // task_lyr_expln 기반 이름 (없으면 null/빈값)
                    cqlFilter
            );
        } catch (IOException e) {
            // 프로젝트 스타일에 맞게 예외 래핑/로깅
            throw new RuntimeException("SHP 레이어 ZIP 생성 중 오류(typeName=" + typeName + ")", e);
        }
    }
}
