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

    /**
     * 方法功能：构造答题成功广播事件载荷。
     */
    public function __construct(
        public readonly int $roomId,
        public readonly int $roundId,
        public readonly string $quizType,
        public readonly string $answer,
        public readonly string $winnerUsername,
        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_answer' => $this->answer,
            'quiz_reward_gold' => $this->rewardGold,
            'quiz_reward_exp' => $this->rewardExp,
            'quiz_round_ended_id' => $this->roundId,
            'answer' => $this->answer,
            'winner_username' => $this->winnerUsername,
            'reward_gold' => $this->rewardGold,
            'reward_exp' => $this->rewardExp,
        ];
    }
}
