package adminseedhandler

import (
	"errors"
	errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
	"net/http"

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

func (h Handler) Call(c echo.Context) error {
	var req adminseedparam.CallSeedRequest
	if err := c.Bind(&req); err != nil {
		return echo.NewHTTPError(http.StatusBadRequest)
	}

	var err error

	switch req.Action {
	case adminseedparam.ActionAddBenefactor:
		_, err = h.adminSeedSvc.AddBenefactor(c.Request().Context())
	case adminseedparam.AddKindBoxReq:
		_, err = h.adminSeedSvc.AddKindBoxReq(c.Request().Context(), entity.KindBoxReqAcceptedStatus)
	case adminseedparam.AssignSenderToKindBoxReq:
		_, err = h.adminSeedSvc.AssignSenderToKindBoxReq(c.Request().Context(), req.UserId)
	case adminseedparam.AddKindBox:
		_, err = h.adminSeedSvc.AddKindBox(c.Request().Context(), req.UserId, entity.KindBoxDeliveredStatus)
	case adminseedparam.AssignReceiverAgentKindBox:
		_, err = h.adminSeedSvc.AssignReceiverAgentKindBox(c.Request().Context(), req.UserId)
	default:
		err = errors.New(errmsg.ErrorMsgInvalidAction)
	}
	if err != nil {
		msg, code := httpmsg.Error(err)

		return echo.NewHTTPError(code, msg)
	}

	return c.JSON(http.StatusNoContent, "success!")
}
