<?php

namespace App\Livewire\Store;

use App\Models\Cart as CartModel;
use Livewire\Attributes\On;
use Livewire\Component;

/**
 * Badge jumlah item keranjang.
 * Ditampilkan di header (icon cart) dan bottom nav (tab Keranjang).
 * Update otomatis saat event 'cart-updated' diterima.
 */
class CartBadge extends Component
{
    public int $count = 0;

    public function mount(): void
    {
        $this->refreshCount();
    }

    #[On('cart-updated')]
    public function refreshCount(): void
    {
        $this->count = CartModel::forSession(session()->getId())->sum('quantity');
    }

    public function render()
    {
        return view('livewire.store.partials.cart-badge');
    }
}
