package server

import (
	"fmt"
	"net/http"
	"strconv"

	"route256/cart/internal/domain/entity"

	"github.com/rs/zerolog/log"
)

func (s *Server) DeleteItemsByUserIDHandler(w http.ResponseWriter, r *http.Request) {
	strUserID := r.PathValue("user_id")
	userID, err := strconv.ParseInt(strUserID, 10, 64)
	if err != nil || userID <= 0 {
		if err == nil {
			err = fmt.Errorf("user_id must be greater than 0")
		}

		makeErrorResponse(w, err, http.StatusBadRequest)

		log.Trace().Err(err).Msgf("user_id=`%s`", strUserID)

		return
	}

	if err := s.cartService.DeleteItemsByUserID(r.Context(), entity.UID(userID)); err != nil {
		makeErrorResponse(w, err, http.StatusBadRequest)

		log.Trace().Err(err).Msgf("cartService.DeleteItemFromCart failed for uid=%d: %d", userID, http.StatusBadRequest)

		return
	}

	w.WriteHeader(http.StatusNoContent)
}
