package benefactorkindboxhandler

import (
	"net/http"

	param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box"
	"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"
)

// GetAll godoc
// @Summary 	 Get all KindBoxes by benefactor
// @Description  Retrieves a list of all KindBoxes with filtering, sorting, and pagination options
// @Tags         Benefactors KindBoxes
// @Accept       json
// @Produce      json
// @Param        filter_id query int false "Filter by ID"
// @Param        filter_kind_box_req_id query int false "Filter by KindBox request ID"
// @Param 		 filter_kind_box_type query string false "Filter by KindBox type" Enums(on-table,cylindrical,stand-up)
// @Param        filter_amount query int false "Filter by amount"
// @Param        filter_serial_number query string false "Filter by serial number"
// @Param        filter_status query string false "Filter by status" Enums(delivered,ready-to-return,assigned-receiver-agent,returned,enumerated)
// @Param        filter_delivered_at query string false "Filter by delivered at" Format(date-time)
// @Param        filter_return_refer_time_id query int false "Filter by return refer time ID"
// @Param        filter_return_refer_date query string false "Filter by return refer date" Format(date)
// @Param        filter_return_address_id query int false "Filter by return address ID"
// @Param        filter_receiver_agent_id query int false "Filter by receiver agent ID"
// @Param        filter_returned_at query string false "Filter by returned at" Format(date-time)
// @Param        filter_sender_agent_id query int false "Filter by sender agent 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        filter_deliver_refer_time_id query int false "Filter by deliver refer time 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,kind_box_req_id,benefactor_id,kind_box_type,amount,serial_number,status,delivered_at,return_refer_time_id,return_refer_date,return_address_id,receiver_agent_id,returned_at,sender_agent_id,deliver_refer_date,deliver_address_id,deliver_refer_time_id)
// @Param        sort_direction query string false "Sort order" Enums(asc,desc)
// @Success      200  {object}  param.KindBoxGetAllResponse
// @Failure      400  {string}  "Bad request"
// @Security 	 AuthBearerBenefactor
// @Router       /benefactors/kindboxes [get].
func (h Handler) GetAll(c echo.Context) error {
	var req param.KindBoxGetAllRequest

	if bErr := c.Bind(&req); bErr != nil {
		return echo.NewHTTPError(http.StatusBadRequest)
	}

	req.Filter = queryparam.GetFilterParams(c)
	req.BenefactorId = claim.GetClaimsFromEchoContext(c).UserID

	resp, sErr := h.benefactorKindBoxSvc.GetAll(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.StatusOK, resp)
}
