package activityapi

import (
	"context"
	"testing"
	"time"

	roomv1 "hyapp.local/api/proto/room/v1"
)

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

	todayStart, todayEnd := userLeaderboardPeriodWindow(leaderboardPeriodToday, 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, _ := userLeaderboardPeriodWindow(leaderboardPeriodWeek, now)
	if weekStart != time.Date(2026, 5, 18, 0, 0, 0, 0, time.UTC) {
		t.Fatalf("week start = %s", weekStart)
	}

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

func TestNormalizeUserLeaderboardTypeAndPeriod(t *testing.T) {
	if normalizeUserLeaderboardType("receiver") != leaderboardTypeReceived {
		t.Fatal("receiver should normalize to received")
	}
	if normalizeUserLeaderboardType("rooms") != leaderboardTypeRoom {
		t.Fatal("rooms should normalize to room")
	}
	if normalizeUserLeaderboardPeriod("monthly") != leaderboardPeriodMonth {
		t.Fatal("monthly should normalize to month")
	}
}

func TestRoomLeaderboardRoomFromSnapshotIncludesAvatar(t *testing.T) {
	room := roomLeaderboardRoomFromSnapshot("room_1", &roomv1.RoomSnapshot{
		RoomId:      "room_1",
		RoomShortId: "1001",
		RoomExt: map[string]string{
			"title":     "Music Room",
			"cover_url": "https://cdn.example/room.png",
		},
	})

	if room.RoomID != "room_1" || room.RoomShortID != "1001" || room.Title != "Music Room" {
		t.Fatalf("room profile mismatch: %+v", room)
	}
	if room.CoverURL != "https://cdn.example/room.png" || room.RoomAvatar != room.CoverURL {
		t.Fatalf("room avatar fields mismatch: %+v", room)
	}
}

func TestUserLeaderboardItemsFromRoomGiftLeaderboardUsesRoomCard(t *testing.T) {
	items := userLeaderboardItemsFromRoomGiftLeaderboard([]*roomv1.RoomGiftLeaderboardItem{{
		Rank:      1,
		RoomId:    "room_1",
		CoinSpent: 900,
		Room: &roomv1.RoomListItem{
			RoomId:      "room_1",
			RoomShortId: "1001",
			Title:       "Music Room",
			CoverUrl:    "https://cdn.example/room.png",
		},
	}})

	if len(items) != 1 || items[0].RoomID != "room_1" || items[0].GiftValue != 900 {
		t.Fatalf("room leaderboard item mismatch: %+v", items)
	}
	if items[0].Room == nil || items[0].Room.RoomShortID != "1001" || items[0].Room.RoomAvatar != "https://cdn.example/room.png" {
		t.Fatalf("room leaderboard card mismatch: %+v", items[0].Room)
	}
}

type fakeUserLeaderboardRoomQueryClient struct {
	snapshotResp *roomv1.GetRoomSnapshotResponse
	snapshotErr  error
}

func (f *fakeUserLeaderboardRoomQueryClient) ListRooms(context.Context, *roomv1.ListRoomsRequest) (*roomv1.ListRoomsResponse, error) {
	return &roomv1.ListRoomsResponse{}, nil
}

func (f *fakeUserLeaderboardRoomQueryClient) ListRoomFeeds(context.Context, *roomv1.ListRoomFeedsRequest) (*roomv1.ListRoomsResponse, error) {
	return &roomv1.ListRoomsResponse{}, nil
}

func (f *fakeUserLeaderboardRoomQueryClient) ListRoomGiftLeaderboard(context.Context, *roomv1.ListRoomGiftLeaderboardRequest) (*roomv1.ListRoomGiftLeaderboardResponse, error) {
	return &roomv1.ListRoomGiftLeaderboardResponse{}, nil
}

func (f *fakeUserLeaderboardRoomQueryClient) GetMyRoom(context.Context, *roomv1.GetMyRoomRequest) (*roomv1.GetMyRoomResponse, error) {
	return &roomv1.GetMyRoomResponse{}, nil
}

func (f *fakeUserLeaderboardRoomQueryClient) GetCurrentRoom(context.Context, *roomv1.GetCurrentRoomRequest) (*roomv1.GetCurrentRoomResponse, error) {
	return &roomv1.GetCurrentRoomResponse{}, nil
}

func (f *fakeUserLeaderboardRoomQueryClient) GetRoomSnapshot(context.Context, *roomv1.GetRoomSnapshotRequest) (*roomv1.GetRoomSnapshotResponse, error) {
	if f.snapshotErr != nil {
		return nil, f.snapshotErr
	}
	if f.snapshotResp != nil {
		return f.snapshotResp, nil
	}
	return &roomv1.GetRoomSnapshotResponse{}, nil
}

func (f *fakeUserLeaderboardRoomQueryClient) GetRoomRocket(context.Context, *roomv1.GetRoomRocketRequest) (*roomv1.GetRoomRocketResponse, error) {
	return &roomv1.GetRoomRocketResponse{}, nil
}

func (f *fakeUserLeaderboardRoomQueryClient) ListRoomBackgrounds(context.Context, *roomv1.ListRoomBackgroundsRequest) (*roomv1.ListRoomBackgroundsResponse, error) {
	return &roomv1.ListRoomBackgroundsResponse{}, nil
}

func (f *fakeUserLeaderboardRoomQueryClient) ListRoomOnlineUsers(context.Context, *roomv1.ListRoomOnlineUsersRequest) (*roomv1.ListRoomOnlineUsersResponse, error) {
	return &roomv1.ListRoomOnlineUsersResponse{}, nil
}

func (f *fakeUserLeaderboardRoomQueryClient) ListRoomBannedUsers(context.Context, *roomv1.ListRoomBannedUsersRequest) (*roomv1.ListRoomBannedUsersResponse, error) {
	return &roomv1.ListRoomBannedUsersResponse{}, nil
}
