<?php

/**
 * 文件功能：用户"拍一拍"广播事件
 *
 * 用户输入 /拍一拍 用户名 后触发，通过 WebSocket 广播给房间内所有用户，
 * 前端显示 "XXX拍了拍XXX" 消息并触发屏幕抖动动画。
 *
 * @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 UserPat implements ShouldBroadcastNow
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * 构造函数
     *
     * @param  int  $roomId  房间 ID
     * @param  string  $fromUser  拍人的用户
     * @param  string  $targetUser  被拍的用户
     * @param  string  $displayText  前端展示文本，如 "流星 拍了拍 张三"
     */
    public function __construct(
        public readonly int $roomId,
        public readonly string $fromUser,
        public readonly string $targetUser,
        public readonly string $displayText,
        public readonly ?string $fromUserHeadface = null,
    ) {}

    /**
     * 广播频道：向房间内所有在线用户推送
     *
     * @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 [
            'from_user' => $this->fromUser,
            'target_user' => $this->targetUser,
            'display_text' => $this->displayText,
            'from_user_headface' => $this->fromUserHeadface,
        ];
    }
}
