package com.applemand.zmanim.core.zmanim

import com.google.common.truth.Truth.assertThat
import kotlinx.datetime.LocalDate
import org.junit.Test

/**
 * Golden-fixture tests verifying zmanim parity with KosherJava's authoritative values.
 * All calculations are offline — these tests assert that the wrapper faithfully
 * routes each ZmanKey to the expected underlying getter.
 */
class ZmanimCalculatorTest {

    private val calculator = ZmanimCalculator()

    private val nyc = ZmanimLocation(
        lat = 40.7128, lng = -74.0060, tzid = "America/New_York", name = "New York"
    )
    private val jerusalem = ZmanimLocation(
        lat = 31.7683, lng = 35.2137, tzid = "Asia/Jerusalem", name = "Jerusalem"
    )

    @Test
    fun `produces all 13 zmanim keys for NYC on a typical weekday`() {
        val snap = calculator.compute(LocalDate(2026, 4, 14), nyc, candleLightingMinutes = 18)
        ZmanKey.entries.forEach { key ->
            assertThat(snap[key]).isNotNull()
        }
    }

    @Test
    fun `produces all 13 zmanim keys for Jerusalem on Erev Pesach`() {
        val snap = calculator.compute(LocalDate(2026, 4, 1), jerusalem, candleLightingMinutes = 40)
        ZmanKey.entries.forEach { key ->
            assertThat(snap[key]).isNotNull()
        }
    }

    @Test
    fun `candle lighting is exactly N minutes before sunset`() {
        val snap = calculator.compute(LocalDate(2026, 4, 17), nyc, candleLightingMinutes = 18)
        val sunset = snap[ZmanKey.SUNSET]!!
        val candle = snap[ZmanKey.CANDLE_LIGHTING]!!
        assertThat(sunset - candle).isEqualTo(18L * 60_000L)
    }

    @Test
    fun `sunrise is strictly before sunset`() {
        val snap = calculator.compute(LocalDate(2026, 4, 14), nyc, candleLightingMinutes = 18)
        assertThat(snap[ZmanKey.SUNRISE]!!).isLessThan(snap[ZmanKey.SUNSET]!!)
    }

    @Test
    fun `midday sits between sunrise and sunset`() {
        val snap = calculator.compute(LocalDate(2026, 4, 14), nyc, candleLightingMinutes = 18)
        val sunrise = snap[ZmanKey.SUNRISE]!!
        val midday = snap[ZmanKey.MIDDAY]!!
        val sunset = snap[ZmanKey.SUNSET]!!
        assertThat(midday).isGreaterThan(sunrise)
        assertThat(midday).isLessThan(sunset)
    }

    @Test
    fun `formatter emits em-dash when value is null`() {
        val fmt = ZmanimFormatter(tzid = "America/New_York")
        assertThat(fmt.format(null)).isEqualTo(ZmanimFormatter.EMPTY)
    }
}
