package mysql

import (
	"testing"

	domain "hyapp/services/activity-service/internal/domain/wheel"
)

func TestAggregateWheelDrawResultsKeepsBatchRewardDetails(t *testing.T) {
	results := []domain.DrawResult{
		{DrawID: "draw-1", CommandID: "cmd#000001", WheelID: "luxury", SelectedTierID: "gift-a", RewardType: domain.RewardTypeGift, RewardID: "gift-a", RewardCount: 1, RTPValueCoins: 100, RewardStatus: domain.StatusPending, MetadataJSON: `{"resource_id":331}`},
		{DrawID: "draw-2", CommandID: "cmd#000002", WheelID: "luxury", SelectedTierID: "gift-b", RewardType: domain.RewardTypeGift, RewardID: "gift-b", RewardCount: 1, RTPValueCoins: 200, RewardStatus: domain.StatusGranted, MetadataJSON: `{"resource_id":332}`},
		{DrawID: "draw-3", CommandID: "cmd#000003", WheelID: "luxury", SelectedTierID: "gift-a", RewardType: domain.RewardTypeGift, RewardID: "gift-a", RewardCount: 1, RTPValueCoins: 100, RewardStatus: domain.StatusPending, MetadataJSON: `{"resource_id":331}`},
	}

	aggregate := aggregateWheelDrawResults(domain.DrawCommand{CommandID: "cmd", DrawCount: 3}, results)

	if aggregate.CommandID != "cmd" || aggregate.SelectedTierID != "batch" || aggregate.RewardType != "batch" {
		t.Fatalf("batch aggregate identity mismatch: %+v", aggregate)
	}
	if len(aggregate.DrawIDs) != 1 || aggregate.DrawIDs[0] != aggregate.DrawID || len(aggregate.Rewards) != 3 {
		t.Fatalf("batch aggregate must keep one persisted draw id and all reward details: %+v", aggregate)
	}
	if aggregate.RewardStatus != domain.StatusPending || aggregate.RTPValueCoins != 400 {
		t.Fatalf("batch aggregate status/value mismatch: %+v", aggregate)
	}
	if aggregate.Rewards[0].RewardID != "gift-a" || aggregate.Rewards[1].RewardID != "gift-b" || aggregate.Rewards[2].MetadataJSON != `{"resource_id":331}` {
		t.Fatalf("batch reward details changed order/content: %+v", aggregate.Rewards)
	}
}
