<?php

namespace App\Events;

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

class UserSendMessageEvent implements ShouldBroadcast
{
	use Dispatchable, InteractsWithSockets, SerializesModels;
	
	public $conversation;
	public $total;
	
	public function __construct($conversation)
	{
		$this->conversation = $conversation;
		$this->total = Conversation::where('has_new', true)->count();
	}
	
	public function broadcastWith()
	{
		return [
			'conversation' => [
				'id'   => $this->conversation->id,
				'name' => $this->conversation->name,
			],
			'total'        => $this->total,
			'timestamp'    => now()->toISOString()
		];
	}
	
	public function broadcastOn()
	{
		return new PrivateChannel('chat.admin.all');
	}
}
