class BoardPost < ApplicationRecord
  belongs_to :board
  belongs_to :participant

  validates :body, presence: true

  # 作成後にAction Cableで配信
  after_create_commit :broadcast_to_board

  private

  def broadcast_to_board
    # ボードごとのチャンネルに配信
    ActionCable.server.broadcast(
      "board_#{board_id}",
      {
        post_id: id,
        participant_id: participant.id,
        power: participant.power, # 国名
        body: body,
        phase: phase, # フェーズ情報
        created_at: created_at.in_time_zone("Asia/Tokyo").strftime("%Y-%m-%d %H:%M")
      }
    )
  end
end
