package space.nstart.pcp.pcp_mission_planing_service.service

import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import space.nstart.pcp.pcp_mission_planing_service.repository.MissionRepository
import space.nstart.pcp.pcp_mission_planing_service.repository.ModeRepository
import space.nstart.pcp.pcp_mission_planing_service.repository.PlanRepository

@Service
class SatelliteDeletedService(
    private val missionRepository: MissionRepository,
    private val planRepository: PlanRepository,
    private val modeRepository: ModeRepository
) {

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

    @Transactional
    fun deleteSatelliteData(satelliteId: Long): SatelliteMissionPlanningDeletionSummary {
        val missionsBeforeDelete = missionRepository.countBySatelliteId(satelliteId)
        val plansDeletedByCascade = planRepository.countByMissionSatelliteId(satelliteId)
        val modesDeletedByCascade = modeRepository.countByPlan_Mission_SatelliteId(satelliteId)
        val missionsDeleted = missionRepository.deleteAllBySatelliteId(satelliteId)
        val summary = SatelliteMissionPlanningDeletionSummary(
            satelliteId = satelliteId,
            missionsDeleted = missionsDeleted,
            plansDeletedByCascade = plansDeletedByCascade,
            modesDeletedByCascade = modesDeletedByCascade
        )
        logger.info(
            "Deleted mission-planning data for satellite: satelliteId={}, missionsBeforeDelete={}, missionsDeleted={}, plansDeletedByCascade={}, modesDeletedByCascade={}",
            summary.satelliteId,
            missionsBeforeDelete,
            summary.missionsDeleted,
            summary.plansDeletedByCascade,
            summary.modesDeletedByCascade
        )
        return summary
    }
}

data class SatelliteMissionPlanningDeletionSummary(
    val satelliteId: Long,
    val missionsDeleted: Long,
    val plansDeletedByCascade: Long,
    val modesDeletedByCascade: Long
)
