package controllers

import (
	"otaupdate/services"
	"time"

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

func HealthCheck() {
	redis := health.NewRedisHealth(services.RedisClientPool())
	server := health.HealthCheckServer{}

	err := server.Serve([]health.Config{
		{
			Name:    "db",
			Check:   health.NewPostgresCheck(services.GetDB().GetDBClient().GetConn()),
			Timeout: time.Second * 1,
		},
		{
			Name:    "redis",
			Check:   redis.Check,
			Timeout: time.Second * 1,
			Info:    redis.RedisStatus,
		},
		{
			Name:    "mongodb",
			Check:   health.NewMongoDBCheck(getMongoClient),
			Timeout: time.Second * 1,
		},
	})
	if err != nil {
		logger.Error().Err(err).Send()
	}
}

func getMongoClient() (health.MongoConnCheckInterface, error) {
	client, err := services.GetMongoClient()
	if err != nil {
		return nil, err
	}

	conn := client.(health.MongoConnCheckInterface)
	return conn, nil
}
