package space.nstart.pcp_tgu_service.integration.kafka

import org.slf4j.LoggerFactory
import org.springframework.kafka.annotation.KafkaListener
import org.springframework.stereotype.Component
import space.nstart.pcp_tgu_service.config.TopicsProperties
import space.nstart.pcp_tgu_service.dto.VisibilityWindowsChangedMessage
import space.nstart.pcp_tgu_service.service.VisibilityRefreshJobService

/**
 * Kafka consumer, который только ставит refresh job и не выполняет расчёт или external I/O.
 */
@Component
class VisibilityWindowsChangedConsumer(
    val topicsProperties: TopicsProperties,
    private val visibilityRefreshJobService: VisibilityRefreshJobService
) {
    private val log = LoggerFactory.getLogger(javaClass)

    /** Обрабатывает сигнал изменения окон видимости как запрос на перечитывание у баллистики. */
    @KafkaListener(
        topics = ["#{__listener.topicsProperties.visibilityWindowsChanged}"],
        groupId = "\${spring.kafka.consumer.group-id}"
    )
    fun consume(message: VisibilityWindowsChangedMessage) {
        visibilityRefreshJobService.enqueue(
            spacecraftId = message.spacecraftId,
            noradId = message.noradId,
            reason = "KAFKA_VISIBILITY_CHANGED"
        )

        log.info(
            "Visibility refresh job enqueued from Kafka. spacecraftId={}, eventId={}",
            message.spacecraftId,
            message.eventId
        )
    }
}
