package services

import (
	"sync"

	"github.com/fiskerinc/cloud-services/services/optimus/utils"

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

var filters *utils.VehicleMessageFilters
var filtersOnce sync.Once

// GetKafkaConsumer returns singleton instance of kafka consumer
func GetVehicleMessageFilters() *utils.VehicleMessageFilters {
	filtersOnce.Do(func() {
		ResetVehicleMessageFilters()
	})

	return filters
}

func ResetVehicleMessageFilters() {
	filters = utils.NewVehicleMessageFilters()

	c, err := GetClickhouseClient()
	if err != nil {
		logger.Error().Err(err).Send()
		return
	}

	defaults, err := c.RetrieveDefaultFilters()
	if err != nil {
		logger.Error().Err(err).Send()
		return
	}

	filters.SetDefaultFilters(defaults)
}
