package incheon.ags.mrb.analysis.mapper;

import incheon.ags.mrb.analysis.vo.*;
import org.apache.ibatis.annotations.*;
import org.geotools.api.feature.simple.SimpleFeatureType;

import java.util.List;

/**
 * 공간 분석 매퍼 인터페이스
 */
@org.egovframe.rte.psl.dataaccess.mapper.Mapper @incheon.com.config.annotation.MainDB
public interface SpatialAnalysisMapper {

    /**
     * 분석을 위한 업무 레이어 조회
     */
    AnalysisLayerInfoDTO selectTaskLayerInfo(@Param("taskLyrId") int taskLyrId);

    /**
     * 분석을 위한 사용자 레이어 조회
     */
    AnalysisLayerInfoDTO selectUserLayerInfo(@Param("userLyrId") int userLyrId);

    /**
     * 분석 결과 테이블 생성
     */
    @UpdateProvider(type = SpatialAnalysisSqlProvider.class, method = "createTableSql")
    void createTable(@Param("type") SimpleFeatureType type,
                     @Param("schemaName") String schemaName,
                     @Param("tableName") String tableName,
                     @Param("geomId") String geomId,
                     @Param("geomColumn") String geomColumn,
                     @Param("geomType") String geomType,
                     @Param("srid") Integer srid);

    /**
     * 분석 결과 단일 데이터 추가
     */
    @InsertProvider(type = SpatialAnalysisSqlProvider.class, method = "insertFeatureSql")
    void rowInsert(@Param("feature") FeatureRowDTO feature,
                     @Param("type") SimpleFeatureType type,
                     @Param("schemaName") String schemaName,
                     @Param("tableName") String tableName);

    /**
     * 분석 결과 테이블 삭제
     */
    @UpdateProvider(type = SpatialAnalysisSqlProvider.class, method = "dropTableSql")
    void dropTableIfExists(@Param("schemaName") String schemaName,
                           @Param("tableName") String tableName);

    /**
     * 분석 결과 테이블 유무 확인
     */
    @Select("""
            SELECT EXISTS (
              SELECT 1
              FROM information_schema.tables
              WHERE table_schema = #{schema}
                AND table_name   = #{table}
            )
            """)
    boolean existsTable(@Param("schema") String schema, @Param("table") String table);

    /**
     * 유니크 인덱스 생성
     */
    @UpdateProvider(type = SpatialAnalysisSqlProvider.class, method = "createUniqueIndexSql")
    void createUniqueIndex(@Param("schema") String schema,
                          @Param("table") String table,
                          @Param("column") String column,
                          @Param("indexName") String indexName);

    /**
     * 공간 인덱스 생성
     */
    @UpdateProvider(type = SpatialAnalysisSqlProvider.class, method = "createSpatialIndexSql")
    void createSpatialIndex(@Param("schema") String schema,
                           @Param("table") String table,
                           @Param("column") String column,
                           @Param("indexName") String indexName);

    /**
     * 사용자 레이어 등록
     */
    int insertUserLayer(UserLyrVO userLyrVO);

    /**
     * 사용자 레이어 삭제
     */
    int deleteUserLayer(int userLyrId);

    /**
     * 사용자 레이어 user_id, user_lyr_nm 조합으로 단일 레코드를 조회
     */
    UserLyrVO selectUserLayer(@Param("userId") String userId, @Param("userLyrNm") String userLyrNm);

    /**
     * 레이어의 컬럼 이름 목록 조회
     */
    List<String> getColumnNames(@Param("schema") String schema, @Param("tableName") String tableName);

}
