package staffhttp

import (
	"net/http"

	staffdomain "legalgo-BE-go/internal/domain/staff"
	staffsvc "legalgo-BE-go/internal/services/staff"
	"legalgo-BE-go/internal/utilities/response"
	"legalgo-BE-go/internal/utilities/utils"

	"github.com/go-chi/chi/v5"
	"github.com/go-playground/validator/v10"
	"github.com/redis/go-redis/v9"
)

func Register(
	router chi.Router,
	validate *validator.Validate,
	authSvc staffsvc.Auth,
	rdb *redis.Client,
) {
	router.Post("/staff/register", func(w http.ResponseWriter, r *http.Request) {
		ctx := r.Context()

		var spec staffdomain.StaffRegister

		if err := utils.UnmarshalBody(r, &spec); err != nil {
			response.ResponseWithErrorCode(
				ctx,
				w,
				err,
				response.ErrBadRequest.Code,
				response.ErrBadRequest.HttpCode,
				"failed to unmarshal request",
			)
			return
		}

		if err := validate.Struct(spec); err != nil {
			response.ResponseWithErrorCode(
				ctx,
				w,
				err,
				response.ErrBadRequest.Code,
				response.ErrBadRequest.HttpCode,
				err.(validator.ValidationErrors).Error(),
			)
			return
		}

		_, err := authSvc.Register(spec)
		if err != nil {
			response.ResponseWithErrorCode(
				ctx,
				w,
				err,
				response.ErrBadRequest.Code,
				response.ErrBadRequest.HttpCode,
				err.Error(),
			)
			return
		}

		response.RespondJsonSuccess(ctx, w, struct {
			Message string
		}{
			Message: "staff created successfully.",
		})
	})
}
