package app.closer.notifications

import app.closer.core.notifications.QuietHours
import java.util.Calendar

/**
 * Decides whether a notification should be suppressed because the current time
 * falls inside the user's quiet hours window.
 *
 * Default quiet hours are 22:00 → 08:00 and are user-configurable via
 * [app.closer.domain.repository.SettingsRepository].
 */
class QuietHoursManager {

    /**
     * Returns true if [now] is inside the configured quiet window.
     *
     * Windows that cross midnight (e.g. 22:00 → 08:00) are handled correctly.
     */
    fun isInQuietHours(
        quietHours: QuietHours,
        now: Calendar = Calendar.getInstance()
    ): Boolean {
        if (!quietHours.enabled) return false

        val currentMinutes = now.get(Calendar.HOUR_OF_DAY) * 60 + now.get(Calendar.MINUTE)
        val startMinutes = quietHours.startHour * 60 + quietHours.startMinute
        val endMinutes = quietHours.endHour * 60 + quietHours.endMinute

        return if (startMinutes <= endMinutes) {
            currentMinutes in startMinutes..endMinutes
        } else {
            currentMinutes >= startMinutes || currentMinutes <= endMinutes
        }
    }
}
