package space.nstart.pcp.routepassportconsumer.controller

import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.core.io.ClassPathResource
import org.springframework.kafka.core.KafkaTemplate
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import space.nstart.pcp.routepassportconsumer.config.KafkaTopicsProperties
import java.nio.charset.StandardCharsets

@RestController
@RequestMapping("/api/route-processing")
@Tag(name = "Route Processing", description = "Тестовые endpoint'ы сервиса обработки маршрутов")
@ConditionalOnProperty(
    name = ["app.route-processing.test-controller.enabled"],
    havingValue = "true",
    matchIfMissing = false,
)
class RouteProcessingTestController(
    private val kafkaTemplate: KafkaTemplate<String, String>,
    private val kafkaTopicsProperties: KafkaTopicsProperties,
) {

    @GetMapping("/test")
    @Operation(summary = "Проверка доступности сервиса")
    fun test(): Map<String, String> = mapOf(
        "status" to "ok",
        "service" to "pcp-route-processing-service",
    )

    @PostMapping("/test/send-passport-message")
    @Operation(summary = "Отправить тестовый паспорт маршрута в Kafka")
    fun sendPassportMessage(): Map<String, String> {
        val payload = ClassPathResource("test-passport-message.json")
            .inputStream
            .readAllBytes()
            .toString(StandardCharsets.UTF_8)

        kafkaTemplate.send(kafkaTopicsProperties.input, payload)

        return mapOf(
            "status" to "sent",
            "topic" to kafkaTopicsProperties.input,
            "resource" to "classpath:test-passport-message.json",
        )
    }
}
