package integration

import (
	"context"
	"strings"

	roomeventsv1 "hyapp.local/api/proto/events/room/v1"
)

// eventTypeOutboxPublisher 只把明确需要可靠腾讯 IM 补偿的展示事件转交给下游。
// 现有房间事件大多已有 direct-IM lane，不能无差别接入否则会给旧客户端制造双消息。
type eventTypeOutboxPublisher struct {
	inner        OutboxPublisher
	allowedTypes map[string]struct{}
}

func NewEventTypeOutboxPublisher(inner OutboxPublisher, eventTypes ...string) OutboxPublisher {
	allowed := make(map[string]struct{}, len(eventTypes))
	for _, eventType := range eventTypes {
		if eventType = strings.TrimSpace(eventType); eventType != "" {
			allowed[eventType] = struct{}{}
		}
	}
	return &eventTypeOutboxPublisher{inner: inner, allowedTypes: allowed}
}

func (p *eventTypeOutboxPublisher) PublishOutboxEvent(ctx context.Context, envelope *roomeventsv1.EventEnvelope) error {
	if p == nil || p.inner == nil || envelope == nil {
		return nil
	}
	if _, allowed := p.allowedTypes[envelope.GetEventType()]; !allowed {
		return nil
	}
	return p.inner.PublishOutboxEvent(ctx, envelope)
}
