package roommq

import (
	"testing"

	"google.golang.org/protobuf/proto"
	roomeventsv1 "hyapp.local/api/proto/events/room/v1"
)

func TestRoomOutboxMessageRoundTrip(t *testing.T) {
	body, err := proto.Marshal(&roomeventsv1.RoomRocketIgnited{RocketId: "rocket-1", Level: 2, LaunchAtMs: 12345})
	if err != nil {
		t.Fatalf("marshal body: %v", err)
	}
	envelope := &roomeventsv1.EventEnvelope{
		AppCode:      "default",
		EventId:      "evt-1",
		EventType:    "RoomRocketIgnited",
		RoomId:       "room-1",
		RoomVersion:  9,
		OccurredAtMs: 12300,
		Body:         body,
	}
	encoded, err := EncodeRoomOutboxMessage(envelope)
	if err != nil {
		t.Fatalf("encode: %v", err)
	}
	decoded, message, err := DecodeRoomOutboxMessage(encoded)
	if err != nil {
		t.Fatalf("decode: %v", err)
	}
	if message.MessageType != MessageTypeRoomOutboxEvent || decoded.GetEventId() != envelope.GetEventId() {
		t.Fatalf("unexpected decoded message: %#v %#v", message, decoded)
	}
}

func TestRoomRocketLaunchDueRoundTrip(t *testing.T) {
	encoded, err := EncodeRoomRocketLaunchDueMessage(RoomRocketLaunchDueMessage{
		AppCode:    "default",
		RoomID:     "room-1",
		RocketID:   "rocket-1",
		Level:      1,
		LaunchAtMS: 12345,
		ResetAtMS:  99999,
		CommandID:  "cmd_room_rocket_launch_rocket-1",
	})
	if err != nil {
		t.Fatalf("encode: %v", err)
	}
	decoded, err := DecodeRoomRocketLaunchDueMessage(encoded)
	if err != nil {
		t.Fatalf("decode: %v", err)
	}
	if decoded.MessageType != MessageTypeRoomRocketLaunchDue || decoded.RocketID != "rocket-1" || decoded.LaunchAtMS != 12345 {
		t.Fatalf("unexpected decoded message: %#v", decoded)
	}
}
