package space.nstart.pcp.pcp_satellites_service.service

import org.junit.jupiter.api.Test
import space.nstart.pcp.pcp_satellites_service.model.mode.SurveyMode
import space.nstart.pcp.pcp_types_lib.dto.satellite.mission.DailyDurationDTO
import space.nstart.pcp.pcp_types_lib.dto.satellite.mission.RevolutionDurationDTO
import java.time.LocalDate
import java.time.LocalDateTime
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals

class SatelliteServiceTest {

    @Test
    fun `mission statistics groups durations by revolution`() {
        val service = SatelliteService(testSatelliteCatalogClient())
        val satellite = service.satellites.first { it.satelliteId == 22L }
        satellite.longMission.surveys.clear()

        satellite.longMission.surveys.add(
            SurveyMode(
                revolution = 101,
                time = LocalDateTime.of(2026, 3, 26, 10, 0),
                timeStop = LocalDateTime.of(2026, 3, 26, 10, 5),
                duration = 300.0
            )
        )
        satellite.longMission.surveys.add(
            SurveyMode(
                revolution = 101,
                time = LocalDateTime.of(2026, 3, 26, 10, 10),
                timeStop = LocalDateTime.of(2026, 3, 26, 10, 12),
                duration = 120.0
            )
        )
        satellite.longMission.surveys.add(
            SurveyMode(
                revolution = 102,
                time = LocalDateTime.of(2026, 3, 26, 11, 0),
                timeStop = LocalDateTime.of(2026, 3, 26, 11, 3),
                duration = 180.0,
                contourWKT = "POLYGON ((0 0, 2 0, 2 1, 0 1, 0 0))"
            )
        )
        satellite.longMission.surveys.add(
            SurveyMode(
                revolution = 103,
                time = LocalDateTime.of(2026, 3, 26, 23, 59),
                timeStop = LocalDateTime.of(2026, 3, 27, 0, 1),
                duration = 120.0,
                contourWKT = "POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))"
            )
        )

        val stats = service.missionStatistics(22L)

        assertEquals(4, stats.modesCount)
        assertEquals(3.0, stats.totalContourArea)
        assertContentEquals(
            listOf(
                RevolutionDurationDTO(revolution = 101, totalDuration = 420.0),
                RevolutionDurationDTO(revolution = 102, totalDuration = 180.0),
                RevolutionDurationDTO(revolution = 103, totalDuration = 120.0)
            ),
            stats.revolutionDurations
        )
        assertContentEquals(
            listOf(
                DailyDurationDTO(date = LocalDate.of(2026, 3, 26), totalDuration = 660.0),
                DailyDurationDTO(date = LocalDate.of(2026, 3, 27), totalDuration = 60.0)
            ),
            stats.dailyDurations
        )
    }
}
