<?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 after a sale is successfully created.
 * Broadcasts on the organisation's private channel so all connected
 * users in the same org receive the update in real-time.
 */
class SaleCreated implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public function __construct(
        public readonly int    $organizationId,
        public readonly int    $saleId,
        public readonly string $saleNumber,
        public readonly float  $total,
        public readonly string $status,
        public readonly string $customerName,
        public readonly ?int   $branchId,
        public readonly string $createdAt,
    ) {}

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

    public function broadcastAs(): string
    {
        return 'sale.created';
    }
}
