package space.nstart.pcp.slots_service.dto.tgu

import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import java.time.Instant
import java.time.LocalDateTime
import java.util.UUID

/**
 * Контракт формата времени на проводе для DTO вкладки ТГУ в pcp-ui-service:
 * времена плана ([TguPlanResponse]) — naive `LocalDateTime` без зоны (как отдаёт pcp-tgu-service),
 * `Instant` ([TguPlatformResponse.validFrom]) — с `Z` (как отдаёт НСИ-прокси).
 */
class TguPlanningDtoContractTest {

    private val mapper = jacksonObjectMapper().findAndRegisterModules()
        .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)

    @Test
    fun `serializes plan times as naive ISO without zone`() {
        val plan = TguPlanResponse(
            planId = UUID.fromString("00000000-0000-0000-0000-000000000001"),
            spacecraftId = "25544",
            startTime = LocalDateTime.of(2026, 5, 29, 1, 0, 0),
            endTime = LocalDateTime.of(2026, 5, 29, 2, 0, 0),
            kppId = "KPP-1",
            status = "PLANNED"
        )

        val node = mapper.readTree(mapper.writeValueAsString(plan))

        assertEquals("2026-05-29T01:00:00", node.get("startTime").asText())
        assertEquals("2026-05-29T02:00:00", node.get("endTime").asText())
        assertFalse(node.get("startTime").asText().endsWith("Z"), "не ожидалась зона Z")
    }

    @Test
    fun `serializes platform validity instants with explicit Z zone`() {
        val platform = TguPlatformResponse(
            id = "25544",
            businessKey = "bk-1",
            name = "SAT",
            status = "OPERATIONAL",
            noradId = 25544L,
            mission = "obs",
            validFrom = Instant.parse("2026-05-29T01:00:00Z"),
            validTo = Instant.parse("2026-12-31T23:00:00Z")
        )

        val node = mapper.readTree(mapper.writeValueAsString(platform))

        assertEquals("2026-05-29T01:00:00Z", node.get("validFrom").asText())
        assertTrue(node.get("validTo").asText().endsWith("Z"), "ожидалась явная зона Z")
    }
}
