package adminbenefactorhandler

import (
	"net/http"

	params "git.gocasts.ir/ebhomengo/niki/param"
	adminaddressparam "git.gocasts.ir/ebhomengo/niki/param/admin/address"
	param "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor"
	adminkindboxparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
	adminkindboxreqparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
	httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
	"github.com/labstack/echo/v4"
)

// GetBenefactor godoc
// @Summary      Get benefactor details by id
// @Description  This endpoint retrieves details for a specific benefactor.
// @Tags         Admins Benefactors
// @Accept       json
// @Produce      json
// @Param 		 id path int true "Benefactor ID"
// @Success      200  {object}  BenefactorAggregatedResponse
// @Failure      400  {string}  "Bad request"
// @Failure      401  {string}  "invalid or expired jwt"
// @Failure      403  {string}  "user not allowed"
// @Failure      404  {string}  "record not found"
// @Failure      500  {string}  "something went wrong"
// @Security 	 AuthBearerAdmin
// @Router       /admins/benefactors/{id} [get].
func (h Handler) GetBenefactor(c echo.Context) error {
	var req param.GetBenefactorByIDRequest
	if err := c.Bind(&req); err != nil {
		return echo.NewHTTPError(http.StatusBadRequest)
	}

	bnf, err := h.benefactorSvc.GetByID(c.Request().Context(), req)
	if err != nil {
		msg, code := httpmsg.Error(err)

		return echo.NewHTTPError(code, msg)
	}

	addresses, err := h.addressSvc.GetAll(c.Request().Context(), adminaddressparam.GetAllAddressesRequest{
		BenefactorID: bnf.Data.ID,
	})
	if err != nil {
		msg, code := httpmsg.Error(err)

		return echo.NewHTTPError(code, msg)
	}

	kindBoxes, err := h.kindBoxSvc.GetAll(c.Request().Context(), adminkindboxparam.KindBoxGetAllRequest{
		Pagination: params.PaginationRequest{
			PageSize:   50,
			PageNumber: 1,
		},
		Sort: params.SortRequest{
			Field:     "created_at",
			Direction: params.DescSortDirection,
		},
		Filter: map[string]any{
			"benefactor_id": bnf.Data.ID,
		},
	})
	if err != nil {
		msg, code := httpmsg.Error(err)

		return echo.NewHTTPError(code, msg)
	}

	kindBoxReqs, err := h.kindBoxReqSvc.GetAll(c.Request().Context(), adminkindboxreqparam.KindBoxReqGetAllRequest{
		Pagination: params.PaginationRequest{
			PageSize:   50,
			PageNumber: 1,
		},
		Sort: params.SortRequest{
			Field:     "created_at",
			Direction: params.DescSortDirection,
		},
		Filter: map[string]any{
			"benefactor_id": bnf.Data.ID,
		},
	})
	if err != nil {
		msg, code := httpmsg.Error(err)

		return echo.NewHTTPError(code, msg)
	}

	resp := BenefactorAggregatedResponse{
		ID:          bnf.Data.ID,
		FirstName:   bnf.Data.FirstName,
		LastName:    bnf.Data.LastName,
		PhoneNumber: bnf.Data.PhoneNumber,
		Description: bnf.Data.Description,
		Email:       bnf.Data.Email,
		Gender:      bnf.Data.Gender,
		BirthDate:   bnf.Data.BirthDate,
		Status:      bnf.Data.Status,
		Addresses:   addresses.Data,
		KindBoxes:   kindBoxes.Data,
		KindBoxReqs: kindBoxReqs.Data,
	}

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