// Package health provides health check endpoints for the operator
package health

import (
	"net/http"

	"github.com/pfeifferj/container-mom-go/pkg/interfaces"
	"sigs.k8s.io/controller-runtime/pkg/log"
)

// Handler implements basic health check endpoints for the operator
type Handler struct {
	Operator interfaces.OperatorInterface
}

// Healthz handles liveness probe requests
func (h *Handler) Healthz(w http.ResponseWriter, _ *http.Request) {
	log.Log.Info("handling health check")
	w.WriteHeader(http.StatusOK)
}

// Readyz handles readiness probe requests
func (h *Handler) Readyz(w http.ResponseWriter, r *http.Request) {
	logger := log.FromContext(r.Context())
	logger.Info("handling readiness check")

	// Check MongoDB connection
	if err := h.Operator.GetMongoDB().Ping(r.Context(), nil); err != nil {
		logger.Error(err, "MongoDB ping failed")
		http.Error(w, "MongoDB not ready", http.StatusServiceUnavailable)
		return
	}
	w.WriteHeader(http.StatusOK)
}
