<?php

/**
 * 文件功能：猜谜活动开始广播事件
 *
 * 管理员手动出题或系统自动出题时触发，广播提示到聊天室。
 */

namespace App\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

/**
 * 类功能：向指定房间广播新一轮猜谜活动题目。
 */
class RiddleGameStarted implements ShouldBroadcastNow
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * 方法功能：构造新回合广播事件载荷。
     */
    public function __construct(
        public readonly int $roomId,
        public readonly string $quizType,
        public readonly string $hint,
        public readonly int $roundId,
        public readonly int $rewardGold = 0,
        public readonly int $rewardExp = 0,
    ) {}

    /**
     * 方法功能：声明广播频道。
     */
    public function broadcastOn(): array
    {
        return [
            new PresenceChannel('room.'.$this->roomId),
        ];
    }

    /**
     * 方法功能：声明广播数据。
     */
    public function broadcastWith(): array
    {
        $quizTypeLabel = \App\Models\Riddle::labelForType($this->quizType);

        return [
            'round_id' => $this->roundId,
            'quiz_type' => $this->quizType,
            'quiz_type_label' => $quizTypeLabel,
            'quiz_round_id' => $this->roundId,
            'quiz_hint' => $this->hint,
            'quiz_reward_gold' => $this->rewardGold,
            'quiz_reward_exp' => $this->rewardExp,
            'hint' => $this->hint,
            'reward_gold' => $this->rewardGold,
            'reward_exp' => $this->rewardExp,
            'message' => "📣 【猜谜活动·{$quizTypeLabel}】第 #{$this->roundId} 题开始！题面：{$this->hint}",
        ];
    }
}
