package agentkindboxhandler

import (
	"net/http"

	"git.gocasts.ir/ebhomengo/niki/entity"
	param "git.gocasts.ir/ebhomengo/niki/param/agent/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 awaiting return KindBoxes by agent
// @Description  Retrieves a list of all awaiting return KindBoxes for agent with filtering, sorting, and pagination options
// @Tags         Agents KindBoxes
// @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_type query entity.KindBoxType false "Filter by KindBox type" Format(enum)
// @Param        filter_serial_number query string false "Filter by serial number"
// @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        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,type,serial_number,return_refer_time_id,return_refer_date,return_address_id)
// @Param        sort_direction query string false "Sort order" Enums(asc,desc)
// @Success      200  {object}  param.GetAllResponse
// @Failure      400  {string}  "Bad Request"
// @Failure      401  {string}  "invalid or expired jwt"
// @Failure      403  {string}  "user not allowed"
// @Failure      422  {object}  httpmsg.ErrorResponse
// @Failure      500  {string}  "something went wrong"
// @Security 	 AuthBearerAdmin
// @Router       /agents/kindboxes [get].
func (h Handler) GetAll(c echo.Context) error {
	var req param.GetAllRequest

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

	req.Filter = queryparam.GetFilterParams(c)
	req.ReceiverAgentId = claim.GetClaimsFromEchoContext(c).UserID
	req.Status = entity.KindBoxAssignedReceiverAgentStatus

	resp, err := h.agentKindBoxSvc.GetAll(c.Request().Context(), req)
	if err != nil {
		msg, code := httpmsg.Error(err)
		if resp.FieldErrors != nil {
			return c.JSON(code, httpmsg.ErrorResponse{
				Message: msg,
				Errors:  resp.FieldErrors,
			})
		}

		return echo.NewHTTPError(code, msg)
	}

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