package space.nstart.pcp.pcp_request_service.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.transaction.annotation.Transactional
import space.nstart.pcp.pcp_request_service.entity.SatelliteInitialConditionEntity

interface SatelliteInitialConditionRepository : JpaRepository<SatelliteInitialConditionEntity, Long> {
    fun findFirstBySatelliteIdAndLastCalculatedTrueOrderByCalculatedAtDescIdDesc(
        satelliteId: Long
    ): SatelliteInitialConditionEntity?

    @Modifying
    @Transactional
    @Query(
        """
        update SatelliteInitialConditionEntity entity
        set entity.lastCalculated = false
        where entity.satelliteId = :satelliteId
          and entity.lastCalculated = true
        """
    )
    fun clearLastCalculated(@Param("satelliteId") satelliteId: Long): Int
}
