package health

import (
	"context"
)

type GetKafkaConsumerFunc func() (KafkaConnCheckInterface, error)

func NewKafkaCheck(fn GetKafkaConsumerFunc) CheckFunc {
	return func(ctx context.Context) error {
		conn, err := fn()
		if err != nil {
			return err
		}

		return conn.Check(ctx)
	}
}

type KafkaConnCheckInterface interface {
	Check(ctx context.Context) error
}

type GetMultiKafkaConsumerFunc func() ([]KafkaConnCheckInterface, error)

func NewKafkaMultiCheck(fn GetMultiKafkaConsumerFunc) CheckFunc {
	return func(ctx context.Context) error {
		conns, err := fn()
		if err != nil {
			return err
		}

		for _, connection := range conns {
			err = connection.Check(ctx)
			if err != nil {
				return err
			}
		}
		return err
	}
}
