package benefactorhandler

import (
	"net/http"

	benefactoreparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactore"
	httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
	"github.com/labstack/echo/v4"
)

// loginOrRegister godoc
// @Summary      Login or register a benefactor
// @Description  This endpoint is used to authenticate an existing benefactor account or register a new one.
// @Tags         Benefactor
// @Accept       json
// @Produce      json
// @Param        Request body   benefactoreparam.LoginOrRegisterRequest true "Login or register request details"
// @Success      200  {object}  benefactoreparam.LoginOrRegisterResponse
// @Failure      400  {string}  "Bad request"
// @Router       /benefactor/login-register [post]
func (h Handler) loginOrRegister(c echo.Context) error {
	var req benefactoreparam.LoginOrRegisterRequest

	if bErr := c.Bind(&req); bErr != nil {
		return echo.NewHTTPError(http.StatusBadRequest)
	}
	if fieldErrors, err := h.benefactorVld.ValidateLoginRegisterRequest(req); err != nil {
		msg, code := httpmsg.Error(err)

		return c.JSON(code, echo.Map{
			"message": msg,
			"errors":  fieldErrors,
		})
	}
	resp, sErr := h.benefactorSvc.LoginOrRegister(c.Request().Context(), req)
	if sErr != nil {
		msg, code := httpmsg.Error(sErr)

		return echo.NewHTTPError(code, msg)
	}

	return c.JSON(http.StatusOK, resp)
}
