package space.nstart.pcp.complan.db.repository

import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param
import org.springframework.stereotype.Repository
import space.nstart.pcp.complan.db.entity.DynamicPlanRouteEntity
import java.util.UUID

@Repository
interface DynamicPlanRouteRepository : JpaRepository<DynamicPlanRouteEntity, Long> {
    fun countBySatelliteId(satelliteId: Long): Long

    @Modifying
    @Query("delete from DynamicPlanRouteEntity route where route.satelliteId = :satelliteId")
    fun deleteAllBySatelliteId(@Param("satelliteId") satelliteId: Long): Int

    @Query(
        value = """
            select *
            from dynamic_plan_route
            where run_id = :runId
            order by start_time asc, id asc
            limit :limit offset :offset
        """,
        nativeQuery = true
    )
    fun findRoutesByRunId(
        @Param("runId") runId: UUID,
        @Param("limit") limit: Int,
        @Param("offset") offset: Int
    ): List<DynamicPlanRouteEntity>
}
