package space.nstart.pcp.pcp_request_service.service

import org.junit.jupiter.api.Test
import space.nstart.pcp.pcp_request_service.configuration.CustomValidationException
import space.nstart.pcp.pcp_types_lib.dto.ballistics.TLEDTO
import space.nstart.pcp.pcp_types_lib.dto.ballistics.TleRevolutionsRequestDTO
import kotlin.test.assertFailsWith
import kotlin.test.assertTrue

class TLEServiceTest {

    @Test
    fun `revolutions calculates ascending nodes from tle epoch`() {
        val service = TLEService()

        val revolutions = service.revolutions(
            TleRevolutionsRequestDTO(
                tle = issTle,
                days = 1.0
            )
        )

        val epoch = service.parseTLE(issTle).time
        assertTrue(revolutions.isNotEmpty())
        assertTrue(revolutions.zipWithNext().all { (previous, next) -> previous.time < next.time })
        assertTrue(revolutions.zipWithNext().all { (previous, next) -> previous.revolution < next.revolution })
        assertTrue(revolutions.all { it.time >= epoch })
        assertTrue(revolutions.all { it.localMeanTime != null })
        assertTrue(revolutions.all { it.keps != null })
    }

    @Test
    fun `revolutions rejects non-positive duration`() {
        val service = TLEService()

        assertFailsWith<CustomValidationException> {
            service.revolutions(
                TleRevolutionsRequestDTO(
                    tle = issTle,
                    days = 0.0
                )
            )
        }
    }

    private companion object {
        val issTle = TLEDTO(
            header = "ISS",
            first = "1 25994U 99068A   26167.13049375  .00000315  00000-0  73126-4 0  9997",
            second = "2 25994  97.9457 216.7186 0001501 193.3628 244.8059 14.61102329409455"
        )
    }
}
