package adminhandler

import (
	"net/http"

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

// Profile godoc
// @Summary 	 Admin profile
// @Tags         Admins
// @Accept       json
// @Produce      json
// @Success      200  {object}  adminserviceparam.ProfileResponse
// @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/profile [get].
func (h Handler) Profile(c echo.Context) error {
	var req adminserviceparam.ProfileRequest

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

	req.AdminID = claim.GetClaimsFromEchoContext(c).UserID
	resp, sErr := h.adminSvc.AdminGetProfile(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)
}
