<?php

/**
 * 文件功能：管理员全员清屏广播事件
 *
 * 管理员触发清屏后，广播给房间内所有用户，前端监听后清除聊天记录（悄悄话除外）。
 *
 * @author   ChatRoom Laravel
 *
 * @version  1.0.0
 */

namespace App\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

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

    /**
     * 构造函数
     *
     * @param  int  $roomId  房间ID
     * @param  string  $operator  执行清屏的管理员用户名
     */
    public function __construct(
        public readonly int $roomId,
        public readonly string $operator,
    ) {}

    /**
     * 广播频道
     *
     * @return array<int, \Illuminate\Broadcasting\Channel>
     */
    public function broadcastOn(): array
    {
        return [
            new PresenceChannel('room.'.$this->roomId),
        ];
    }

    /**
     * 广播数据
     *
     * @return array<string, mixed>
     */
    public function broadcastWith(): array
    {
        return [
            'operator' => $this->operator,
        ];
    }
}
