<?php

/**
 * 文件功能：求婚事件（广播至被求婚方私人频道）
 *
 * 触发时机：MarriageController::propose() 成功后广播。
 * B 上线时前端订阅频道立即收到，展示求婚 Banner 弹窗。
 *
 * @author  ChatRoom Laravel
 *
 * @version 1.0.0
 */

namespace App\Events;

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

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

    /**
     * @param  Marriage  $marriage  婚姻记录
     * @param  User  $proposer  求婚方
     * @param  User  $target  被求婚方
     */
    public function __construct(
        public readonly Marriage $marriage,
        public readonly User $proposer,
        public readonly User $target,
    ) {}

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

    /**
     * 广播数据。
     *
     * @return array<string, mixed>
     */
    public function broadcastWith(): array
    {
        return [
            'marriage_id' => $this->marriage->id,
            'proposer' => [
                'username' => $this->proposer->username,
                'headface' => $this->proposer->headface,
                'user_level' => $this->proposer->user_level,
            ],
            'ring' => $this->marriage->ringItem?->only(['name', 'icon']),
            'expires_at' => $this->marriage->expires_at,
        ];
    }

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