package main

import (
	"testing"
	"time"
)

func berlin(t *testing.T) *time.Location {
	t.Helper()
	loc, err := time.LoadLocation("Europe/Berlin")
	if err != nil {
		t.Fatal(err)
	}
	return loc
}

func TestParseOpens(t *testing.T) {
	for in, want := range map[string]Schedule{
		"Fri 16:00":    {time.Friday, 16, 0},
		"mon 09:30":    {time.Monday, 9, 30},
		"Sunday 23:59": {time.Sunday, 23, 59},
	} {
		if got, err := parseOpens(in); err != nil || got != want {
			t.Errorf("parseOpens(%q) = %v, %v; want %v", in, got, err, want)
		}
	}
	for _, in := range []string{"", "Fri", "Fr 16:00", "Fry 16:00", "Fri 24:00", "Fri 16:0", "Fri 16:00 x", "16:00"} {
		if _, err := parseOpens(in); err == nil {
			t.Errorf("parseOpens(%q) accepted", in)
		}
	}
}

func TestOpeningAcrossDST(t *testing.T) {
	loc := berlin(t)
	s := Schedule{time.Friday, 16, 0}
	// Clocks go back on Sunday 25 October 2026 and forward on Sunday 29 March 2026.
	for _, c := range []struct {
		from, want time.Time
		gap        time.Duration
	}{
		{time.Date(2026, 10, 23, 16, 0, 0, 0, loc), time.Date(2026, 10, 30, 16, 0, 0, 0, loc), 169 * time.Hour},
		{time.Date(2026, 3, 27, 16, 0, 0, 0, loc), time.Date(2026, 4, 3, 16, 0, 0, 0, loc), 167 * time.Hour},
	} {
		got := s.next(c.from)
		if !got.Equal(c.want) || got.Hour() != 16 || got.Sub(c.from) != c.gap {
			t.Errorf("next(%v) = %v, want %v (%v later)", c.from, got, c.want, c.gap)
		}
		if p := s.prev(got.Add(time.Minute)); !p.Equal(c.want) {
			t.Errorf("prev = %v, want %v", p, c.want)
		}
	}
}

func TestBoxKeyAtOpening(t *testing.T) {
	loc := berlin(t)
	s := Schedule{time.Friday, 16, 0}
	o := time.Date(2026, 10, 16, 16, 0, 0, 0, loc)
	if k := keyOf(s.next(o.Add(-time.Nanosecond))); k != "2026-10-16" {
		t.Errorf("just before the opening: key %s", k)
	}
	if k := keyOf(s.next(o)); k != "2026-10-23" {
		t.Errorf("at the opening instant: key %s, want the next box", k)
	}
	if !s.prev(o).Equal(o) {
		t.Errorf("prev(o) = %v", s.prev(o))
	}
}

func TestWords(t *testing.T) {
	for got, want := range map[string]string{
		Schedule{time.Friday, 16, 0}.plate(): "Opens Fridays 16:00",
		Schedule{time.Monday, 9, 30}.plate(): "Opens Mondays 09:30",
		countLine(1):                         "1 note inside.",
		countLine(0):                         "0 notes inside.",
		countLine(7):                         "7 notes inside.",
		plural(1, "earlier opening", "x"):    "1 earlier opening",
		spell(24 * time.Hour):                "24 hours",
		spell(2 * time.Minute):               "2 minutes",
		spell(90 * time.Minute):              "1 hour 30 minutes",
	} {
		if got != want {
			t.Errorf("got %q, want %q", got, want)
		}
	}
}

func TestStartURL(t *testing.T) {
	detect := func() string { return "192.168.1.20" }
	for addr, want := range map[string]string{
		":8080":            "http://192.168.1.20:8080",
		"0.0.0.0:8080":     "http://192.168.1.20:8080",
		"[::]:8080":        "http://192.168.1.20:8080",
		"10.0.0.5:8080":    "http://10.0.0.5:8080",
		"box.example:8080": "http://box.example:8080",
	} {
		if got := startURL(addr, 8080, detect); got != want {
			t.Errorf("startURL(%q) = %q, want %q", addr, got, want)
		}
	}
}
