package space.nstart.pcp.complan.apl

import org.locationtech.jts.io.WKTReader
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class IntervalCoverageCheckerTest {

    private val reader = WKTReader()
    private val checker = IntervalCoverageChecker()

    @Test
    fun `returns empty geometry when intervals fully cover contour`() {
        val contour = reader.read("POLYGON ((0 0, 2 0, 2 1, 0 1, 0 0))")
        val intervals = listOf(
            reader.read("POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))"),
            reader.read("POLYGON ((1 0, 2 0, 2 1, 1 1, 1 0))")
        )

        val uncovered = checker.findUncoveredGeometry(contour, intervals)

        assertTrue(uncovered.isEmpty)
    }

    @Test
    fun `returns uncovered part when intervals leave gap in contour`() {
        val contour = reader.read("POLYGON ((0 0, 2 0, 2 1, 0 1, 0 0))")
        val intervals = listOf(
            reader.read("POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))")
        )

        val uncovered = checker.findUncoveredGeometry(contour, intervals)

        assertEquals(1.0, uncovered.area)
    }

    @Test
    fun `returns full contour when intervals are empty`() {
        val contour = reader.read("POLYGON ((0 0, 2 0, 2 1, 0 1, 0 0))")

        val uncovered = checker.findUncoveredGeometry(contour, emptyList())

        assertEquals(contour.area, uncovered.area)
    }
}
