package agentkindboxreqhandler

import (
	"net/http"

	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"
	"github.com/labstack/echo/v4"
)

// GetAwaitingDelivery godoc
// @Summary      Get a kind box reqs that is awaiting delivery by agent
// @Tags         Agents KindBoxReqs
// @Accept       json
// @Produce      json
// @Param        id path int true "KindBoxReq ID"
// @Success      200  {object}  param.DeliveryAwaitingGetResponse
// @Failure      400  {string}  "Bad request"
// @Security 	 AuthBearerAdmin
// @Router       /agents/kindboxreqs/awaiting-delivery/{id} [get].
func (h Handler) GetAwaitingDelivery(c echo.Context) error {
	var req param.DeliveryAwaitingGetRequest
	if bErr := c.Bind(&req); bErr != nil {
		return echo.NewHTTPError(http.StatusBadRequest)
	}

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

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