<?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;

/** 界面文档 §2.1：`result.published` */
final class DrawResultPublishedBroadcast implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

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

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

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

    /**
     * @param  array<string, mixed>|null  $data  与 GET draw/current 的 data 相同（含 result_items）
     */
    public function __construct(
        public readonly ?array $data,
        public readonly int $emittedAtMs,
    ) {}

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

    public function broadcastAs(): string
    {
        return 'result.published';
    }

    /** @return array{data: array<string, mixed>|null, emitted_at_ms: int} */
    public function broadcastWith(): array
    {
        return [
            'data' => $this->data,
            'emitted_at_ms' => $this->emittedAtMs,
        ];
    }
}
