<?php

/**
 * 文件功能：求婚被拒事件（广播至求婚方私人频道）
 *
 * 触发时机：对方拒绝求婚，戒指消失后广播。
 *
 * @author  ChatRoom Laravel
 *
 * @version 1.0.0
 */

namespace App\Events;

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

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

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

    /**
     * 广播至求婚方私人频道。
     *
     * @return array<int, \Illuminate\Broadcasting\Channel>
     */
    public function broadcastOn(): array
    {
        return [new PrivateChannel('user.'.$this->marriage->user_id)];
    }

    /**
     * @return array<string, mixed>
     */
    public function broadcastWith(): array
    {
        return [
            'partner_username' => $this->marriage->partner?->username,
            'ring_name' => $this->marriage->ringItem?->name,
            'message' => '对方拒绝了您的求婚，戒指已消失。',
        ];
    }

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