<?php

/**
 * 文件功能：节日福利开始广播事件
 *
 * 管理员配置的节日活动到达触发时间后，由 TriggerHolidayEventJob 触发，
 * 通过 Reverb WebSocket 广播给房间内所有在线用户，
 * 前端收到后弹出领取弹窗和公屏系统消息。
 *
 * @author  ChatRoom Laravel
 *
 * @version 1.0.0
 */

namespace App\Events;

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

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

    /**
     * @param  HolidayEvent  $event  节日活动实例
     */
    public function __construct(
        public readonly HolidayEvent $event,
    ) {}

    /**
     * 广播至房间公共频道（所有在线用户均可收到）。
     *
     * @return array<Channel>
     */
    public function broadcastOn(): array
    {
        return [
            new PresenceChannel('room.1'),
        ];
    }

    /**
     * 广播事件名。
     */
    public function broadcastAs(): string
    {
        return 'holiday.started';
    }

    /**
     * 广播数据：供前端构建弹窗和公屏消息。
     *
     * @return array<string, mixed>
     */
    public function broadcastWith(): array
    {
        return [
            'event_id' => $this->event->id,
            'name' => $this->event->name,
            'description' => $this->event->description,
            'total_amount' => $this->event->total_amount,
            'max_claimants' => $this->event->max_claimants,
            'distribute_type' => $this->event->distribute_type,
            'fixed_amount' => $this->event->fixed_amount,
            'claimed_count' => $this->event->claimed_count,
            'expires_at' => $this->event->expires_at?->toIso8601String(),
        ];
    }
}
