package ping

import (
	"backend/internal/types"
	errs "backend/pkg/library/errors"
	"backend/pkg/library/errors/code"
	"net/http"

	"backend/internal/logic/ping"
	"backend/internal/svc"

	"github.com/zeromicro/go-zero/rest/httpx"
)

// PingHandler 系統健康檢查
func PingHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		l := ping.NewPingLogic(r.Context(), svcCtx)
		err := l.Ping()
		if err != nil {
			e := errs.FromError(err)
			httpx.WriteJsonCtx(r.Context(), w, e.HTTPStatus(), types.Resp{
				Code:    e.DisplayCode(),
				Message: e.Error(),
				Error:   e.Unwrap(),
			})
		} else {
			httpx.WriteJsonCtx(r.Context(), w, http.StatusOK, types.Resp{
				Code:    code.SUCCESSCode,
				Message: code.SUCCESSMessage,
			})
		}
	}
}
