<?php

namespace App\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

/**
 * Fired whenever product stock levels change (sale, purchase received,
 * inventory adjustment, stock transfer).
 * The frontend refreshes the affected products on the POS/product list.
 */
class StockUpdated implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public function __construct(
        public readonly int   $organizationId,
        public readonly ?int  $branchId,
        /** @var int[] Array of product IDs whose stock changed */
        public readonly array $productIds,
        /** Source action that triggered the change */
        public readonly string $source = 'sale',
    ) {}

    public function broadcastOn(): array
    {
        return [new PrivateChannel("org.{$this->organizationId}")];
    }

    public function broadcastAs(): string
    {
        return 'stock.updated';
    }
}
