package com.tt.honeyDue.widget

import kotlinx.datetime.LocalDateTime
import org.junit.Assert.assertEquals
import org.junit.Test

/**
 * Pure-logic tests for [WidgetRefreshSchedule]. No Android/framework deps.
 *
 * Cadence matches iOS-parity spec:
 *  - 06:00 (inclusive) .. 23:00 (exclusive) local → 30-minute interval
 *  - 23:00 (inclusive) .. 06:00 (exclusive) local → 120-minute interval
 *
 * (iOS [BackgroundTaskManager.swift] uses a random-window overnight refresh;
 * Android uses WorkManager and the plan specifies this fixed-cadence split
 * since WorkManager can't simulate the iOS random-BGTask scheduling.)
 */
class WidgetRefreshScheduleTest {

    private fun dt(hour: Int, minute: Int = 0): LocalDateTime =
        LocalDateTime(year = 2026, monthNumber = 4, dayOfMonth = 16, hour = hour, minute = minute)

    @Test
    fun intervalMinutes_at_09am_returns_30() {
        assertEquals(30L, WidgetRefreshSchedule.intervalMinutes(dt(9, 0)))
    }

    @Test
    fun intervalMinutes_at_22_59_returns_30() {
        assertEquals(30L, WidgetRefreshSchedule.intervalMinutes(dt(22, 59)))
    }

    @Test
    fun intervalMinutes_at_23_00_returns_120() {
        assertEquals(120L, WidgetRefreshSchedule.intervalMinutes(dt(23, 0)))
    }

    @Test
    fun intervalMinutes_at_05_59_returns_120() {
        assertEquals(120L, WidgetRefreshSchedule.intervalMinutes(dt(5, 59)))
    }

    @Test
    fun intervalMinutes_at_06_00_returns_30() {
        assertEquals(30L, WidgetRefreshSchedule.intervalMinutes(dt(6, 0)))
    }

    @Test
    fun intervalMinutes_at_02_00_returns_120() {
        assertEquals(120L, WidgetRefreshSchedule.intervalMinutes(dt(2, 0)))
    }

    @Test
    fun intervalMinutes_at_midnight_returns_120() {
        assertEquals(120L, WidgetRefreshSchedule.intervalMinutes(dt(0, 0)))
    }

    @Test
    fun intervalMinutes_at_noon_returns_30() {
        assertEquals(30L, WidgetRefreshSchedule.intervalMinutes(dt(12, 0)))
    }

    @Test
    fun nextRefreshTime_at_09am_is_09_30() {
        val next = WidgetRefreshSchedule.nextRefreshTime(dt(9, 0))
        assertEquals(9, next.hour)
        assertEquals(30, next.minute)
        assertEquals(16, next.dayOfMonth)
    }

    @Test
    fun nextRefreshTime_at_23_00_is_01_00_next_day() {
        val next = WidgetRefreshSchedule.nextRefreshTime(dt(23, 0))
        assertEquals(1, next.hour)
        assertEquals(0, next.minute)
        assertEquals(17, next.dayOfMonth)
    }

    @Test
    fun nextRefreshTime_at_22_45_is_23_15_same_day() {
        // 22:45 + 30min = 23:15 (still 30min because 22:45 < 23:00)
        val next = WidgetRefreshSchedule.nextRefreshTime(dt(22, 45))
        assertEquals(23, next.hour)
        assertEquals(15, next.minute)
        assertEquals(16, next.dayOfMonth)
    }

    @Test
    fun nextRefreshTime_at_05_30_is_07_30_same_day() {
        // 05:30 + 120min = 07:30
        val next = WidgetRefreshSchedule.nextRefreshTime(dt(5, 30))
        assertEquals(7, next.hour)
        assertEquals(30, next.minute)
        assertEquals(16, next.dayOfMonth)
    }
}
