package org.nstart.dep265.requestservice.service

import org.nstart.dep265.requestservice.dto.GridRebuildResponseDto
import org.nstart.dep265.requestservice.entity.RequestStatus
import org.nstart.dep265.requestservice.repository.EarthCellRepository
import org.nstart.dep265.requestservice.repository.RequestCellRepository
import org.nstart.dep265.requestservice.repository.RequestRepository
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.time.OffsetDateTime
import java.time.ZoneOffset

class GridRebuildAlreadyRunningException : RuntimeException("Grid rebuild is already running")

@Service
class GridRebuildService(
    private val gridSettingsService: GridSettingsService,
    private val earthGridCatalogService: EarthGridCatalogService,
    private val requestCellRepository: RequestCellRepository,
    private val requestRepository: RequestRepository,
    private val requestGridProjectionService: RequestGridProjectionService,
    private val earthCellRepository: EarthCellRepository,
    private val gridRebuildLockService: GridRebuildLockService,
) {
    @Transactional
    fun rebuildGrid(): GridRebuildResponseDto {
        if (!gridRebuildLockService.tryAcquireTransactionLock()) {
            throw GridRebuildAlreadyRunningException()
        }

        val settings = gridSettingsService.getSettings()
        val activeRequests = requestRepository.findByStatusIn(ACTIVE_REQUEST_STATUSES)

        requestCellRepository.deleteAllInBatch()
        earthGridCatalogService.init(settings.calculationStep)

        activeRequests.forEach { request ->
            requestGridProjectionService.rebuildProjection(request)
        }

        return GridRebuildResponseDto(
            cellsCount = earthCellRepository.count(),
            rebuiltRequestProjectionsCount = activeRequests.size.toLong(),
            rebuiltAt = OffsetDateTime.now(ZoneOffset.UTC),
        )
    }

    private companion object {
        val ACTIVE_REQUEST_STATUSES = setOf(RequestStatus.ACCEPTED, RequestStatus.ACTIVE)
    }
}
