package categoryhttp

import (
	"fmt"
	authmiddleware "legalgo-BE-go/internal/api/http/middleware/auth"
	categorysvc "legalgo-BE-go/internal/services/category"
	"legalgo-BE-go/internal/utilities/response"
	"net/http"

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

func Delete(
	router chi.Router,
	categorySvc categorysvc.Category,
) {
	router.
		With(authmiddleware.Authorize()).
		Delete("/category/{category_id}/delete", func(w http.ResponseWriter, r *http.Request) {
			ctx := r.Context()
			categoryID := chi.URLParam(r, "category_id")

			if categoryID == "" {
				response.RespondJsonErrorWithCode(
					ctx,
					w,
					fmt.Errorf("category id is not provided"),
					response.ErrBadRequest.Code,
					response.ErrBadRequest.HttpCode,
					"category id is not provided",
				)
				return
			}

			if err := categorySvc.Delete(categoryID); err != nil {
				response.RespondJsonErrorWithCode(
					ctx,
					w,
					err,
					response.ErrBadRequest.Code,
					response.ErrBadRequest.HttpCode,
					err.Error(),
				)
				return
			}

			response.RespondJsonSuccess(ctx, w, struct {
				Message string
			}{
				Message: "category has been deleted",
			})
		})
}
