package services

import (
	"sync"

	"github.com/fiskerinc/cloud-services/pkg/kafka"
	"github.com/fiskerinc/cloud-services/pkg/logger"
)

const serviceName = "depot"
const oldServiceName = "old-depot"

var consumer kafka.ConsumerInterface
var oldConsumer kafka.ConsumerInterface
var consumerOnce sync.Once

// GetKafkaConsumer returns singleton instance of kafka consumer
func GetKafkaConsumer() (kafka.ConsumerInterface, kafka.ConsumerInterface, error) {
	var err error

	consumerOnce.Do(func() {
		consumer, err = kafka.NewConsumer(serviceName)
		if err != nil {
			logger.Error().Err(err).Send()
		}
		oldConsumer, err = kafka.NewConsumer(oldServiceName)
		if err != nil {
			logger.Error().Err(err).Send()
		}
	})
	if err != nil {
		return consumer, oldConsumer, err
	}

	return consumer, oldConsumer, nil
}
