// Code generated by candi v1.3.1.

package resthandler

import (
	"net/http"

	"github.com/labstack/echo"

	"monorepo/services/user-service/internal/modules/auth/domain"
	"monorepo/services/user-service/internal/modules/auth/usecase"

	"pkg.agungdp.dev/candi/candihelper"
	"pkg.agungdp.dev/candi/codebase/interfaces"
	"pkg.agungdp.dev/candi/tracer"
	"pkg.agungdp.dev/candi/wrapper"
)

// RestHandler handler
type RestHandler struct {
	mw        interfaces.Middleware
	uc        usecase.AuthUsecase
	validator interfaces.Validator
}

// NewRestHandler create new rest handler
func NewRestHandler(mw interfaces.Middleware, uc usecase.AuthUsecase, validator interfaces.Validator) *RestHandler {
	return &RestHandler{
		mw: mw, uc: uc, validator: validator,
	}
}

// Mount handler with root "/"
// handling version in here
func (h *RestHandler) Mount(root *echo.Group) {
	v1Root := root.Group(candihelper.V1)

	auth := v1Root.Group("/auth")
	auth.POST("/login", h.login)
}

func (h *RestHandler) login(c echo.Context) error {
	trace := tracer.StartTrace(c.Request().Context(), "DeliveryREST:Hello")
	defer trace.Finish()
	ctx := trace.Context()

	var payload domain.LoginRequest
	if err := c.Bind(&payload); err != nil {
		return wrapper.NewHTTPResponse(http.StatusOK, err.Error()).JSON(c.Response())
	}

	data, err := h.uc.Login(ctx, &payload)
	if err != nil {
		return wrapper.NewHTTPResponse(http.StatusOK, err.Error()).JSON(c.Response())
	}

	return wrapper.NewHTTPResponse(http.StatusOK, "ok", data).JSON(c.Response())
}
