package adminkindboxreqhandler

import (
	"net/http"

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

// AddKindBoxReq godoc
// @Summary      Add a new kind box request for a benefactor by admin
// @Tags         Admins KindBoxReqs
// @Accept       json
// @Produce      json
// @Param        Request body   param.KindBoxReqAddRequest true "New kind box request details"
// @Success      200  {object}  param.KindBoxReqAddResponse
// @Failure      400  {string}  "Bad request"
// @Security     AuthBearerAdmin
// @Router       /admins/kindboxreqs [post].
func (h Handler) AddKindBoxReq(c echo.Context) error {
	req := param.KindBoxReqAddRequest{}
	if err := c.Bind(&req); err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, errmsg.ErrBadRequest)
	}

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

		return echo.NewHTTPError(code, msg)
	}

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