<?php

namespace Modules\Cart\Services;

use Modules\Cart\Repositories\Interfaces\CartRepositoryInterface;

class CartService
{
    public function __construct(
        private CartRepositoryInterface $cartRepository
    ) {}

    public function getCart(string $sessionId): \Illuminate\Database\Eloquent\Collection
    {
        return $this->cartRepository->getCart($sessionId);
    }

    public function addItem(array $data): \Modules\Cart\Models\CartItem
    {
        return $this->cartRepository->addItem($data);
    }

    public function updateQuantity(int $id, int $quantity): \Modules\Cart\Models\CartItem
    {
        return $this->cartRepository->updateQuantity($id, $quantity);
    }

    public function removeItem(int $id): bool
    {
        return $this->cartRepository->removeItem($id);
    }

    public function clearCart(string $sessionId): bool
    {
        return $this->cartRepository->clearCart($sessionId);
    }

    public function getCartTotal(string $sessionId): array
    {
        $items = $this->cartRepository->getCart($sessionId);
        $subtotal = $items->sum(fn($item) => $item->price * $item->quantity);
        return [
            'items' => $items,
            'subtotal' => $subtotal,
            'delivery_fee' => 0,
            'service_fee' => 0,
            'total' => $subtotal,
            'count' => $items->sum('quantity'),
        ];
    }

    public function syncCart(string $sessionId, array $items): \Illuminate\Database\Eloquent\Collection
    {
        return $this->cartRepository->syncCart($sessionId, $items);
    }
}
