package space.nstart.pcp.pcp_satellites_service.service

import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import space.nstart.pcp.pcp_satellites_service.repository.ComplexPlanRunRepository
import space.nstart.pcp.pcp_satellites_service.model.mode.SurveyMode
import java.time.LocalDateTime
import kotlin.system.measureTimeMillis

@Service
class ComplexPlanPersistenceService(
    private val satelliteModePersistenceService: SatelliteModePersistenceService,
    private val complexPlanRunRepository: ComplexPlanRunRepository
) {

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

    fun clearModesForInterval(
        satelliteIds: Collection<Long>,
        intervalStart: LocalDateTime,
        intervalEnd: LocalDateTime
    ) {
        val durationMs = measureTimeMillis {
            satelliteModePersistenceService.deleteModesForInterval(
                satelliteIds = satelliteIds,
                intervalStart = intervalStart,
                intervalEnd = intervalEnd
            )
        }
        logger.info(
            "Complex plan persistence clear-modes completed: satellites={}, interval=[{} - {}], durationMs={}",
            satelliteIds.distinct(),
            intervalStart,
            intervalEnd,
            durationMs
        )
    }

    fun saveModesForInterval(
        satelliteIds: Collection<Long>,
        intervalStart: LocalDateTime,
        intervalEnd: LocalDateTime,
        surveysBySatelliteId: Map<Long, List<SurveyMode>>,
        complexPlanRunId: Long? = null
    ): SatelliteModeSaveResult {
        lateinit var result: SatelliteModeSaveResult
        val durationMs = measureTimeMillis {
            result = satelliteModePersistenceService.saveModesForInterval(
                satelliteIds = satelliteIds,
                intervalStart = intervalStart,
                intervalEnd = intervalEnd,
                surveysBySatelliteId = surveysBySatelliteId,
                complexPlanRun = complexPlanRunId?.let { complexPlanRunRepository.getReferenceById(it) }
            )
        }
        logger.info(
            "Complex plan persistence save-modes completed: runId={}, satellites={}, modes={}, durationMs={}",
            complexPlanRunId,
            satelliteIds.distinct(),
            result.calculatedModesCount,
            durationMs
        )
        return result
    }

    fun replaceModesForInterval(
        satelliteIds: Collection<Long>,
        intervalStart: LocalDateTime,
        intervalEnd: LocalDateTime,
        surveysBySatelliteId: Map<Long, List<SurveyMode>>,
        complexPlanRunId: Long? = null
    ): SatelliteModeSaveResult {
        lateinit var result: SatelliteModeSaveResult
        val durationMs = measureTimeMillis {
            result = satelliteModePersistenceService.replaceModesForInterval(
                satelliteIds = satelliteIds,
                intervalStart = intervalStart,
                intervalEnd = intervalEnd,
                surveysBySatelliteId = surveysBySatelliteId,
                complexPlanRun = complexPlanRunId?.let { complexPlanRunRepository.getReferenceById(it) }
            )
        }
        logger.info(
            "Complex plan persistence replace-modes completed: runId={}, satellites={}, modes={}, durationMs={}",
            complexPlanRunId,
            satelliteIds.distinct(),
            result.calculatedModesCount,
            durationMs
        )
        return result
    }

    fun clearAll() {
        val durationMs = measureTimeMillis {
            satelliteModePersistenceService.clearAll()
        }
        logger.info("Complex plan persistence clear-all completed: durationMs={}", durationMs)
    }
}
