<?php

/**
 * 文件功能：五子棋落子广播事件
 *
 * 每次玩家（或 AI）落子后通过私有对局频道广播，
 * 双方前端实时更新棋盘显示并切换行棋方。
 *
 * @author  ChatRoom Laravel
 *
 * @version 1.0.0
 */

namespace App\Events;

use App\Models\GomokuGame;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class GomokuMovedEvent implements ShouldBroadcastNow
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * @param  GomokuGame  $game  当前对局
     * @param  int  $row  落子行（0-14）
     * @param  int  $col  落子列（0-14）
     * @param  int  $color  落子颜色（1=黑 2=白）
     */
    public function __construct(
        public readonly GomokuGame $game,
        public readonly int $row,
        public readonly int $col,
        public readonly int $color,
    ) {}

    /**
     * 广播至对局私有频道（仅双方可见）。
     *
     * @return array<\Illuminate\Broadcasting\Channel>
     */
    public function broadcastOn(): array
    {
        return [new PrivateChannel("gomoku.{$this->game->id}")];
    }

    /**
     * 广播事件名（前端监听 .gomoku.moved）。
     */
    public function broadcastAs(): string
    {
        return 'gomoku.moved';
    }

    /**
     * 广播数据。
     *
     * @return array<string, mixed>
     */
    public function broadcastWith(): array
    {
        return [
            'game_id' => $this->game->id,
            'row' => $this->row,
            'col' => $this->col,
            'color' => $this->color,
            'current_turn' => $this->game->current_turn,
            'board' => $this->game->board,
        ];
    }
}
