package benefactorkindboxreqhandler

import (
	"net/http"

	param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
	errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
	httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
	claim "git.gocasts.ir/ebhomengo/niki/pkg/claim"
	echo "github.com/labstack/echo/v4"
)

// UpdateKindBoxReq godoc
// @Summary      update kindBoxReq 
// @Tags         KindBoxReq
// @Accept       json
// @Produce      json
// @Param 		 id path int true "KindBoxReq ID"
// @Param        Request body   param.KindBoxReqUpdateRequest true "Update kind box req fields"
// @Success      204  {object}  param.KindBoxReqUpdateResponse
// @Failure      400  {string}  "Bad request"
// @Security     AuthBearerBenefactor
// @Router       /benefactor/kindboxreqs/{id} [patch]
func (h Handler) Update(c echo.Context) error {
	
	var req param.KindBoxReqUpdateRequest
	
	if err := c.Bind(&req); err != nil {
		return c.JSON(http.StatusBadRequest, echo.Map{
			"message": errmsg.ErrBadRequest,
		})
	}

	if bErr := echo.PathParamsBinder(c).Uint("id", &req.KindBoxReqID).BindError(); bErr != nil {
		return echo.NewHTTPError(http.StatusBadRequest)
	}


	claims := claim.GetClaimsFromEchoContext(c)
	req.BenefactorID = claims.UserID

	if fieldErrors, err := h.benefactorKindBoxReqVld.ValidateUpdateRequest(req); err != nil {
		msg, code := httpmsg.Error(err)

		return c.JSON(code, echo.Map{
			"message": msg,
			"errors":  fieldErrors,
		})
	}

	resp, err := h.benefactorKindBoxReqSvc.UpdateKindBoxReq(c.Request().Context(), req)
	if err != nil {
		msg, code := httpmsg.Error(err)
		return echo.NewHTTPError(code, msg)
	}

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