package space.nstart.pcp.pcp_mission_planing_service.repository

import org.springframework.data.jpa.repository.Query
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.repository.query.Param
import space.nstart.pcp.pcp_mission_planing_service.entity.SurveyModeEntity
import java.time.LocalDateTime
import java.util.UUID

interface SurveyModeRepository : JpaRepository<SurveyModeEntity, Long> {
    fun findAllByPlan_IdOrderByTimeStartAsc(planId: Long): List<SurveyModeEntity>
    fun findAllByPlan_Mission_MissionIdOrderByTimeStartAsc(missionId: UUID): List<SurveyModeEntity>

    @Query(
        """
        select sm
        from SurveyModeEntity sm
        join sm.plan p
        join p.mission m
        where concat('', m.satelliteId) = :satelliteNumber
          and sm.revolution = :revolution
          and sm.timeStart between :timeStartFrom and :timeStartTo
        order by sm.timeStart asc, sm.id asc
        """
    )
    fun findAllBySatelliteNumberAndRevolutionAndTimeStartBetween(
        @Param("satelliteNumber") satelliteNumber: String,
        @Param("revolution") revolution: Long,
        @Param("timeStartFrom") timeStartFrom: LocalDateTime,
        @Param("timeStartTo") timeStartTo: LocalDateTime
    ): List<SurveyModeEntity>
}
