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

// LoginByPhoneNumber godoc
// @Summary 	 Admin login by	PhoneNumber
// @Tags         Admin
// @Accept       json
// @Produce      json
// @Param        Request body   adminserviceparam.LoginWithPhoneNumberRequest true "Admin login request body"
// @Success      200  {object}  adminserviceparam.LoginWithPhoneNumberResponse
// @Failure      400  {string}  "Bad request"
// @Router       /admins/login-by-phone [post]
func (h Handler) LoginByPhoneNumber(c echo.Context) error {
	var req adminserviceparam.LoginWithPhoneNumberRequest

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

	if fieldErrors, err := h.adminVld.ValidateLoginWithPhoneNumberRequest(req); err != nil {
		msg, code := httpmsg.Error(err)

		return c.JSON(code, echo.Map{
			"message": msg,
			"errors":  fieldErrors,
		})
	}
	resp, sErr := h.adminSvc.LoginWithPhoneNumber(c.Request().Context(), req)
	if sErr != nil {
		msg, code := httpmsg.Error(sErr)

		return echo.NewHTTPError(code, msg)
	}

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