package me.khodak.claudeusage

import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
import java.util.Calendar
import java.util.GregorianCalendar
import java.util.TimeZone

/**
 * Pure-JVM tests for PeakHours.isPeak. The clock is injected via `now`, and PeakHours evaluates the
 * window in America/Los_Angeles regardless of the device timezone — so we build each instant in PT
 * explicitly, making these deterministic without touching the system clock.
 */
class PeakHoursTest {

    private val pt: TimeZone = TimeZone.getTimeZone("America/Los_Angeles")

    /** Absolute epoch millis for the given wall-clock time in PT on a fixed reference date. */
    private fun ptInstant(year: Int, month0: Int, day: Int, hour: Int): Long {
        val cal = GregorianCalendar(pt)
        cal.clear()
        cal.set(year, month0, day, hour, 0, 0)
        return cal.timeInMillis
    }

    @Test
    fun activeOnWeekdayInsideWindow() {
        // 2026-06-10 is a Wednesday. 8 AM PT is inside 5–11 AM.
        assertTrue(PeakHours.isPeak(ptInstant(2026, Calendar.JUNE, 10, 8)))
    }

    @Test
    fun inactiveAtStartBoundaryIsInclusiveButBeforeIsNot() {
        // 5 AM PT is inside (inclusive start); 4 AM PT is outside.
        assertTrue(PeakHours.isPeak(ptInstant(2026, Calendar.JUNE, 10, 5)))
        assertFalse(PeakHours.isPeak(ptInstant(2026, Calendar.JUNE, 10, 4)))
    }

    @Test
    fun inactiveAtEndBoundaryExclusive() {
        // 11 AM PT is the exclusive end → not peak.
        assertFalse(PeakHours.isPeak(ptInstant(2026, Calendar.JUNE, 10, 11)))
    }

    @Test
    fun inactiveOnWeekendEvenInsideHours() {
        // 2026-06-13 is a Saturday, 2026-06-14 is a Sunday. 8 AM PT, but weekend → not peak.
        assertFalse(PeakHours.isPeak(ptInstant(2026, Calendar.JUNE, 13, 8)))
        assertFalse(PeakHours.isPeak(ptInstant(2026, Calendar.JUNE, 14, 8)))
    }

    @Test
    fun inactiveOnWeekdayEvening() {
        // 8 PM PT Wednesday → outside the morning window.
        assertFalse(PeakHours.isPeak(ptInstant(2026, Calendar.JUNE, 10, 20)))
    }
}
