<?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 BrowserRefreshRequested implements ShouldBroadcastNow
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * 构造函数：记录房间与操作者信息。
     */
    public function __construct(
        public readonly int $roomId,
        public readonly string $operator,
        public readonly string $reason = '',
    ) {}

    /**
     * 广播频道：当前聊天室 PresenceChannel。
     *
     * @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,
            'reason' => $this->reason,
        ];
    }
}
