package app

import (
	"context"
	"strings"
	"testing"

	"hyapp/pkg/outboxreplay"
	"hyapp/services/wallet-service/internal/config"
)

func TestNormalizeWalletReplayEventTypes(t *testing.T) {
	eventTypes, err := NormalizeWalletReplayEventTypes([]string{" GiftConfigChanged ", "WalletBalanceChanged", "GiftConfigChanged"})
	if err != nil {
		t.Fatalf("NormalizeWalletReplayEventTypes failed: %v", err)
	}
	if len(eventTypes) != 2 || eventTypes[0] != "GiftConfigChanged" || eventTypes[1] != "WalletBalanceChanged" {
		t.Fatalf("normalized event types mismatch: %v", eventTypes)
	}
	if _, err := NormalizeWalletReplayEventTypes([]string{"bad || tag"}); err == nil {
		t.Fatal("expected invalid RocketMQ tag rejection")
	}
}

func TestRejectRealtimeReplayEventTypes(t *testing.T) {
	if err := rejectRealtimeReplayEventTypes([]string{"GiftConfigChanged"}, []string{"WalletRedPacketCreated"}); err != nil {
		t.Fatalf("normal event rejected: %v", err)
	}
	if err := rejectRealtimeReplayEventTypes([]string{"WalletRedPacketCreated"}, []string{"WalletRedPacketCreated"}); err == nil {
		t.Fatal("expected realtime lane rejection")
	}
}

func TestWalletReplayRejectsRealtimeTypeWhenLaneIsDisabled(t *testing.T) {
	cfg := config.Default()
	cfg.RealtimeOutboxWorker.Enabled = false
	cfg.RocketMQ.RealtimeOutbox.Enabled = false
	cfg.RealtimeOutboxWorker.EventTypes = []string{"WalletRedPacketCreated"}
	_, err := ReplayWalletOutboxOnce(context.Background(), cfg, outboxreplay.Scope{AppCode: "huwaa", Limit: 1}, []string{"WalletRedPacketCreated"})
	if err == nil || !strings.Contains(err.Error(), "realtime outbox lane") {
		t.Fatalf("disabled realtime lane must retain event ownership, err=%v", err)
	}
}
