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"
)

// RefreshAccess godoc
// @Summary      Get a new access token by providing a refresh token
// @Tags         Admins
// @Accept       json
// @Produce      json
// @Param        Request body   adminserviceparam.RefreshAccessRequest true "Refresh access request body"
// @Success      200  {object}  adminserviceparam.RefreshAccessResponse
// @Failure      400  {string}  "Bad Request"
// @Failure      422  {string}  "invalid or expired jwt"
// @Failure      500  {string}  "something went wrong"
// @Router       /admins/refresh-access [post].
func (h Handler) RefreshAccess(c echo.Context) error {
	var req adminserviceparam.RefreshAccessRequest

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

	resp, err := h.adminSvc.RefreshAccess(c.Request().Context(), req)
	if err != nil {
		msg, code := httpmsg.Error(err)

		return echo.NewHTTPError(code, msg)
	}

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