package admindashboard

import (
	"errors"
	"net/http"

	"hfb_sys/backend/pkg/response"

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

type Handler struct {
	service *Service
}

func NewHandler(service *Service) *Handler {
	return &Handler{service: service}
}

func (h *Handler) Summary(c *gin.Context) {
	item, err := h.service.Summary()
	if err != nil {
		writeDashboardError(c, err)
		return
	}
	response.OK(c, item)
}

func writeDashboardError(c *gin.Context, err error) {
	switch {
	case errors.Is(err, ErrDependencyUnavailable):
		response.ServiceUnavailable(c, "数据库未连接")
	default:
		response.Error(c, http.StatusInternalServerError, "internal_error", "后台仪表盘服务暂时不可用")
	}
}
