<?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 MarriageDivorced implements ShouldBroadcastNow
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * @param  Marriage  $marriage  婚姻记录
     * @param  string  $divorceType  离婚类型（mutual|forced|auto|admin）
     */
    public function __construct(
        public readonly Marriage $marriage,
        public readonly string $divorceType,
    ) {}

    /**
     * 广播至全房间。
     *
     * @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', 'partner:id,username']);

        return [
            'user_username' => $this->marriage->user?->username,
            'partner_username' => $this->marriage->partner?->username,
            'divorce_type' => $this->divorceType,
        ];
    }

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