<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

/** Jackpot 爆池公共广播：`jackpot.burst` */
final class JackpotBurstBroadcast implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /** 异步广播队列 */
    public string $queue = 'broadcasts';

    /** 最多重试 3 次 */
    public int $tries = 3;

    /** 单任务超时 10 秒 */
    public int $timeout = 10;

    public function __construct(
        public readonly int $drawId,
        public readonly string $drawNo,
        public readonly string $firstPrizeNumber,
        public readonly string $currencyCode,
        public readonly int $totalPayoutAmount,
        public readonly int $winnerCount,
        public readonly string $triggerType,
        public readonly int $poolAmountAfter,
        public readonly int $emittedAtMs,
    ) {}

    /** @return array<int, Channel> */
    public function broadcastOn(): array
    {
        return [new Channel('lottery-hall')];
    }

    public function broadcastAs(): string
    {
        return 'jackpot.burst';
    }

    /** @return array{draw_id: int, draw_no: string, first_prize_number: string, currency_code: string, total_payout_amount: int, winner_count: int, trigger_type: string, pool_amount_after: int, emitted_at_ms: int} */
    public function broadcastWith(): array
    {
        return [
            'draw_id' => $this->drawId,
            'draw_no' => $this->drawNo,
            'first_prize_number' => $this->firstPrizeNumber,
            'currency_code' => $this->currencyCode,
            'total_payout_amount' => $this->totalPayoutAmount,
            'winner_count' => $this->winnerCount,
            'trigger_type' => $this->triggerType,
            'pool_amount_after' => $this->poolAmountAfter,
            'emitted_at_ms' => $this->emittedAtMs,
        ];
    }
}
