package publishschedule

import (
	"testing"
	"time"
)

func TestBuildScheduleUsesTimezoneSlotsAndReturnsUTCNano(t *testing.T) {
	loc, err := time.LoadLocation("Asia/Taipei")
	if err != nil {
		t.Fatal(err)
	}
	start := time.Date(2026, 6, 29, 9, 0, 0, 0, loc) // Monday
	got := BuildSchedule(start.UTC().UnixNano(), "Asia/Taipei", []Slot{
		{Weekday: 1, Time: "09:30"},
		{Weekday: 3, Time: "12:30"},
	}, 2)
	if len(got) != 2 {
		t.Fatalf("expected 2 timestamps, got %d", len(got))
	}
	first := time.Unix(0, got[0]).In(loc)
	if first.Weekday() != time.Monday || first.Hour() != 9 || first.Minute() != 30 {
		t.Fatalf("unexpected first slot: %s", first)
	}
	second := time.Unix(0, got[1]).In(loc)
	if second.Weekday() != time.Wednesday || second.Hour() != 12 || second.Minute() != 30 {
		t.Fatalf("unexpected second slot: %s", second)
	}
}

func TestBuildScheduleFallsBackToIntervalsWhenNoSlots(t *testing.T) {
	start := time.Date(2026, 6, 29, 9, 0, 0, 0, time.UTC)
	got := BuildSchedule(start.UnixNano(), "UTC", nil, 2)
	if len(got) != 2 {
		t.Fatalf("expected 2 timestamps, got %d", len(got))
	}
	if got[1]-got[0] != int64(2*time.Hour) {
		t.Fatalf("expected 2h interval, got %s", time.Duration(got[1]-got[0]))
	}
}
