package space.nstart.pcp.slots_service.service

import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import space.nstart.pcp.slots_service.repository.BookedSlotsRepository
import space.nstart.pcp.slots_service.repository.SatelliteIcRepository
import space.nstart.pcp.slots_service.repository.SlotRepository

@Service
class SatelliteDeletedService(
    private val slotRepository: SlotRepository,
    private val bookedSlotsRepository: BookedSlotsRepository,
    private val satelliteIcRepository: SatelliteIcRepository
) {

    private val logger = LoggerFactory.getLogger(this::class.java)

    @Transactional
    fun deleteSatelliteData(satelliteId: Long): SatelliteSlotsDeletionSummary {
        val bookedSlotsDeletedByCascade = bookedSlotsRepository.countBySlot_SatelliteId(satelliteId)
        val slotsBeforeDelete = slotRepository.countBySatelliteId(satelliteId)
        val slotsDeleted = slotRepository.deleteBySatId(satelliteId)
        val initialConditionsDeleted = satelliteIcRepository.deleteBySatelliteId(satelliteId)
        val summary = SatelliteSlotsDeletionSummary(
            satelliteId = satelliteId,
            slotsDeleted = slotsDeleted,
            bookedSlotsDeletedByCascade = bookedSlotsDeletedByCascade,
            initialConditionsDeleted = initialConditionsDeleted
        )
        logger.info(
            "Deleted slots data for satellite: satelliteId={}, slotsDeleted={}, slotsBeforeDelete={}, bookedSlotsDeletedByCascade={}, initialConditionsDeleted={}",
            summary.satelliteId,
            summary.slotsDeleted,
            slotsBeforeDelete,
            summary.bookedSlotsDeletedByCascade,
            summary.initialConditionsDeleted
        )
        return summary
    }
}

data class SatelliteSlotsDeletionSummary(
    val satelliteId: Long,
    val slotsDeleted: Int,
    val bookedSlotsDeletedByCascade: Long,
    val initialConditionsDeleted: Int
)
