<?php

/**
 * 文件功能：百家乐开局广播事件
 *
 * 新局开始时广播给房间所有用户，携带局次 ID 和下注截止时间，
 * 前端收到后展示倒计时下注面板。
 *
 * @author  ChatRoom Laravel
 *
 * @version 1.0.0
 */

namespace App\Events;

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

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

    /**
     * @param  BaccaratRound  $round  本局信息
     */
    public function __construct(
        public readonly BaccaratRound $round,
    ) {}

    /**
     * 广播至房间公共频道。
     *
     * @return array<Channel>
     */
    public function broadcastOn(): array
    {
        return [new PresenceChannel('room.1')];
    }

    /**
     * 广播事件名（前端监听 .baccarat.opened）。
     */
    public function broadcastAs(): string
    {
        return 'baccarat.opened';
    }

    /**
     * 广播数据。
     *
     * @return array<string, mixed>
     */
    public function broadcastWith(): array
    {
        return [
            'round_id' => $this->round->id,
            'bet_opens_at' => $this->round->bet_opens_at->toIso8601String(),
            'bet_closes_at' => $this->round->bet_closes_at->toIso8601String(),
            'bet_seconds' => (int) now()->diffInSeconds($this->round->bet_closes_at),
        ];
    }
}
