package adminkindboxhandler

import (
	"net/http"

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

// Get godoc
// @Summary      Get a specific kind box by admin
// @Description  This endpoint retrieves a specific kind box by admin
// @Tags         Admins KindBoxes
// @Accept       json
// @Produce      json
// @Param 		 id path int true "Kind box ID"
// @Success      200  {object}  KindBoxAggregatedResponse
// @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/kindboxes/{id} [get].
func (h Handler) Get(c echo.Context) error {
	var req param.KindBoxGetRequest
	if bErr := c.Bind(&req); bErr != nil {
		return echo.NewHTTPError(http.StatusBadRequest)
	}

	kindBox, sErr := h.adminKindBoxSvc.Get(c.Request().Context(), req)
	if sErr != nil {
		msg, code := httpmsg.Error(sErr)
		if kindBox.FieldErrors != nil {
			return c.JSON(code, echo.Map{
				"message": msg,
				"errors":  kindBox.FieldErrors,
			})
		}

		return echo.NewHTTPError(code, msg)
	}

	benefactor, bErr := h.adminBenefactorAggSvc.GetByID(c.Request().Context(), kindBox.Data.BenefactorID)
	if bErr != nil {
		msg, code := httpmsg.Error(bErr)

		return echo.NewHTTPError(code, msg)
	}

	deliverAddress, dAErr := h.addressAggSvc.GetAggregatedByID(c.Request().Context(), kindBox.Data.DeliverAddressID)
	if dAErr != nil {
		msg, code := httpmsg.Error(dAErr)

		return echo.NewHTTPError(code, msg)
	}
	returnAddress, rAErr := h.addressAggSvc.GetAggregatedByID(c.Request().Context(), kindBox.Data.ReturnAddressID)
	if rAErr != nil {
		msg, code := httpmsg.Error(rAErr)

		return echo.NewHTTPError(code, msg)
	}

	deliverReferTime, dRErr := h.referTimeAggSvc.GetReferTimeByID(c.Request().Context(), kindBox.Data.DeliverReferTimeID)
	if dRErr != nil {
		msg, code := httpmsg.Error(dRErr)

		return echo.NewHTTPError(code, msg)
	}

	returnReferTime, rRErr := h.referTimeAggSvc.GetReferTimeByID(c.Request().Context(), kindBox.Data.ReturnReferTimeID)
	if rRErr != nil {
		msg, code := httpmsg.Error(rRErr)

		return echo.NewHTTPError(code, msg)
	}

	resp := KindBoxAggregatedResponse{
		ID:                 kindBox.Data.ID,
		KindBoxReqID:       kindBox.Data.KindBoxReqID,
		BenefactorID:       kindBox.Data.BenefactorID,
		KindBoxType:        kindBox.Data.KindBoxType,
		Amount:             kindBox.Data.Amount,
		SerialNumber:       kindBox.Data.SerialNumber,
		Status:             kindBox.Data.Status,
		DeliverReferTimeID: kindBox.Data.DeliverReferTimeID,
		DeliverReferDate:   kindBox.Data.DeliverReferDate,
		DeliverAddressID:   kindBox.Data.DeliverAddressID,
		SenderAgentID:      kindBox.Data.SenderAgentID,
		DeliveredAt:        kindBox.Data.DeliveredAt,
		ReturnReferTimeID:  kindBox.Data.ReturnReferTimeID,
		ReturnReferDate:    kindBox.Data.ReturnReferDate,
		ReturnAddressID:    kindBox.Data.ReturnAddressID,
		ReceiverAgentID:    kindBox.Data.ReceiverAgentID,
		ReturnedAt:         kindBox.Data.ReturnedAt,
		Benefactor:         benefactor,
		DeliverAddress:     deliverAddress,
		ReturnAddress:      returnAddress,
		DeliverReferTime:   deliverReferTime,
		ReturnReferTime:    returnReferTime,
	}

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