package adminbenefactorhandler

import (
	"net/http"

	param "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor"
	httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
	queryparam "git.gocasts.ir/ebhomengo/niki/pkg/query_param"
	"github.com/labstack/echo/v4"
)

// GetAllBenefactor godoc
// @Summary 	 Get all benefactors by admin
// @Tags         Admins Benefactors
// @Accept       json
// @Produce      json
// @Param        search query string false "Search by id, phone_number, concat(first_name, last_name) benefactor"
// @Param        filter_id query int false "Filter by ID"
// @Param        filter_first_name query string false "Filter by first_name"
// @Param        filter_last_name query string false "Filter by last_name"
// @Param        filter_phone_number query string false "Filter by phone_number"
// @Param        filter_email query string false "Filter by email"
// @Param        filter_status query string false "Filter by status" Enums(active,inactive)
// @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,first_name,last_name,phone_number,email,status,created_at)
// @Param        sort_direction query string false "Sort order" Enums(asc,desc)
// @Success      200  {object}  param.BenefactorGetAllResponse
// @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       /admins/benefactors [get].
func (h Handler) GetAllBenefactor(c echo.Context) error {
	var req param.BenefactorGetAllRequest

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

	req.Filter = queryparam.GetFilterParams(c)

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