<?php

/**
 * 文件功能：婚礼庆典事件（广播至全房间）
 *
 * 触发时机：婚礼红包触发分发后广播。
 * 前端收到后：播放烟花特效 + 婚礼音效 + 展示红包弹窗（含领取按钮）。
 *
 * @author  ChatRoom Laravel
 *
 * @version 1.0.0
 */

namespace App\Events;

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

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

    /**
     * @param  WeddingCeremony  $ceremony  婚礼仪式记录
     * @param  Marriage  $marriage  婚姻记录
     */
    public function __construct(
        public readonly WeddingCeremony $ceremony,
        public readonly Marriage $marriage,
    ) {}

    /**
     * 广播至全房间。
     *
     * @return array<int, \Illuminate\Broadcasting\Channel>
     */
    public function broadcastOn(): array
    {
        return [new PresenceChannel('room.1')];
    }

    /**
     * 广播数据（前端据此展示红包弹窗及新人信息）。
     *
     * @return array<string, mixed>
     */
    public function broadcastWith(): array
    {
        $this->marriage->load(['user:id,username,headface', 'partner:id,username,headface', 'ringItem:id,name,icon']);

        return [
            'ceremony_id' => $this->ceremony->id,
            'tier_name' => $this->ceremony->tier?->name ?? '婚礼',
            'tier_icon' => $this->ceremony->tier?->icon ?? '🎊',
            'total_amount' => $this->ceremony->total_amount,
            'expires_at' => $this->ceremony->expires_at,
            'user' => $this->marriage->user?->only(['id', 'username', 'headface']),
            'partner' => $this->marriage->partner?->only(['id', 'username', 'headface']),
            'ring' => $this->marriage->ringItem?->only(['name', 'icon']),
        ];
    }

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