package authhttp

import (
	"errors"
	staffdomain "legalgo-BE-go/internal/domain/staff"
	authsvc "legalgo-BE-go/internal/services/auth"
	"legalgo-BE-go/internal/utilities/response"
	"legalgo-BE-go/internal/utilities/utils"
	"net/http"

	"github.com/go-chi/chi/v5"
)

func UpdateStaff(
	router chi.Router,
	authSvc authsvc.Auth,
) {
	router.Patch("/staff/{id}/update", func(w http.ResponseWriter, r *http.Request) {
		ctx := r.Context()

		id := chi.URLParam(r, "id")
		if id == "" {
			response.ResponseWithErrorCode(
				ctx,
				w,
				errors.New("provided id is empty"),
				response.ErrBadRequest.Code,
				response.ErrBadRequest.HttpCode,
				"required params is not provided",
			)
			return
		}

		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
		}

		staff := staffdomain.Staff{
			ID:       id,
			Email:    spec.Email,
			Password: spec.Password,
			Name:     spec.Name,
		}

		if err := authSvc.UpdateStaff(staff); err != nil {
			response.ResponseWithErrorCode(
				ctx,
				w,
				err,
				response.ErrBadRequest.Code,
				response.ErrBadRequest.HttpCode,
				err.Error(),
			)
			return
		}

		responsePayload := struct {
			Message string
		}{
			Message: "update staff success",
		}

		response.RespondJsonSuccess(ctx, w, responsePayload)
	})
}
