package space.nstart.pcp.pcp_satellites_service.service

import org.springframework.stereotype.Component
import space.nstart.pcp.pcp_satellites_service.dto.ComplexPlanRunResponseDTO
import space.nstart.pcp.pcp_satellites_service.dto.ComplexPlanRunSummaryDTO
import space.nstart.pcp.pcp_satellites_service.entity.ComplexPlanRunEntity

@Component
class ComplexPlanRunMapper {

    fun toResponse(entity: ComplexPlanRunEntity): ComplexPlanRunResponseDTO =
        ComplexPlanRunResponseDTO(
            runId = entity.id ?: 0,
            status = entity.status,
            intervalStart = entity.intervalStart,
            intervalEnd = entity.intervalEnd,
            satelliteIds = satelliteIds(entity),
            requestedBy = entity.requestedBy,
            createdAt = entity.createdAt,
            startedAt = entity.startedAt,
            finishedAt = entity.finishedAt,
            durationMs = entity.durationMs,
            errorMessage = entity.errorMessage,
            calculatedModesCount = entity.calculatedModesCount,
            bookedModesCount = entity.bookedModesCount,
            complanModesCount = entity.complanModesCount,
            mixedModesCount = entity.mixedModesCount,
            affectedSatellitesCount = entity.affectedSatellitesCount,
            processedCellsCount = entity.processedCellsCount,
            bookedSlotLinksCount = entity.bookedSlotLinksCount,
            snapshotIds = snapshotIds(entity),
            requestPayload = entity.requestPayload,
            totalRequestsArea = entity.totalRequestsArea,
            coveredArea = entity.coveredArea,
            coveredAreaPercent = entity.coveredAreaPercent
        )

    fun toSummary(entity: ComplexPlanRunEntity): ComplexPlanRunSummaryDTO =
        ComplexPlanRunSummaryDTO(
            runId = entity.id ?: 0,
            status = entity.status,
            intervalStart = entity.intervalStart,
            intervalEnd = entity.intervalEnd,
            satelliteIds = satelliteIds(entity),
            requestedBy = entity.requestedBy,
            createdAt = entity.createdAt,
            startedAt = entity.startedAt,
            finishedAt = entity.finishedAt,
            durationMs = entity.durationMs,
            errorMessage = entity.errorMessage,
            calculatedModesCount = entity.calculatedModesCount,
            bookedModesCount = entity.bookedModesCount,
            complanModesCount = entity.complanModesCount,
            mixedModesCount = entity.mixedModesCount,
            affectedSatellitesCount = entity.affectedSatellitesCount,
            processedCellsCount = entity.processedCellsCount,
            bookedSlotLinksCount = entity.bookedSlotLinksCount,
            snapshotIds = snapshotIds(entity),
            totalRequestsArea = entity.totalRequestsArea,
            coveredArea = entity.coveredArea,
            coveredAreaPercent = entity.coveredAreaPercent
        )

    private fun satelliteIds(entity: ComplexPlanRunEntity): List<Long> =
        entity.satellites
            .map { it.satelliteId }
            .distinct()
            .sorted()

    private fun snapshotIds(entity: ComplexPlanRunEntity): List<Long> =
        entity.snapshots
            .mapNotNull { it.id }
            .distinct()
            .sorted()
}
