package main

import (
	"testing"
	"time"
)

func TestGrantsAreTheHalvingSeriesThenBelowTheMinimum(t *testing.T) {
	want := []int64{900000, 450000, 225000, 112500, 56250, 28125, 14062}
	for i, w := range want {
		if g := Grant(i + 1); g != w || g <= MinGrant {
			t.Fatalf("grant %d = %d, want %d above the minimum", i+1, g, w)
		}
	}
	if g := Grant(8); g != 7031 || g > MinGrant {
		t.Fatalf("grant 8 = %d, want 7031 at or under the minimum", g)
	}
}

func TestNumberOfGrantsIsSevenComputedFromTheConstants(t *testing.T) {
	if n := MaxGrants(); n != 7 {
		t.Fatalf("MaxGrants = %d, want 7", n)
	}
}

func TestCumulativeTotalNeverReachesTheCeiling(t *testing.T) {
	for n := 1; n <= 200; n++ {
		if Total(n) >= Ceiling {
			t.Fatalf("total after %d grants = %d, not below %d", n, Total(n), Ceiling)
		}
	}
}

func TestSumOfAllGrantsAndShortfall(t *testing.T) {
	if s := SumAll(); s != 1_799_989 {
		t.Fatalf("SumAll = %d, want 1799989", s)
	}
	if s := Shortfall(); s != Ceiling-SumAll() || s != 11 {
		t.Fatalf("Shortfall = %d, want 11", s)
	}
}

func TestTotalAndRemainingAfterSeven(t *testing.T) {
	if Total(7) != 1_785_937 || Ceiling-Total(7) != 14_063 {
		t.Fatalf("after seven: total %d remaining %d", Total(7), Ceiling-Total(7))
	}
}

func TestWords(t *testing.T) {
	cases := map[int64]string{
		900000: "15 minutes", 450000: "7 minutes 30 seconds", 225000: "3 minutes 45 seconds",
		112500: "1 minute 52.5 seconds", 56250: "56.25 seconds", 28125: "28.125 seconds",
		14062: "14.062 seconds", 14063: "14.063 seconds", 7031: "7.031 seconds", 10000: "10 seconds",
		1000: "1 second", 60000: "1 minute", 1: "0.001 seconds", 0: "0 seconds",
		1785937: "29 minutes 45.937 seconds", 1800000: "30 minutes",
	}
	for in, want := range cases {
		if got := Words(in); got != want {
			t.Errorf("Words(%d) = %q, want %q", in, got, want)
		}
	}
}

func TestClockFormat(t *testing.T) {
	base := time.Date(2026, 10, 13, 23, 0, 0, 0, time.UTC)
	cases := map[int64]string{
		0: "23:00:00", 1687500: "23:28:07.5", 1743750: "23:29:03.75",
		1771875: "23:29:31.875", 1785937: "23:29:45.937",
	}
	for off, want := range cases {
		if got := Clock(base.Add(ms(off))); got != want {
			t.Errorf("Clock(+%d) = %q, want %q", off, got, want)
		}
	}
}
