<?php

/**
 * 文件功能：求婚超时失效事件（广播至求婚方私人频道）
 *
 * 触发时机：Horizon Job ExpireMarriageProposals 扫描到超时求婚后广播。
 *
 * @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 MarriageExpired 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.expired';
    }
}
