package game

import (
	"testing"
	"time"
)

func TestInAwayWindow(t *testing.T) {
	cases := []struct {
		name              string
		dl, start, end    int
		wantIn, wantToday bool
	}{
		{"non-crossing inside", 120, 0, 420, true, true},
		{"non-crossing before", 500, 0, 420, false, false},
		{"non-crossing at start", 0, 0, 420, true, true},
		{"non-crossing at end excluded", 420, 0, 420, false, false},
		{"crossing evening", 1380, 1320, 360, true, false},
		{"crossing morning", 180, 1320, 360, true, true},
		{"crossing daytime out", 720, 1320, 360, false, false},
	}
	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			in, today := inAwayWindow(tc.dl, tc.start, tc.end)
			if in != tc.wantIn || today != tc.wantToday {
				t.Errorf("inAwayWindow(%d,%d,%d) = (%v,%v), want (%v,%v)",
					tc.dl, tc.start, tc.end, in, today, tc.wantIn, tc.wantToday)
			}
		})
	}
}

func TestEffectiveDeadline(t *testing.T) {
	utc := time.UTC
	day := func(h, m int) time.Time { return time.Date(2026, 6, 2, h, m, 0, 0, utc) }
	hour := time.Hour

	cases := []struct {
		name      string
		start     time.Time
		timeout   time.Duration
		awayStart int
		awayEnd   int
		want      time.Time
	}{
		{"no window", day(1, 0), hour, 0, 0, day(2, 0)},
		{"outside window", day(8, 0), hour, 0, 420, day(9, 0)},
		{"inside non-crossing pushed to end", day(1, 0), hour, 0, 420, day(7, 0)},
		{"inside non-crossing at boundary", day(2, 30), 3 * hour, 0, 420, day(7, 0)},
		{"crossing evening pushed to next day", day(22, 0), hour, 1320, 360, day(6, 0).AddDate(0, 0, 1)},
		{"crossing morning pushed to today end", day(2, 0), hour, 1320, 360, day(6, 0)},
		{"crossing daytime untouched", day(11, 0), hour, 1320, 360, day(12, 0)},
	}
	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			got := effectiveDeadline(tc.start, tc.timeout, utc, tc.awayStart, tc.awayEnd)
			if !got.Equal(tc.want) {
				t.Errorf("effectiveDeadline = %s, want %s", got, tc.want)
			}
		})
	}
}

func TestMinutesOfDay(t *testing.T) {
	got := minutesOfDay(time.Date(1, 1, 1, 7, 30, 0, 0, time.UTC))
	if got != 450 {
		t.Errorf("minutesOfDay(07:30) = %d, want 450", got)
	}
}

func TestLoadLocationFallsBackToUTC(t *testing.T) {
	if loadLocation("") != time.UTC {
		t.Error("empty zone must fall back to UTC")
	}
	if loadLocation("Totally/Bogus") != time.UTC {
		t.Error("unknown zone must fall back to UTC")
	}
}
