package staffhttp

import (
	authmiddleware "legalgo-BE-go/internal/api/http/middleware/auth"
	staffsvc "legalgo-BE-go/internal/services/staffsvc"
	"legalgo-BE-go/internal/utilities/response"
	"legalgo-BE-go/internal/utilities/utils"
	"net/http"

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

func GetUsers(
	router chi.Router,
	authSvc staffsvc.Auth,
) {
	router.With(authmiddleware.Authorize()).Get("/staff/users", func(w http.ResponseWriter, r *http.Request) {
		ctx := r.Context()
		staffDetail, err := utils.GetTokenDetail(r)
		if err != nil {
			response.RespondJsonErrorWithCode(
				ctx,
				w,
				err,
				response.ErrBadRequest.Code,
				response.ErrBadRequest.HttpCode,
				"failed to get staff token",
			)
			return
		}

		if staffDetail.Role != "staff" {
			response.RespondJsonErrorWithCode(
				ctx,
				w,
				err,
				response.ErrUnauthorized.Code,
				response.ErrUnauthorized.HttpCode,
				"unauthorized",
			)
			return
		}

		users, err := authSvc.GetUsers()
		if err != nil {
			response.RespondJsonErrorWithCode(
				ctx,
				w,
				err,
				response.ErrBadRequest.Code,
				response.ErrBadRequest.HttpCode,
				"failed to get users",
			)
			return
		}

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