package space.nstart.pcp.pcp_request_service.service

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.ObjectProvider
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service
import org.springframework.web.reactive.function.client.WebClient
import space.nstart.pcp.pcp_types_lib.dto.satellite.SatelliteSummaryDTO

/** Клиент к pcp-satellite-catalog-service для отображения человекочитаемых данных КА. */
@Service
class SatelliteCatalogClient(webClientBuilderProvider: ObjectProvider<WebClient.Builder>) {
    private val webClientBuilder: WebClient.Builder = webClientBuilderProvider.ifAvailable ?: WebClient.builder()
    private val logger: Logger = LoggerFactory.getLogger(this::class.java)

    @Value("\${settings.satellite-catalog-service:pcp-satellite-catalog-service}")
    val url = ""

    fun getSatellites(): List<SatelliteSummaryDTO> =
        runCatching {
            webClientBuilder.build()
                .get()
                .uri("$url/api/satellites")
                .retrieve()
                .bodyToFlux(SatelliteSummaryDTO::class.java)
                .collectList()
                .block()
                ?: emptyList()
        }.getOrElse { e ->
            logger.error("Не удалось получить список КА из $url/api/satellites: ${e.message}")
            emptyList()
        }
}
