package messagingimp

import (
	mb "github.com/tech/sendico/pkg/messaging/broker"
	me "github.com/tech/sendico/pkg/messaging/envelope"
	"github.com/tech/sendico/pkg/mlogger"
	"github.com/tech/sendico/pkg/mutil/mzap"
	"go.uber.org/zap"
)

type ChannelProducer struct {
	logger mlogger.Logger
	broker mb.Broker
}

func (p *ChannelProducer) SendMessage(envelope me.Envelope) error {
	// TODO: won't work with Kafka, need to serialize/deserialize
	if err := p.broker.Publish(envelope); err != nil {
		p.logger.Warn("Failed to publish message", zap.Error(err), mzap.Envelope(envelope))
		return err
	}
	return nil
}

func NewProducer(logger mlogger.Logger, broker mb.Broker) *ChannelProducer {
	return &ChannelProducer{logger: logger.Named("producer"), broker: broker}
}
