package adminhandler

import (
	"net/http"

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

// Register godoc
// @Summary      Register an admin by super-admin
// @Tags         Admins
// @Accept       json
// @Produce      json
// @Param        Request body   adminserviceparam.RegisterRequest true "Admin Register Request Body"
// @Success      200  {object}  adminserviceparam.RegisterResponse
// @Failure      400  {string}  "Bad request"
// @Security 	 AuthBearerAdmin
// @Router       /admins/register [post].
func (h Handler) Register(c echo.Context) error {
	var req adminserviceparam.RegisterRequest

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

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