package routing

import (
	"testing"
	"time"
)

func TestInQuietHours(t *testing.T) {
	mkTime := func(h, m int) time.Time {
		return time.Date(0, 1, 1, h, m, 0, 0, time.UTC)
	}
	mkNow := func(h, m int) time.Time {
		// 2026-06-13 — a Saturday, in UTC.
		return time.Date(2026, 6, 13, h, m, 0, 0, time.UTC)
	}

	cases := []struct {
		name             string
		now              time.Time
		quietStart       time.Time
		quietEnd         time.Time
		tz               string
		want             bool
	}{
		{
			name:       "same-day window: 09:00-17:00, now=12:00",
			now:        mkNow(12, 0),
			quietStart: mkTime(9, 0),
			quietEnd:   mkTime(17, 0),
			tz:         "UTC",
			want:       true,
		},
		{
			name:       "same-day window: 09:00-17:00, now=08:00 (before)",
			now:        mkNow(8, 0),
			quietStart: mkTime(9, 0),
			quietEnd:   mkTime(17, 0),
			tz:         "UTC",
			want:       false,
		},
		{
			name:       "same-day window: 09:00-17:00, now=18:00 (after)",
			now:        mkNow(18, 0),
			quietStart: mkTime(9, 0),
			quietEnd:   mkTime(17, 0),
			tz:         "UTC",
			want:       false,
		},
		{
			name:       "wrap-around: 22:00-06:00, now=23:00 (in)",
			now:        mkNow(23, 0),
			quietStart: mkTime(22, 0),
			quietEnd:   mkTime(6, 0),
			tz:         "UTC",
			want:       true,
		},
		{
			name:       "wrap-around: 22:00-06:00, now=03:00 (in)",
			now:        mkNow(3, 0),
			quietStart: mkTime(22, 0),
			quietEnd:   mkTime(6, 0),
			tz:         "UTC",
			want:       true,
		},
		{
			name:       "wrap-around: 22:00-06:00, now=12:00 (out)",
			now:        mkNow(12, 0),
			quietStart: mkTime(22, 0),
			quietEnd:   mkTime(6, 0),
			tz:         "UTC",
			want:       false,
		},
		{
			name:       "always quiet: 00:00-00:00 (Carol's seed)",
			now:        mkNow(15, 0),
			quietStart: mkTime(0, 0),
			quietEnd:   mkTime(0, 0),
			tz:         "UTC",
			want:       true,
		},
		{
			name:       "never quiet: 12:00-12:00",
			now:        mkNow(15, 0),
			quietStart: mkTime(12, 0),
			quietEnd:   mkTime(12, 0),
			tz:         "UTC",
			want:       false,
		},
		{
			name:       "edge: same-day window, now exactly at start (in)",
			now:        mkNow(9, 0),
			quietStart: mkTime(9, 0),
			quietEnd:   mkTime(17, 0),
			tz:         "UTC",
			want:       true,
		},
		{
			name:       "edge: same-day window, now exactly at end (out)",
			now:        mkNow(17, 0),
			quietStart: mkTime(9, 0),
			quietEnd:   mkTime(17, 0),
			tz:         "UTC",
			want:       false,
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			got := inQuietHours(tc.now, tc.quietStart, tc.quietEnd, tc.tz)
			if got != tc.want {
				t.Errorf("inQuietHours(now=%v, start=%v, end=%v, tz=%q) = %v, want %v",
					tc.now.Format("15:04"), tc.quietStart.Format("15:04"), tc.quietEnd.Format("15:04"),
					tc.tz, got, tc.want)
			}
		})
	}
}
