<?php

/**
 * 文件功能：红包领取成功广播事件（广播至领取者私有频道）
 *
 * 触发时机：RedPacketController::claim() 成功后广播，
 * 前端收到后弹出 Toast 通知展示到账金额。
 *
 * @author  ChatRoom Laravel
 *
 * @version 1.0.0
 */

namespace App\Events;

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

    /**
     * @param  User  $claimer  领取用户
     * @param  int  $amount  领取金额
     * @param  int  $envelopeId  红包 ID
     */
    public function __construct(
        public readonly User $claimer,
        public readonly int $amount,
        public readonly int $envelopeId,
    ) {}

    /**
     * 广播至领取者私有频道。
     *
     * @return array<int, \Illuminate\Broadcasting\Channel>
     */
    public function broadcastOn(): array
    {
        return [new PrivateChannel('user.'.$this->claimer->id)];
    }

    /**
     * @return array<string, mixed>
     */
    public function broadcastWith(): array
    {
        return [
            'envelope_id' => $this->envelopeId,
            'amount' => $this->amount,
            'message' => "🧧 成功抢到 {$this->amount} 金币礼包！",
        ];
    }

    /** 广播事件名称。 */
    public function broadcastAs(): string
    {
        return 'red-packet.claimed';
    }
}
