package agentkindboxreqhandler

import (
	"net/http"

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

// GetAllAwaitingDelivery godoc
// @Summary 	 Get all awaiting delivery KindBox requests
// @Description  Retrieves a list of all awaiting KindBox requests with filtering, sorting, and pagination options
// @Tags         Agents KindBoxReqs
// @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_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.DeliveryAwaitingGetAllResponse
// @Failure      400  {string}  "Bad request"
// @Security 	 AuthBearerAdmin
// @Router       /agents/kindboxreqs/awaiting-delivery [get].
func (h Handler) GetAllAwaitingDelivery(c echo.Context) error {
	var req param.DeliveryAwaitingGetAllRequest

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

	req.SenderAgentId = claim.GetClaimsFromEchoContext(c).UserID
	req.Status = entity.KindBoxReqAssignedSenderAgentStatus

	resp, sErr := h.agentKindBoxReqSvc.GetAllAwaitingDelivery(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)
}
