<?php

/**
 * 文件功能：用户被踢出房间广播事件
 *
 * 管理员踢出/冻结用户时触发，前端监听后强制该用户跳转至大厅。
 *
 * @author   ChatRoom Laravel
 *
 * @version  1.0.0
 */

namespace App\Events;

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

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

    /**
     * 构造函数
     *
     * @param  int  $roomId  房间ID
     * @param  string  $username  被踢出的用户昵称
     * @param  string  $reason  踢出原因
     */
    public function __construct(
        public readonly int $roomId,
        public readonly string $username,
        public readonly string $reason = '',
    ) {}

    /**
     * 广播频道
     *
     * @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 [
            'username' => $this->username,
            'reason' => $this->reason,
        ];
    }
}
