package newshttp

import (
	newsdomain "legalgo-BE-go/internal/domain/news"
	newssvc "legalgo-BE-go/internal/services/news"
	"legalgo-BE-go/internal/utilities/response"
	"net/http"

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

func GetAll(
	router chi.Router,
	newsSvc newssvc.News,
) {
	router.Get("/news", func(w http.ResponseWriter, r *http.Request) {
		var (
			news []newsdomain.News
			err  error
		)
		ctx := r.Context()
		query := r.URL.Query()

		category := query.Get("categories")
		tags := query.Get("tags")

		news, err = newsSvc.GetAll(category, tags)

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

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