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"
)

// SendOtp godoc
// @Summary      Send OTP to benefactor
// @Description  This endpoint sends an OTP to the benefactor's phone number for verification purposes.
// @Tags         Benefactor
// @Accept       json
// @Produce      json
// @Param        Request body   benefactoreparam.SendOtpRequest true "Send OTP request details"
// @Success      200  {object}  benefactoreparam.SendOtpResponse
// @Failure      400  {string}  "Bad request"
// @Router       /benefactor/send-otp [post]
func (h Handler) SendOtp(c echo.Context) error {
	var req benefactoreparam.SendOtpRequest

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

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

		return echo.NewHTTPError(code, msg)
	}

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