package adminkindboxhandler

import (
	"net/http"

	param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
	"git.gocasts.ir/ebhomengo/niki/pkg/claim"
	httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
	"github.com/labstack/echo/v4"
)

// GetAwaitingReturn godoc
// @Summary      Get a kind box that is awaiting return by agent
// @Tags         KindBox
// @Accept       json
// @Produce      json
// @Param        id path int true "KindBox ID"
// @Success      200  {object}  param.ReturnAwaitingGetResponse
// @Failure      400  {string}  "Bad Request"
// @Failure      401  {string}  "invalid or expired jwt"
// @Failure      403  {string}  "user not allowed"
// @Failure      422  {object}  httpmsg.ErrorResponse
// @Failure      404  {string}  "record not found"
// @Failure      500  {string}  "something went wrong"
// @Security 	 AuthBearerAdmin
// @Router       /admin/kindboxes/awaiting-return/{id} [get]
func (h Handler) GetAwaitingReturn(c echo.Context) error {
	var req param.ReturnAwaitingGetRequest
	if bErr := c.Bind(&req); bErr != nil {
		return echo.NewHTTPError(http.StatusBadRequest)
	}

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

	if fieldErrors, err := h.adminKindBoxVld.ValidateGetAwaitingReturnRequest(req); err != nil {
		msg, code := httpmsg.Error(err)

		return c.JSON(code, httpmsg.ErrorResponse{
			Message: msg,
			Errors:  fieldErrors,
		})
	}

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

		return echo.NewHTTPError(code, msg)
	}

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