package service

import (
	"github.com/wtb-ordering/services/order/model"
	"github.com/wtb-ordering/services/order/repository"
)

type CartService struct {
	repo *repository.CartRepo
}

func NewCartService(repo *repository.CartRepo) *CartService {
	return &CartService{repo: repo}
}

func (s *CartService) Add(seatID string, userID uint, item model.CartItem) error {
	if item.Quantity <= 0 {
		return s.repo.Remove(seatID, item.DishID)
	}
	return s.repo.Upsert(seatID, userID, item)
}

func (s *CartService) List(seatID string) ([]model.CartItem, error) {
	return s.repo.ListBySeat(seatID)
}

func (s *CartService) Update(seatID string, userID uint, dishID uint, quantity int, remark string) error {
	if quantity <= 0 {
		return s.repo.Remove(seatID, dishID)
	}
	return s.repo.UpdateQuantity(seatID, dishID, quantity, remark)
}

func (s *CartService) Remove(seatID string, dishID uint) error {
	return s.repo.Remove(seatID, dishID)
}

func (s *CartService) Clear(seatID string) error {
	return s.repo.Clear(seatID)
}
