package gamemq

import "testing"

func TestEncodeGameOutboxMessageAllowsZeroCoinGameOrder(t *testing.T) {
	message := GameOutboxMessage{
		AppCode:      "lalu",
		EventID:      "GameOrderSettled:gord_zero_credit",
		EventType:    EventTypeGameOrderSettled,
		OrderID:      "gord_zero_credit",
		UserID:       42,
		PlatformCode: "zeeone",
		GameID:       "1021",
		OpType:       "credit",
		CoinAmount:   0,
		OccurredAtMS: 1781199149295,
	}

	body, err := EncodeGameOutboxMessage(message)
	if err != nil {
		t.Fatalf("zero coin game order should be publishable: %v", err)
	}
	decoded, err := DecodeGameOutboxMessage(body)
	if err != nil {
		t.Fatalf("zero coin game order should be decodable: %v", err)
	}
	if decoded.CoinAmount != 0 || decoded.PayloadJSON != "{}" {
		t.Fatalf("decoded zero coin order mismatch: %+v", decoded)
	}
}

func TestEncodeGameOutboxMessageRejectsNegativeGameOrderAmount(t *testing.T) {
	_, err := EncodeGameOutboxMessage(GameOutboxMessage{
		AppCode:      "lalu",
		EventID:      "GameOrderSettled:gord_negative",
		EventType:    EventTypeGameOrderSettled,
		OrderID:      "gord_negative",
		UserID:       42,
		PlatformCode: "zeeone",
		GameID:       "1021",
		OpType:       "credit",
		CoinAmount:   -1,
		OccurredAtMS: 1781199149295,
	})
	if err == nil {
		t.Fatalf("negative game order amount should be rejected")
	}
}

func TestEncodeGameOutboxMessageAllowsSelfGameEventWithoutOrderFields(t *testing.T) {
	body, err := EncodeGameOutboxMessage(GameOutboxMessage{
		AppCode:      "lalu",
		EventID:      "SelfGameMatchSettled:match_1",
		EventType:    EventTypeSelfGameMatchSettled,
		UserID:       42,
		PlatformCode: "self",
		GameID:       "dice",
		PayloadJSON:  `{"match_id":"match_1"}`,
		OccurredAtMS: 1781199149295,
	})
	if err != nil {
		t.Fatalf("self game fact should not require order fields: %v", err)
	}
	if _, err := DecodeGameOutboxMessage(body); err != nil {
		t.Fatalf("self game fact should decode: %v", err)
	}
}
