package benefactorkindboxreqhandler

import (
	param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
	"git.gocasts.ir/ebhomengo/niki/pkg/claim"
	httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
	queryparam "git.gocasts.ir/ebhomengo/niki/pkg/query_param"
	"github.com/labstack/echo/v4"
	"net/http"
)

// GetAll godoc
// @Summary 	 Get all KindBox requests
// @Description  Retrieves a list of all KindBox requests with filtering, sorting, and pagination options
// @Tags         KindBoxReq
// @Accept       json
// @Produce      json
// @Param        filter_id query int false "Filter by ID"
// @Param        filter_benefactor_id query int false "Filter by benefactor ID"
// @Param 		 filter_kind_box_type query entity.KindBoxType false "Filter by KindBox type" Format(enum)
// @Param        filter_status query string false "Filter by KindBoxReq Status" Enums(pending,accepted,assigned-sender-agent,rejected,delivered)
// @Param        filter_count_requested query int false "Filter by count requested"
// @Param        filter_count_accepted query int false "Filter by count accepted"
// @Param        filter_deliver_refer_time_id query int false "Filter by deliver refer time ID"
// @Param        filter_deliver_refer_date query string false "Filter by deliver refer date" Format(date)
// @Param        filter_deliver_address_id query int false "Filter by deliver address ID"
// @Param        page_number query int false "Page number"
// @Param        page_size query int false "Page size"
// @Param        sort_field query string false "Sort by field" Enums(id,benefactor_id,kind_box_type,count_requested,count_accepted,deliver_refer_time_id,deliver_refer_date,deliver_address_id)
// @Param        sort_direction query string false "Sort order" Enums(asc,desc)
// @Success      200  {object}  param.GetAllResponse
// @Failure      400  {string}  "Bad request"
// @Security 	 AuthBearerBenefactor
// @Router       /benefactor/kindboxreqs/ [get]
func (h Handler) GetAll(c echo.Context) error {
	var req param.GetAllRequest

	if bErr := c.Bind(&req); bErr != nil {

		return echo.NewHTTPError(http.StatusBadRequest)
	}

	req.Filter = queryparam.GetFilterParams(c)
	if fieldErrors, err := h.benefactorKindBoxReqVld.ValidateGetAll(req); err != nil {
		msg, code := httpmsg.Error(err)

		return c.JSON(code, echo.Map{
			"message": msg,
			"errors":  fieldErrors,
		})
	}
	req.Filter["benefactor_id"] = claim.GetClaimsFromEchoContext(c).UserID

	resp, sErr := h.benefactorKindBoxReqSvc.GetAll(c.Request().Context(), req)
	if sErr != nil {
		msg, code := httpmsg.Error(sErr)

		return echo.NewHTTPError(code, msg)
	}

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