package newshttp

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

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

func Delete(
	router chi.Router,
	newsSvc newssvc.News,
) {
	router.
		With(authmiddleware.Authorize()).
		Delete("/news/{news_id}/delete", func(w http.ResponseWriter, r *http.Request) {
			ctx := r.Context()
			newsID := chi.URLParam(r, "news_id")

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

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

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