package space.nstart.pcp.pcp_types_lib.message

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import space.nstart.pcp.pcp_types_lib.dto.routes.AngleRangeDto
import space.nstart.pcp.pcp_types_lib.dto.routes.RoutePassportDto
import java.math.BigDecimal
import java.time.LocalDateTime
import java.util.UUID

class KafkaMessageSerializationTest {

    private val objectMapper = jacksonObjectMapper().findAndRegisterModules()

    @Test
    fun `should serialize traceId and correlationId without changing values`() {
        val message = KafkaMessage(
            type = PcpKafkaEvent.RouteGeoRefEvent,
            data = RoutePassportDto(
                routeId = UUID.fromString("51b07d12-1823-4bd5-b55d-de02a5304c03"),
                kaShort = "S1A",
                kaFull = "sentinel-1a",
                routeNameFull = "ROUTE-001",
                routeNameShort = "S1A-GRDH",
                orbitNumber = 59796L,
                orbitState = "Descending",
                intervalBegin = LocalDateTime.of(2026, 4, 1, 10, 15, 0),
                intervalEnd = LocalDateTime.of(2026, 4, 1, 10, 16, 30),
                rollAngle = AngleRangeDto(BigDecimal("-15.5"), BigDecimal("-12.0")),
                visirAngle = AngleRangeDto(BigDecimal("21.0"), BigDecimal("24.5")),
                resolutionRange = BigDecimal("10.0"),
                resolutionAzimuth = BigDecimal("10.0"),
                polarisation = listOf("VH", "VV"),
                processingLevel = "GRD",
                geometry = "POLYGON((0 0, 1 0, 1 1, 0 0))",
            ),
            traceId = "trace-out",
            correlationId = "corr-out",
            group = "pcp",
        )

        val json = objectMapper.readTree(objectMapper.writeValueAsString(message))

        assertEquals("trace-out", json.path("traceId").asText())
        assertEquals("corr-out", json.path("correlationId").asText())
        assertEquals("", json.path("trace_id").asText())
        assertEquals("", json.path("correlationid").asText())
    }

    @Test
    fun `should deserialize legacy trace and correlation aliases`() {
        val json = """
            {
              "type": "RouteGeoRefEvent",
              "data": {
                "routeId": "51b07d12-1823-4bd5-b55d-de02a5304c03",
                "kaShort": "S1A",
                "kaFull": "sentinel-1a",
                "routeNameFull": "ROUTE-001",
                "routeNameShort": "S1A-GRDH",
                "orbitNumber": 59796,
                "orbitState": "Descending",
                "intervalBegin": "2026-04-01T10:15:00",
                "intervalEnd": "2026-04-01T10:16:30",
                "rollAngle": {"min": -15.5, "max": -12.0},
                "visirAngle": {"min": 21.0, "max": 24.5},
                "resolutionRange": 10.0,
                "resolutionAzimuth": 10.0,
                "polarisation": ["VH", "VV"],
                "processingLevel": "GRD",
                "geometry": "POLYGON((0 0, 1 0, 1 1, 0 0))"
              },
              "trace_id": "trace-legacy",
              "correlationid": "corr-legacy"
            }
        """.trimIndent()

        val message: KafkaMessage<RoutePassportDto> = objectMapper.readValue(json)

        assertEquals("trace-legacy", message.traceId)
        assertEquals("corr-legacy", message.correlationId)
    }
}
