package http

import (
	"testing"
	"time"
)

func TestScheduleOverdue(t *testing.T) {
	mustParse := func(s string) time.Time {
		t.Helper()
		v, err := time.Parse(time.RFC3339, s)
		if err != nil {
			t.Fatalf("parse %q: %v", s, err)
		}
		return v
	}
	daily := "0 2 * * *" // 02:00 every day

	cases := []struct {
		name       string
		cron       string
		lastBackup *time.Time
		now        time.Time
		want       bool
	}{
		{name: "never backed up is overdue", cron: daily, lastBackup: nil, now: mustParse("2026-06-15T09:00:00Z"), want: true},
		{name: "missed last nights window", cron: daily, lastBackup: ptrTime(mustParse("2026-06-13T02:05:00Z")), now: mustParse("2026-06-15T09:00:00Z"), want: true},
		{name: "backed up after the most recent window", cron: daily, lastBackup: ptrTime(mustParse("2026-06-15T02:05:00Z")), now: mustParse("2026-06-15T09:00:00Z"), want: false},
		{name: "unparseable cron is never overdue", cron: "not a cron", lastBackup: nil, now: mustParse("2026-06-15T09:00:00Z"), want: false},
	}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := scheduleOverdue(c.cron, c.lastBackup, c.now)
			if got != c.want {
				t.Fatalf("scheduleOverdue(%q, %v, %v) = %v, want %v", c.cron, c.lastBackup, c.now, got, c.want)
			}
		})
	}
}

func ptrTime(t time.Time) *time.Time { return &t }
