package adshttp

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

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

func Delete(
	router chi.Router,
	adsSvc adssvc.Ads,
) {
	router.
		With(authmiddleware.Authorize()).
		Delete("/ads/{ads_id}/delete", func(w http.ResponseWriter, r *http.Request) {
			ctx := r.Context()
			adsID := chi.URLParam(r, "ads_id")

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

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

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