package healthcheck

import (
	"net/http"
	"sync"

	"github.com/gin-gonic/gin"
)

type HealthCheck interface {
	SetStatus(status string)
	GetStatus() string
	HealthCheckHandler(c *gin.Context)
}

type healthCheck struct {
	status string
	sync   sync.Mutex
}

func New() HealthCheck {
	return &healthCheck{
		status: "ok",
	}
}

func (hc *healthCheck) SetStatus(status string) {
	hc.sync.Lock()
	defer hc.sync.Unlock()

	hc.status = status
}

func (hc *healthCheck) GetStatus() string {
	hc.sync.Lock()
	defer hc.sync.Unlock()

	return hc.status
}

func (hc *healthCheck) HealthCheckHandler(c *gin.Context) {
	switch hc.GetStatus() {
	case "nok":
		c.JSON(http.StatusServiceUnavailable, gin.H{
			"msg": hc.GetStatus(),
		})
	default:
		c.JSON(http.StatusOK, gin.H{
			"msg": hc.GetStatus(),
		})
	}
}
