package space.nstart.pcp.complan.apl

import com.fasterxml.jackson.databind.ObjectMapper
import org.junit.jupiter.api.io.TempDir
import space.nstart.pcp.complan.JacksonConfig
import space.nstart.pcp.complan.types.IntervalItemDTO
import space.nstart.pcp.complan.types.ObservationParametersDebugFileDTO
import space.nstart.pcp.pcp_types_lib.dto.ballistics.RevolutionSign
import space.nstart.pcp.pcp_types_lib.dto.ballistics.SquareViewParamDTO
import java.nio.file.Files
import java.nio.file.Path
import java.time.LocalDateTime
import java.util.UUID
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class ObservationParametersDebugFileWriterTest {

    private val objectMapper: ObjectMapper = JacksonConfig().objectMapper()

    @Test
    fun `writes observation parameters artifact with intervals and mpl`(@TempDir tempDir: Path) {
        val requestId = UUID.fromString("11111111-1111-1111-1111-111111111111")
        val intervalId = UUID.fromString("22222222-2222-2222-2222-222222222222")
        val writer = ObservationParametersDebugFileWriter(
            objectMapper = objectMapper,
            outputDir = tempDir.resolve("observation-parameters").toString()
        )
        val artifact = ObservationParametersDebugFileDTO(
            createdAt = LocalDateTime.parse("2026-05-05T10:15:30"),
            requestId = requestId,
            satelliteIds = listOf(1001L),
            missingSatelliteIds = emptyList(),
            calculationStart = LocalDateTime.parse("2026-05-05T00:00:00"),
            calculationEnd = LocalDateTime.parse("2026-05-06T00:00:00"),
            parallelBallisticsCalculation = true,
            chunkSize = 10,
            parallelism = 2,
            intervalsCount = 1,
            observationParametersCount = 1,
            intervals = listOf(
                IntervalItemDTO(
                    id = intervalId,
                    regionId = UUID.fromString("33333333-3333-3333-3333-333333333333"),
                    revSign = RevolutionSign.ASC,
                    geometry = "POLYGON ((0 0, 1 0, 1 1, 0 0))",
                    mpl = mutableListOf(
                        SquareViewParamDTO(
                            noradId = 1001L,
                            objectId = intervalId.toString(),
                            revolutionBegin = 42L,
                            timeBegin = LocalDateTime.parse("2026-05-05T01:00:00"),
                            revolutionEnd = 42L,
                            timeEnd = LocalDateTime.parse("2026-05-05T01:01:00")
                        )
                    )
                )
            )
        )

        val file = writer.write(artifact)

        assertTrue(Files.exists(file))
        assertEquals("observation-parameters-$requestId.json", file.fileName.toString())
        assertEquals(file, writer.filePathFor(requestId))
        val saved = writer.readByRequestId(requestId)
        assertEquals(1, saved.schemaVersion)
        assertEquals(intervalId, saved.intervals.single().id)
        assertEquals(1001L, saved.intervals.single().mpl.single().noradId)
    }
}
