package space.nstart.pcp.complan.service

import org.springframework.beans.factory.annotation.Value
import org.springframework.core.ParameterizedTypeReference
import org.springframework.stereotype.Service
import org.springframework.web.client.RestClient
import org.springframework.web.util.UriComponentsBuilder
import space.nstart.pcp.pcp_types_lib.dto.satellite.SatelliteSummaryDTO

/** Получение данных КА из satellite-catalog-service. */
@Service
class SatelliteCatalogServiceClient {
    @Value("\${complex-plan.satellite-catalog-service-url}")
    private lateinit var satelliteCatalogServiceUrl: String

    fun getSatellites(): List<SatelliteSummaryDTO> =
        RestClient.create()
            .get()
            .uri(buildSatelliteCatalogUri("/api/satellites"))
            .retrieve()
            .body(SATELLITE_SUMMARY_LIST_TYPE)
            .orEmpty()

    private fun buildSatelliteCatalogUri(path: String): String =
        UriComponentsBuilder
            .fromUriString(satelliteCatalogServiceUrl)
            .path(path)
            .toUriString()

    private companion object {
        val SATELLITE_SUMMARY_LIST_TYPE = object : ParameterizedTypeReference<List<SatelliteSummaryDTO>>() {}
    }
}
