package userleaderboard

import (
	"testing"
	"time"
)

func TestQueryWindowUsesExistingUTCBoundaries(t *testing.T) {
	now := time.Date(2026, 5, 20, 15, 30, 0, 0, time.FixedZone("CST", 8*60*60))

	todayStart, todayEnd := QueryWindow(PeriodToday, now)
	if todayStart != time.Date(2026, 5, 20, 0, 0, 0, 0, time.UTC) {
		t.Fatalf("today start = %s", todayStart)
	}
	if !todayEnd.Equal(now.UTC()) {
		t.Fatalf("today end = %s", todayEnd)
	}

	weekStart, _ := QueryWindow(PeriodWeek, now)
	if weekStart != time.Date(2026, 5, 18, 0, 0, 0, 0, time.UTC) {
		t.Fatalf("week start = %s", weekStart)
	}

	monthStart, _ := QueryWindow(PeriodMonth, now)
	if monthStart != time.Date(2026, 5, 1, 0, 0, 0, 0, time.UTC) {
		t.Fatalf("month start = %s", monthStart)
	}
}

func TestNormalizeAliases(t *testing.T) {
	if NormalizeBoardType("receiver") != BoardReceived {
		t.Fatal("receiver should normalize to received")
	}
	if NormalizeBoardType("gift_sent") != BoardSent {
		t.Fatal("gift_sent should normalize to sent")
	}
	if NormalizePeriod("monthly") != PeriodMonth {
		t.Fatal("monthly should normalize to month")
	}
}

func TestStoreKeyPrefixAndBucketAreStable(t *testing.T) {
	store := NewStore(nil, "activity:user_leaderboard:")
	weekStart := time.Date(2026, 12, 28, 0, 0, 0, 0, time.UTC)

	if got := store.scoreKey(" HyApp_Prod ", BoardSent, PeriodWeek, weekStart); got != "activity:user_leaderboard:hyapp_prod:sent:week:202653:scores" {
		t.Fatalf("score key mismatch: %s", got)
	}
	if got := store.itemKey("hyapp_prod", BoardReceived, PeriodToday, time.Date(2026, 6, 26, 0, 0, 0, 0, time.UTC), "10001"); got != "activity:user_leaderboard:hyapp_prod:received:today:20260626:users:10001" {
		t.Fatalf("item key mismatch: %s", got)
	}
}
