package server

import (
	"context"
	"net/http"

	"github.com/gofiber/fiber/v2"
)

type HealthResponse struct {
	Status string `json:"status,omitempty"`
}

func (s *Server) HealthHandler(c *fiber.Ctx) error {
	// Only 404 indicate service as non-healthy
	err := s.GetDatabase().Ping(context.Background())
	if err != nil {
		// fmt.Println("db unavailable", err)
		return c.SendStatus(http.StatusNotFound)
	}

	err = s.GetLogger().Ping()
	if err != nil {
		// fmt.Println("logger unavailable", err)
		return c.SendStatus(http.StatusNotFound)
	}

	err = s.GetCache().Ping(context.Background()).Err()
	if err != nil {
		// fmt.Println("cache unavailable", err)
		return c.SendStatus(http.StatusNotFound)
	}

	return c.JSON(&HealthResponse{
		Status: "OK",
	})
}
