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

    /**
     * Create a new event instance.
     *
     * @param  int  $roomId  房间ID
     * @param  string  $username  被封口的用户昵称
     * @param  int  $muteTime  封口时长（如 10 分钟），如果为 0 则是解封
     */
    public function __construct(
        public readonly int $roomId,
        public readonly string $username,
        public readonly int $muteTime,
        public readonly string $message = '',
        public readonly string $operator = '',
    ) {}

    /**
     * Get the channels the event should broadcast on.
     *
     * @return array<int, \Illuminate\Broadcasting\Channel>
     */
    public function broadcastOn(): array
    {
        return [
            new PresenceChannel('room.'.$this->roomId),
        ];
    }

    /**
     * 获取广播时的数据
     *
     * @return array<string, mixed>
     */
    public function broadcastWith(): array
    {
        $statusMessage = $this->message ?: ($this->muteTime > 0
            ? "管理员 [{$this->operator}] 已将 [{$this->username}] 禁言 {$this->muteTime} 分钟。"
            : "用户 [{$this->username}] 已被解除禁言。");

        return [
            'username' => $this->username,
            'mute_time' => $this->muteTime,
            'operator' => $this->operator,
            'message' => $statusMessage,
        ];
    }
}
