package staffhttp

import (
	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"
	"net/http"

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

func Update(
	router chi.Router,
	authSvc staffsvc.Auth,
) {
	router.Put("/staff/update", func(w http.ResponseWriter, r *http.Request) {
		ctx := r.Context()

		destructedToken, err := utils.GetTokenDetail(r)
		if err != nil {
			response.ResponseWithErrorCode(
				ctx,
				w,
				err,
				response.ErrBadRequest.Code,
				response.ErrBadRequest.HttpCode,
				err.Error(),
			)
			return
		}

		var spec staffdomain.StaffUpdate

		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 := authSvc.Update(destructedToken.ID, spec); 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)
	})
}
