<?php

namespace App\Events;

use App\Models\Bill;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class BillCreated implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $bill;

    /**
     * Create a new event instance.
     */
    public function __construct(Bill $bill)
    {
        // Load relationships for the notification
        $this->bill = $bill->load(['shop', 'distributor']);
    }

    /**
     * Get the channels the event should broadcast on.
     */
    public function broadcastOn(): array
    {
        return [
            new PrivateChannel('admin-notifications'),
        ];
    }

    /**
     * Data to broadcast.
     */
    public function broadcastWith(): array
    {
        return [
            'invoice_number' => $this->bill->invoice_number,
            'shop_name' => $this->bill->shop->name,
            'distributor_name' => $this->bill->distributor->name,
            'total_amount' => $this->bill->total_amount,
            'message' => "New bill created by {$this->bill->distributor->name} for {$this->bill->shop->name}",
        ];
    }
}
