<?php

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

namespace App\Events;

use App\Models\HolidayEventRun;
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  HolidayEventRun  $run  节日福利发放批次
     */
    public function __construct(
        public readonly HolidayEventRun $run,
    ) {}

    /**
     * 广播至房间公共频道（所有在线用户均可收到）。
     *
     * @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 [
            'run_id' => $this->run->id,
            'event_id' => $this->run->holiday_event_id,
            'name' => $this->run->event_name,
            'description' => $this->run->event_description,
            'total_amount' => $this->run->total_amount,
            'max_claimants' => $this->run->max_claimants,
            'distribute_type' => $this->run->distribute_type,
            'fixed_amount' => $this->run->fixed_amount,
            'claimed_count' => $this->run->claimed_count,
            'expires_at' => $this->run->expires_at?->toIso8601String(),
            'scheduled_for' => $this->run->scheduled_for?->toIso8601String(),
            'repeat_type' => $this->run->repeat_type,
        ];
    }
}
