package space.nstart.pcp.slots_service.dto

import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
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.Assertions.assertTrue
import org.junit.jupiter.api.Test
import space.nstart.pcp.pcp_types_lib.dto.requests.ComplexPlanProcessRequestDTO
import space.nstart.pcp.pcp_types_lib.dto.requests.slots.SlotCoverageBatchRequestDTO
import space.nstart.pcp.pcp_types_lib.dto.requests.slots.SlotCoverageStrategy
import java.time.LocalDateTime

class CoverageStrategyDTOTest {

    private val mapper = jacksonObjectMapper().registerModule(JavaTimeModule())
    private val intervalStart = LocalDateTime.of(2026, 3, 26, 10, 0)
    private val intervalEnd = LocalDateTime.of(2026, 3, 26, 12, 0)

    @Test
    fun `slot coverage batch request defaults strategy to greedy`() {
        val request = SlotCoverageBatchRequestDTO(
            timeStart = intervalStart,
            timeStop = intervalEnd
        )

        assertEquals(SlotCoverageStrategy.GREEDY, request.coverageStrategy)
    }

    @Test
    fun `slot coverage batch request copy keeps coverage strategy`() {
        val request = SlotCoverageBatchRequestDTO(
            timeStart = intervalStart,
            timeStop = intervalEnd,
            coverageStrategy = SlotCoverageStrategy.CONTINUOUS
        )

        assertEquals(SlotCoverageStrategy.CONTINUOUS, request.copy(cov = true).coverageStrategy)
    }

    @Test
    fun `slot coverage batch request serializes coverage strategy`() {
        val json = mapper.writeValueAsString(
            SlotCoverageBatchRequestDTO(
                timeStart = intervalStart,
                timeStop = intervalEnd,
                coverageStrategy = SlotCoverageStrategy.CONTINUOUS
            )
        )

        assertTrue(json.contains("\"coverageStrategy\":\"CONTINUOUS\""))
    }

    @Test
    fun `slot coverage batch request deserializes missing strategy as greedy`() {
        val request = mapper.readValue<SlotCoverageBatchRequestDTO>(
            """
            {
              "items": [],
              "timeStart": "2026-03-26T10:00:00",
              "timeStop": "2026-03-26T12:00:00"
            }
            """.trimIndent()
        )

        assertEquals(SlotCoverageStrategy.GREEDY, request.coverageStrategy)
    }

    @Test
    fun `complex plan process request copy keeps coverage strategy`() {
        val request = ComplexPlanProcessRequestDTO(
            intervalStart = intervalStart,
            intervalEnd = intervalEnd,
            coverageStrategy = SlotCoverageStrategy.CONTINUOUS
        )

        assertEquals(SlotCoverageStrategy.CONTINUOUS, request.copy(satelliteId = 22L).coverageStrategy)
    }
}
