<?php

/**
 * 文件功能：结婚公告事件（广播至全房间）
 *
 * 触发时机：求婚被接受，正式结婚后广播。
 * 前端收到后展示全屏烟花特效 + 婚礼设置弹窗（仅婚姻双方）。
 *
 * @author  ChatRoom Laravel
 *
 * @version 1.0.0
 */

namespace App\Events;

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

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

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

    /**
     * 广播至当前所有房间（PresenceChannel room.*）。
     * 使用大厅房间 ID=1，若业务支持多房间可扩展。
     *
     * @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 [
            'marriage_id' => $this->marriage->id,
            'user' => $this->marriage->user?->only(['id', 'username', 'headface']),
            'partner' => $this->marriage->partner?->only(['id', 'username', 'headface']),
            'ring' => $this->marriage->ringItem?->only(['name', 'icon']),
            'married_at' => $this->marriage->married_at,
        ];
    }

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