package controllers

import (
	"github.com/fiskerinc/cloud-services/services/jetfire/services"
	"time"

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

var mismatchTypeError = errors.New("mismatch type error")

func HealthCheck() {
	server := health.HealthCheckServer{}
	err := server.Serve([]health.Config{
		{
			Name:    "clickhouse",
			Check:   health.NewClickhouseCheck(getClickhouseConsumer),
			Timeout: time.Second * 1,
		},
	})
	if err != nil {
		logger.Error().Err(err).Send()
	}
}

func getKafkaConsumer() (health.KafkaConnCheckInterface, error) {
	client, err := services.GetKafkaConsumer()
	if err != nil {
		return nil, err
	}

	conn, ok := client.(health.KafkaConnCheckInterface)
	if !ok {
		return nil, errors.WithStack(mismatchTypeError)
	}

	return conn, nil
}

func getClickhouseConsumer() (health.ClickhouseConnCheckInterface, error) {
	client, err := services.GetClickhouseConnection()
	if err != nil {
		return nil, err
	}

	conn, ok := client.(health.ClickhouseConnCheckInterface)
	if !ok {
		return nil, errors.WithStack(mismatchTypeError)
	}

	return conn, nil
}
