<?php

namespace App\Events;

use App\Models\Kot;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class KotUpdated implements ShouldBroadcastNow
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * Ensure the event is only broadcast after the database
     * transaction has been committed, so that all KOT items
     * are visible to the listeners when they refresh.
     *
     * @var bool
     */
    public $afterCommit = true;

    /**
     * The KOT instance.
     *
     * @var \App\Models\Kot
     */
    public $kot;

    public function __construct(Kot $kot)
    {
        $this->kot = $kot;
    }

    public function broadcastOn()
    {
        return new Channel('kots');
    }

    public function broadcastAs()
    {
        return 'kot.updated';
    }

    public function broadcastWith()
    {
        return [
            'kot_id' => $this->kot->id,
        ];
    }
}
