package app

import (
	"testing"

	"hyapp/pkg/walletmq"
)

func TestRedPacketEventFromWalletMessageKeepsDisplayPayloadFacts(t *testing.T) {
	body, err := walletmq.EncodeWalletOutboxMessage(walletmq.WalletOutboxMessage{
		AppCode:       "lalu",
		EventID:       "wallet-red-packet-created-1",
		EventType:     "WalletRedPacketCreated",
		TransactionID: "tx-red-packet-1",
		CommandID:     "cmd-red-packet-1",
		UserID:        10001,
		PayloadJSON: `{
			"packet_id":"VRRP2026052712340001",
			"packet_type":"delayed",
			"sender_user_id":10001,
			"room_id":"9001",
			"region_id":310,
			"room_locked":true,
			"owner_user_id":323411852102995968,
			"total_amount":15000,
			"packet_count":5,
			"remaining_amount":15000,
			"remaining_count":5,
			"status":"waiting_open",
			"open_at_ms":1780000000000,
			"expires_at_ms":1780086400000
		}`,
		OccurredAtMS: 1_779_999_700_000,
	})
	if err != nil {
		t.Fatalf("encode wallet message failed: %v", err)
	}

	event, ok, err := redPacketEventFromWalletMessage(body)
	if err != nil || !ok {
		t.Fatalf("decode red packet event failed: ok=%t err=%v", ok, err)
	}
	if event.PacketID != "VRRP2026052712340001" || event.PacketType != "delayed" || event.RoomID != "9001" || event.RegionID != 310 || !event.RoomLocked || event.OwnerUserID != 323411852102995968 {
		t.Fatalf("red packet identity fields mismatch: %+v", event)
	}
	if event.SenderUserID != 10001 || event.TotalAmount != 15000 || event.TotalCount != 5 || event.RemainingAmount != 15000 || event.RemainingCount != 5 {
		t.Fatalf("red packet display facts mismatch: %+v", event)
	}
	if event.Status != "waiting_open" || event.OpenAtMS != 1_780_000_000_000 || event.ExpiresAtMS != 1_780_086_400_000 || event.CreatedAtMS != 1_779_999_700_000 {
		t.Fatalf("red packet timing/status fields mismatch: %+v", event)
	}
}
